Skip to content

Commit

Permalink
feat(test): line-by-line execution of tests and request reset in-betw…
Browse files Browse the repository at this point in the history
…een commands

Closes: #63
  • Loading branch information
nergal-perm committed Sep 12, 2024
1 parent bd5eec0 commit 626d405
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 181 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<dependency>
<groupId>io.github.nergal-perm</groupId>
<artifactId>java-decita</artifactId>
<version>0.9.0-SNAPSHOT</version>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>com.renomad</groupId>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ru/ewc/checklogic/FileStateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.yaml.snakeyaml.Yaml;
import ru.ewc.decisions.api.InMemoryLocator;
import ru.ewc.decisions.api.Locator;
import ru.ewc.state.State;

Expand Down Expand Up @@ -96,7 +97,7 @@ private Map<String, Locator> locatorsFromFile() {
"There is no Arrange section in the test file, you should add one"
);
}
raw.forEach((name, data) -> locators.put(name, new InMemoryStorage(name, data)));
raw.forEach((name, data) -> locators.put(name, new InMemoryLocator(name, data)));
}
return locators;
}
Expand All @@ -106,7 +107,7 @@ private Map<String, Locator> locatorsFromFile() {
private static State stateFromAppConfig(final InputStream file) {
final Map<String, Object> config = new Yaml().load(file);
return new State(((List<String>) config.get("locators")).stream()
.map(name -> new InMemoryStorage(name, new HashMap<>()))
.map(name -> new InMemoryLocator(name, new HashMap<>()))
.collect(Collectors.toList())
);
}
Expand Down
93 changes: 0 additions & 93 deletions src/main/java/ru/ewc/checklogic/InMemoryStorage.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/ru/ewc/checklogic/LogicChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void main(final String[] args) {
registerEndpoints(web, new ConfigPage(factory.configuration()));
registerEndpoints(web, new CommandPage(context));
registerEndpoints(web, new ContextPage(context, factory.configuration()));
registerEndpoints(web, new AllEndpoints(context));
registerEndpoints(web, new AllEndpoints(context, factory.configuration()));
minum.block();
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ru/ewc/checklogic/MockStateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import ru.ewc.decisions.api.InMemoryLocator;
import ru.ewc.state.State;

/**
Expand All @@ -42,8 +43,8 @@ public MockStateFactory(final String root) {
public State initialState() {
return new State(
List.of(
new InMemoryStorage("locator", Map.of("fragment", "value")),
new InMemoryStorage("request", Map.of())
new InMemoryLocator("locator", Map.of("fragment", "value")),
new InMemoryLocator("request", Map.of())
)
);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/ru/ewc/checklogic/ServerInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import ru.ewc.decisions.api.ComputationContext;
import ru.ewc.decisions.api.DecisionTables;
import ru.ewc.decisions.api.DecitaException;
import ru.ewc.decisions.api.InMemoryLocator;
import ru.ewc.decisions.input.CombinedCsvFileReader;
import ru.ewc.state.State;

Expand Down Expand Up @@ -129,10 +130,13 @@ public boolean isAvailable(final String command, final String field) {
}

public void update(final List<String> values) {
this.state.locators().put(
this.server.requestLocatorName(),
InMemoryStorage.from(this.server.requestLocatorName(), values)
);
final InMemoryLocator request = InMemoryLocator.empty(requestLocatorName());
values.forEach(
value -> {
final String[] split = value.split(":");
request.setFragmentValue(split[0].trim(), split[1].trim());
});
this.state.locators().put(this.server.requestLocatorName(), request);
this.context = new ComputationContext(this.state, this.getAllTables());
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ru/ewc/checklogic/server/AllEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.renomad.minum.web.Response;
import com.renomad.minum.web.WebFramework;
import java.util.Map;
import ru.ewc.checklogic.ServerConfiguration;
import ru.ewc.checklogic.ServerInstance;

/**
Expand All @@ -45,9 +46,9 @@ public final class AllEndpoints implements Endpoints {
*/
private final WebPages pages;

public AllEndpoints(final ServerInstance context) {
public AllEndpoints(final ServerInstance context, ServerConfiguration configuration) {
this.context = context;
this.pages = WebPages.create(new ResourceTemplateRender(), context.getRoot());
this.pages = WebPages.create(new ResourceTemplateRender(), context.getRoot(), configuration);
}

@Override
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import ru.ewc.checklogic.ServerConfiguration;
import ru.ewc.checklogic.ServerContextFactory;
import ru.ewc.checklogic.ServerInstance;
import ru.ewc.checklogic.testing.CheckSuite;
Expand All @@ -53,17 +54,20 @@ public final class WebPages {
*/
private final String root;

private WebPages(final TemplateRender processors, final String root) {
private final ServerConfiguration config;

private WebPages(final TemplateRender processors, final String root, ServerConfiguration config) {
this.processors = processors;
this.root = root;
this.config = config;
}

public static WebPages create(final TemplateRender processors, final String root) {
return new WebPages(processors, root);
public static WebPages create(final TemplateRender processors, final String root, ServerConfiguration configuration) {
return new WebPages(processors, root, configuration);
}

public static WebPages testable() {
return new WebPages(new MockTemplateRender(), "root folder");
return new WebPages(new MockTemplateRender(), "root folder", new ServerConfiguration());
}

public Response uninitializedPage() {
Expand All @@ -76,7 +80,7 @@ public Response testPage() {
);
final long start = System.currentTimeMillis();
final ComputationContext context = ServerContextFactory.create(this.root).context();
final List<TestResult> results = suite.perform(context);
final List<TestResult> results = suite.perform(context, this.config.requestLocatorName());
final String rows = results.stream()
.sorted(Comparator.naturalOrder())
.map(TestResult::asHtmlTableRow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@
import ru.ewc.decisions.commands.Assignment;
import ru.ewc.decisions.conditions.Condition;

public class CheckRuleFragments {
public class CheckFile {
private final List<RuleFragments> rules;

public CheckRuleFragments(List<RuleFragments> rules) {
public CheckFile(List<RuleFragments> rules) {
this.rules = rules;
}

public List<TestResult> perform(ComputationContext context) {
public List<TestResult> performChecks(ComputationContext context, String locator) {
return this.rules.stream()
.map(rule -> CheckRuleFragments.performAndLog(context, rule))
.map(rule -> CheckFile.performAndLog(context, rule, locator))
.toList();
}

private static TestResult performAndLog(final ComputationContext ctx, final RuleFragments rule) {
private static TestResult performAndLog(final ComputationContext ctx, final RuleFragments rule, String locator) {
logCheckpoint(ctx, "%s - started".formatted(rule.header()));
final OutputTracker<String> tracker = ctx.startTracking();
List<CheckFailure> failures = new ArrayList<>(1);
Expand All @@ -57,10 +57,10 @@ private static TestResult performAndLog(final ComputationContext ctx, final Rule
failures.add(new CheckFailure(check.asString(), check.result()));
}
} else {
CheckRuleFragments.perform(fragment, ctx);
CheckFile.perform(fragment, ctx, locator);
}
}
logCheckpoint(ctx, "%s - %s".formatted(rule.header(), CheckRuleFragments.desc(failures)));
logCheckpoint(ctx, "%s - %s".formatted(rule.header(), CheckFile.desc(failures)));
return new TestResult(
rule.header(),
failures.isEmpty(),
Expand All @@ -69,19 +69,18 @@ private static TestResult performAndLog(final ComputationContext ctx, final Rule
);
}

private static void perform(RuleFragment fragment, ComputationContext ctx) {
private static void perform(RuleFragment fragment, ComputationContext ctx, String locator) {
switch (fragment.type()) {
case "ASG" -> new Assignment(fragment.left(), fragment.right()).performIn(ctx);
case "OUT" -> {
if ("execute".equals(fragment.left())) {
ctx.perform(fragment.right());
ctx.resetComputationState(locator);
}
}
case "HDR" -> {
// do nothing
default -> {
// do nothing, because we don't know what to do with such a fragment type
}
default ->
throw new IllegalArgumentException("Unknown fragment type: %s".formatted(fragment.type()));
}
}

Expand All @@ -105,7 +104,7 @@ private static String resultAsUnorderedList(final List<CheckFailure> failures) {

private static String checkFailureAsHtml(final List<CheckFailure> failures) {
return failures.stream()
.map(CheckRuleFragments::formattedDescriptionFor)
.map(CheckFile::formattedDescriptionFor)
.collect(Collectors.joining());
}

Expand Down
52 changes: 0 additions & 52 deletions src/main/java/ru/ewc/checklogic/testing/CheckInstance.java

This file was deleted.

Loading

0 comments on commit 626d405

Please sign in to comment.