Skip to content

Commit

Permalink
feat: ability to make assertions against state
Browse files Browse the repository at this point in the history
Closes: #6
  • Loading branch information
nergal-perm committed May 13, 2024
1 parent 3cf3f1c commit 0ec58d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/main/java/ru/ewc/checklogic/Computation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@

import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.yaml.snakeyaml.Yaml;
import ru.ewc.commands.CommandsFacade;
import ru.ewc.decisions.api.ComputationContext;
import ru.ewc.decisions.api.DecitaException;
import ru.ewc.decisions.api.DecitaFacade;
import ru.ewc.decisions.api.Locator;
Expand Down Expand Up @@ -108,6 +110,20 @@ public void perform(final Transition command) {
this.commands.perform(command.name(), this.state.mergedWith(command.request()));
}

public boolean hasStateFor(String table) {
return this.state.hasLocator(table);
}

public Map<String, String> stateFor(String table, Map<String, String> state) {
final Locator locator = this.state.locatorFor(table);
final ComputationContext context = new ComputationContext(this.state);
Map<String, String> actual = new HashMap<>(state.size());
for (String s : state.keySet()) {
actual.put(s, locator.fragmentBy(s, context));
}
return actual;
}

/**
* Loads the state from the specified {@code InputStream}.
*
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/ru/ewc/checklogic/LogicChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,17 @@ static void testPerformingFileBasedTest(final TestData test, final SoftAssertion
target.perform(command);
}
for (final String table : test.expectations.keySet()) {
softly
.assertThat(target.decideFor(table))
.describedAs(String.format("Table '%s'", table))
.isEqualTo(test.expectations.get(table));
if (target.hasStateFor(table)) {
softly
.assertThat(target.stateFor(table, test.expectations.get(table)))
.describedAs(String.format("State for entity '%s'", table))
.containsExactlyInAnyOrderEntriesOf(test.expectations.get(table));
} else {
softly
.assertThat(target.decideFor(table))
.describedAs(String.format("Table '%s'", table))
.isEqualTo(test.expectations.get(table));
}
}
softly.assertAll();
LOGGER.info("Running test for %s... done".formatted(test.toString()));
Expand Down
7 changes: 6 additions & 1 deletion src/test/resources/tic-tac-toe/states/01-first-X-move.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ commands:
---
game_state:
is_over: "false"
winner: "none"
winner: "none"
cells:
A1: "X"
table:
nextPlayer: "X"
currentPlayer: "O"

0 comments on commit 0ec58d0

Please sign in to comment.