Skip to content

Commit 609878c

Browse files
committed
feat(test): ability to include tests inside tests
only the first rule in the test right now, will be expanded later. Closes: #66
1 parent d8fcde4 commit 609878c

File tree

6 files changed

+68
-30
lines changed

6 files changed

+68
-30
lines changed

src/main/java/ru/ewc/checklogic/server/WebPages.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ public Response uninitializedPage() {
8181

8282
public Response testPage() {
8383
final CheckSuite suite = CheckSuite.using(
84-
new CombinedCsvFileReader(Path.of(this.root, "tests").toUri(), ".csv", ";")
84+
new CombinedCsvFileReader(Path.of(this.root, "tests").toUri(), ".csv", ";"),
85+
this.root,
86+
this.config.requestLocatorName()
8587
);
8688
final long start = System.currentTimeMillis();
87-
final List<TestResult> results = suite.perform(this.root, this.config.requestLocatorName());
89+
final List<TestResult> results = suite.perform();
8890
final String rows = results.stream()
8991
.sorted(Comparator.naturalOrder())
9092
.map(TestResult::asHtmlTableRow)

src/main/java/ru/ewc/checklogic/testing/CheckFile.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ArrayList;
2727
import java.util.List;
2828
import java.util.stream.Collectors;
29+
import lombok.Getter;
2930
import ru.ewc.checklogic.ServerContextFactory;
3031
import ru.ewc.decisions.api.ComputationContext;
3132
import ru.ewc.decisions.api.OutputTracker;
@@ -41,27 +42,45 @@
4142
* @since 0.4.1
4243
*/
4344
public final class CheckFile {
45+
/**
46+
* The file containing the tests.
47+
*/
48+
@Getter
49+
private final String file;
50+
4451
/**
4552
* The collection of single tests inside the file.
4653
*/
4754
private final List<RuleFragments> tests;
4855

49-
public CheckFile(final List<RuleFragments> tests) {
56+
/**
57+
* The name of the request locator.
58+
*/
59+
private final String request;
60+
61+
/**
62+
* The link to the suite that contains this file.
63+
*/
64+
private CheckSuite suite;
65+
66+
public CheckFile(final String file, final List<RuleFragments> tests, final String request) {
67+
this.file = file;
5068
this.tests = tests;
69+
this.request = request;
5170
}
5271

53-
public List<TestResult> performChecks(final String root, final String locator) {
72+
public List<TestResult> performChecks(final String root, final CheckSuite files) {
73+
this.suite = files;
5474
return this.tests.stream()
55-
.map(rule -> CheckFile.performAndLog(root, rule, locator))
75+
.map(rule -> this.getTestResult(rule, ServerContextFactory.create(root).context()))
5676
.toList();
5777
}
5878

59-
private static TestResult performAndLog(
60-
final String root,
61-
final RuleFragments rule,
62-
final String locator
63-
) {
64-
final ComputationContext ctx = ServerContextFactory.create(root).context();
79+
public void performInSameContext(final ComputationContext ctx) {
80+
this.getTestResult(this.tests.getFirst(), ctx);
81+
}
82+
83+
private TestResult getTestResult(final RuleFragments rule, final ComputationContext ctx) {
6584
logCheckpoint(ctx, "%s - started".formatted(rule.header()));
6685
final OutputTracker<String> tracker = ctx.startTracking();
6786
final List<CheckFailure> failures = new ArrayList<>(1);
@@ -72,7 +91,7 @@ private static TestResult performAndLog(
7291
failures.add(new CheckFailure(check.asString(), check.result()));
7392
}
7493
} else {
75-
CheckFile.perform(fragment, ctx, locator);
94+
this.perform(fragment, ctx);
7695
}
7796
}
7897
logCheckpoint(ctx, "%s - %s".formatted(rule.header(), CheckFile.desc(failures)));
@@ -84,17 +103,16 @@ private static TestResult performAndLog(
84103
);
85104
}
86105

87-
private static void perform(
88-
final RuleFragment fragment,
89-
final ComputationContext ctx,
90-
final String locator
91-
) {
106+
private void perform(final RuleFragment fragment, final ComputationContext ctx) {
92107
switch (fragment.type()) {
93108
case "ASG" -> new Assignment(fragment.left(), fragment.right()).performIn(ctx);
94-
case "OUT" -> {
95-
if ("execute".equals(fragment.left())) {
109+
case "EXE" -> {
110+
if ("command".equals(fragment.left())) {
96111
ctx.perform(fragment.right());
97-
ctx.resetComputationState(locator);
112+
ctx.resetComputationState(this.request);
113+
}
114+
if ("include".equals(fragment.left())) {
115+
this.suite.findAndPerform(fragment.right(), ctx);
98116
}
99117
}
100118
default -> {

src/main/java/ru/ewc/checklogic/testing/CheckSuite.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.Collection;
2828
import java.util.List;
29+
import ru.ewc.decisions.api.ComputationContext;
2930
import ru.ewc.decisions.input.ContentsReader;
3031

3132
/**
@@ -41,22 +42,39 @@ public final class CheckSuite {
4142
*/
4243
private final Collection<CheckFile> tests;
4344

44-
private CheckSuite(final Collection<CheckFile> tests) {
45+
/**
46+
* The root directory of the business logic source files.
47+
*/
48+
private final String root;
49+
50+
private CheckSuite(final Collection<CheckFile> tests, final String root) {
4551
this.tests = tests;
52+
this.root = root;
4653
}
4754

48-
public static CheckSuite using(final ContentsReader reader) {
49-
return new CheckSuite(reader.readAll().stream()
50-
.map(sl -> new CheckFile(sl.specifiedRulesFragments()))
51-
.toList()
55+
public static CheckSuite using(
56+
final ContentsReader reader,
57+
final String root,
58+
final String request
59+
) {
60+
return new CheckSuite(
61+
reader.readAll().stream()
62+
.map(sl -> new CheckFile(sl.fileName(), sl.specifiedRulesFragments(), request))
63+
.toList(),
64+
root
5265
);
5366
}
5467

55-
public List<TestResult> perform(final String root, final String locator) {
68+
public List<TestResult> perform() {
5669
return this.tests.stream()
57-
.map(test -> test.performChecks(root, locator))
70+
.map(test -> test.performChecks(this.root, this))
5871
.flatMap(List::stream)
5972
.toList();
6073
}
6174

75+
public void findAndPerform(final String file, final ComputationContext ctx) {
76+
this.tests.stream().filter(test -> file.equals(test.getFile()))
77+
.findFirst()
78+
.ifPresent(test -> test.performInSameContext(ctx));
79+
}
6280
}

src/test/resources/tic-tac-toe/tests/00-initialize.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
OUT;execute;initialize
1+
EXE;command;initialize
22
CND;table::currentPlayer;X
33
CND;table::nextPlayer;O
44
CND;cells::A1;empty

src/test/resources/tic-tac-toe/tests/01-first-move.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ASG;cells::B3;empty;empty
1212
ASG;cells::C1;empty;empty
1313
ASG;cells::C2;empty;empty
1414
ASG;cells::C3;empty;empty
15-
OUT;execute;computed_move;computed_move
15+
EXE;command;computed_move;computed_move
1616
CND;cells::A1;empty;X
1717
CND;computed_move::available;false;false
1818
CND;game_state::is_over;false;false

src/test/resources/tic-tac-toe/tests/02-second-move.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ASG;cells::B3;empty;empty
1111
ASG;cells::C1;empty;empty
1212
ASG;cells::C2;empty;empty
1313
ASG;cells::C3;empty;empty
14-
OUT;execute;computed_move;computed_move
14+
EXE;command;computed_move;computed_move
1515
CND;computed_move::available;false;false
1616
CND;cells::A1;X;X
1717
CND;cells::B2;O;empty

0 commit comments

Comments
 (0)