Skip to content

Commit 0ec58d0

Browse files
committed
feat: ability to make assertions against state
Closes: #6
1 parent 3cf3f1c commit 0ec58d0

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/main/java/ru/ewc/checklogic/Computation.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626

2727
import java.io.InputStream;
2828
import java.net.URI;
29+
import java.util.HashMap;
2930
import java.util.Map;
3031
import java.util.function.Function;
3132
import java.util.stream.Collectors;
3233
import org.yaml.snakeyaml.Yaml;
3334
import ru.ewc.commands.CommandsFacade;
35+
import ru.ewc.decisions.api.ComputationContext;
3436
import ru.ewc.decisions.api.DecitaException;
3537
import ru.ewc.decisions.api.DecitaFacade;
3638
import ru.ewc.decisions.api.Locator;
@@ -108,6 +110,20 @@ public void perform(final Transition command) {
108110
this.commands.perform(command.name(), this.state.mergedWith(command.request()));
109111
}
110112

113+
public boolean hasStateFor(String table) {
114+
return this.state.hasLocator(table);
115+
}
116+
117+
public Map<String, String> stateFor(String table, Map<String, String> state) {
118+
final Locator locator = this.state.locatorFor(table);
119+
final ComputationContext context = new ComputationContext(this.state);
120+
Map<String, String> actual = new HashMap<>(state.size());
121+
for (String s : state.keySet()) {
122+
actual.put(s, locator.fragmentBy(s, context));
123+
}
124+
return actual;
125+
}
126+
111127
/**
112128
* Loads the state from the specified {@code InputStream}.
113129
*

src/main/java/ru/ewc/checklogic/LogicChecker.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,17 @@ static void testPerformingFileBasedTest(final TestData test, final SoftAssertion
103103
target.perform(command);
104104
}
105105
for (final String table : test.expectations.keySet()) {
106-
softly
107-
.assertThat(target.decideFor(table))
108-
.describedAs(String.format("Table '%s'", table))
109-
.isEqualTo(test.expectations.get(table));
106+
if (target.hasStateFor(table)) {
107+
softly
108+
.assertThat(target.stateFor(table, test.expectations.get(table)))
109+
.describedAs(String.format("State for entity '%s'", table))
110+
.containsExactlyInAnyOrderEntriesOf(test.expectations.get(table));
111+
} else {
112+
softly
113+
.assertThat(target.decideFor(table))
114+
.describedAs(String.format("Table '%s'", table))
115+
.isEqualTo(test.expectations.get(table));
116+
}
110117
}
111118
softly.assertAll();
112119
LOGGER.info("Running test for %s... done".formatted(test.toString()));

src/test/resources/tic-tac-toe/states/01-first-X-move.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ commands:
2020
---
2121
game_state:
2222
is_over: "false"
23-
winner: "none"
23+
winner: "none"
24+
cells:
25+
A1: "X"
26+
table:
27+
nextPlayer: "X"
28+
currentPlayer: "O"

0 commit comments

Comments
 (0)