Skip to content

Commit

Permalink
feat(test): ability to include tests inside tests
Browse files Browse the repository at this point in the history
only the first rule in the test right now, will be expanded later.

Closes: #66
  • Loading branch information
nergal-perm committed Sep 16, 2024
1 parent d8fcde4 commit 609878c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
6 changes: 4 additions & 2 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ public Response uninitializedPage() {

public Response testPage() {
final CheckSuite suite = CheckSuite.using(
new CombinedCsvFileReader(Path.of(this.root, "tests").toUri(), ".csv", ";")
new CombinedCsvFileReader(Path.of(this.root, "tests").toUri(), ".csv", ";"),
this.root,
this.config.requestLocatorName()
);
final long start = System.currentTimeMillis();
final List<TestResult> results = suite.perform(this.root, this.config.requestLocatorName());
final List<TestResult> results = suite.perform();
final String rows = results.stream()
.sorted(Comparator.naturalOrder())
.map(TestResult::asHtmlTableRow)
Expand Down
54 changes: 36 additions & 18 deletions src/main/java/ru/ewc/checklogic/testing/CheckFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import ru.ewc.checklogic.ServerContextFactory;
import ru.ewc.decisions.api.ComputationContext;
import ru.ewc.decisions.api.OutputTracker;
Expand All @@ -41,27 +42,45 @@
* @since 0.4.1
*/
public final class CheckFile {
/**
* The file containing the tests.
*/
@Getter
private final String file;

/**
* The collection of single tests inside the file.
*/
private final List<RuleFragments> tests;

public CheckFile(final List<RuleFragments> tests) {
/**
* The name of the request locator.
*/
private final String request;

/**
* The link to the suite that contains this file.
*/
private CheckSuite suite;

public CheckFile(final String file, final List<RuleFragments> tests, final String request) {
this.file = file;
this.tests = tests;
this.request = request;
}

public List<TestResult> performChecks(final String root, final String locator) {
public List<TestResult> performChecks(final String root, final CheckSuite files) {
this.suite = files;
return this.tests.stream()
.map(rule -> CheckFile.performAndLog(root, rule, locator))
.map(rule -> this.getTestResult(rule, ServerContextFactory.create(root).context()))
.toList();
}

private static TestResult performAndLog(
final String root,
final RuleFragments rule,
final String locator
) {
final ComputationContext ctx = ServerContextFactory.create(root).context();
public void performInSameContext(final ComputationContext ctx) {
this.getTestResult(this.tests.getFirst(), ctx);
}

private TestResult getTestResult(final RuleFragments rule, final ComputationContext ctx) {
logCheckpoint(ctx, "%s - started".formatted(rule.header()));
final OutputTracker<String> tracker = ctx.startTracking();
final List<CheckFailure> failures = new ArrayList<>(1);
Expand All @@ -72,7 +91,7 @@ private static TestResult performAndLog(
failures.add(new CheckFailure(check.asString(), check.result()));
}
} else {
CheckFile.perform(fragment, ctx, locator);
this.perform(fragment, ctx);
}
}
logCheckpoint(ctx, "%s - %s".formatted(rule.header(), CheckFile.desc(failures)));
Expand All @@ -84,17 +103,16 @@ private static TestResult performAndLog(
);
}

private static void perform(
final RuleFragment fragment,
final ComputationContext ctx,
final String locator
) {
private void perform(final RuleFragment fragment, final ComputationContext ctx) {
switch (fragment.type()) {
case "ASG" -> new Assignment(fragment.left(), fragment.right()).performIn(ctx);
case "OUT" -> {
if ("execute".equals(fragment.left())) {
case "EXE" -> {
if ("command".equals(fragment.left())) {
ctx.perform(fragment.right());
ctx.resetComputationState(locator);
ctx.resetComputationState(this.request);
}
if ("include".equals(fragment.left())) {
this.suite.findAndPerform(fragment.right(), ctx);
}
}
default -> {
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/ru/ewc/checklogic/testing/CheckSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.Collection;
import java.util.List;
import ru.ewc.decisions.api.ComputationContext;
import ru.ewc.decisions.input.ContentsReader;

/**
Expand All @@ -41,22 +42,39 @@ public final class CheckSuite {
*/
private final Collection<CheckFile> tests;

private CheckSuite(final Collection<CheckFile> tests) {
/**
* The root directory of the business logic source files.
*/
private final String root;

private CheckSuite(final Collection<CheckFile> tests, final String root) {
this.tests = tests;
this.root = root;
}

public static CheckSuite using(final ContentsReader reader) {
return new CheckSuite(reader.readAll().stream()
.map(sl -> new CheckFile(sl.specifiedRulesFragments()))
.toList()
public static CheckSuite using(
final ContentsReader reader,
final String root,
final String request
) {
return new CheckSuite(
reader.readAll().stream()
.map(sl -> new CheckFile(sl.fileName(), sl.specifiedRulesFragments(), request))
.toList(),
root
);
}

public List<TestResult> perform(final String root, final String locator) {
public List<TestResult> perform() {
return this.tests.stream()
.map(test -> test.performChecks(root, locator))
.map(test -> test.performChecks(this.root, this))
.flatMap(List::stream)
.toList();
}

public void findAndPerform(final String file, final ComputationContext ctx) {
this.tests.stream().filter(test -> file.equals(test.getFile()))
.findFirst()
.ifPresent(test -> test.performInSameContext(ctx));
}
}
2 changes: 1 addition & 1 deletion src/test/resources/tic-tac-toe/tests/00-initialize.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OUT;execute;initialize
EXE;command;initialize
CND;table::currentPlayer;X
CND;table::nextPlayer;O
CND;cells::A1;empty
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/tic-tac-toe/tests/01-first-move.csv
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ASG;cells::B3;empty;empty
ASG;cells::C1;empty;empty
ASG;cells::C2;empty;empty
ASG;cells::C3;empty;empty
OUT;execute;computed_move;computed_move
EXE;command;computed_move;computed_move
CND;cells::A1;empty;X
CND;computed_move::available;false;false
CND;game_state::is_over;false;false
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/tic-tac-toe/tests/02-second-move.csv
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ASG;cells::B3;empty;empty
ASG;cells::C1;empty;empty
ASG;cells::C2;empty;empty
ASG;cells::C3;empty;empty
OUT;execute;computed_move;computed_move
EXE;command;computed_move;computed_move
CND;computed_move::available;false;false
CND;cells::A1;X;X
CND;cells::B2;O;empty

0 comments on commit 609878c

Please sign in to comment.