Skip to content

Commit 626d405

Browse files
committed
feat(test): line-by-line execution of tests and request reset in-between commands
Closes: #63
1 parent bd5eec0 commit 626d405

11 files changed

+46
-181
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<dependency>
114114
<groupId>io.github.nergal-perm</groupId>
115115
<artifactId>java-decita</artifactId>
116-
<version>0.9.0-SNAPSHOT</version>
116+
<version>0.9.0</version>
117117
</dependency>
118118
<dependency>
119119
<groupId>com.renomad</groupId>

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.stream.Collectors;
3737
import lombok.SneakyThrows;
3838
import org.yaml.snakeyaml.Yaml;
39+
import ru.ewc.decisions.api.InMemoryLocator;
3940
import ru.ewc.decisions.api.Locator;
4041
import ru.ewc.state.State;
4142

@@ -96,7 +97,7 @@ private Map<String, Locator> locatorsFromFile() {
9697
"There is no Arrange section in the test file, you should add one"
9798
);
9899
}
99-
raw.forEach((name, data) -> locators.put(name, new InMemoryStorage(name, data)));
100+
raw.forEach((name, data) -> locators.put(name, new InMemoryLocator(name, data)));
100101
}
101102
return locators;
102103
}
@@ -106,7 +107,7 @@ private Map<String, Locator> locatorsFromFile() {
106107
private static State stateFromAppConfig(final InputStream file) {
107108
final Map<String, Object> config = new Yaml().load(file);
108109
return new State(((List<String>) config.get("locators")).stream()
109-
.map(name -> new InMemoryStorage(name, new HashMap<>()))
110+
.map(name -> new InMemoryLocator(name, new HashMap<>()))
110111
.collect(Collectors.toList())
111112
);
112113
}

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

-93
This file was deleted.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void main(final String[] args) {
5757
registerEndpoints(web, new ConfigPage(factory.configuration()));
5858
registerEndpoints(web, new CommandPage(context));
5959
registerEndpoints(web, new ContextPage(context, factory.configuration()));
60-
registerEndpoints(web, new AllEndpoints(context));
60+
registerEndpoints(web, new AllEndpoints(context, factory.configuration()));
6161
minum.block();
6262
}
6363

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.InputStream;
2727
import java.util.List;
2828
import java.util.Map;
29+
import ru.ewc.decisions.api.InMemoryLocator;
2930
import ru.ewc.state.State;
3031

3132
/**
@@ -42,8 +43,8 @@ public MockStateFactory(final String root) {
4243
public State initialState() {
4344
return new State(
4445
List.of(
45-
new InMemoryStorage("locator", Map.of("fragment", "value")),
46-
new InMemoryStorage("request", Map.of())
46+
new InMemoryLocator("locator", Map.of("fragment", "value")),
47+
new InMemoryLocator("request", Map.of())
4748
)
4849
);
4950
}

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import ru.ewc.decisions.api.ComputationContext;
3232
import ru.ewc.decisions.api.DecisionTables;
3333
import ru.ewc.decisions.api.DecitaException;
34+
import ru.ewc.decisions.api.InMemoryLocator;
3435
import ru.ewc.decisions.input.CombinedCsvFileReader;
3536
import ru.ewc.state.State;
3637

@@ -129,10 +130,13 @@ public boolean isAvailable(final String command, final String field) {
129130
}
130131

131132
public void update(final List<String> values) {
132-
this.state.locators().put(
133-
this.server.requestLocatorName(),
134-
InMemoryStorage.from(this.server.requestLocatorName(), values)
135-
);
133+
final InMemoryLocator request = InMemoryLocator.empty(requestLocatorName());
134+
values.forEach(
135+
value -> {
136+
final String[] split = value.split(":");
137+
request.setFragmentValue(split[0].trim(), split[1].trim());
138+
});
139+
this.state.locators().put(this.server.requestLocatorName(), request);
136140
this.context = new ComputationContext(this.state, this.getAllTables());
137141
}
138142

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.renomad.minum.web.Response;
2828
import com.renomad.minum.web.WebFramework;
2929
import java.util.Map;
30+
import ru.ewc.checklogic.ServerConfiguration;
3031
import ru.ewc.checklogic.ServerInstance;
3132

3233
/**
@@ -45,9 +46,9 @@ public final class AllEndpoints implements Endpoints {
4546
*/
4647
private final WebPages pages;
4748

48-
public AllEndpoints(final ServerInstance context) {
49+
public AllEndpoints(final ServerInstance context, ServerConfiguration configuration) {
4950
this.context = context;
50-
this.pages = WebPages.create(new ResourceTemplateRender(), context.getRoot());
51+
this.pages = WebPages.create(new ResourceTemplateRender(), context.getRoot(), configuration);
5152
}
5253

5354
@Override

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.stream.Collectors;
32+
import ru.ewc.checklogic.ServerConfiguration;
3233
import ru.ewc.checklogic.ServerContextFactory;
3334
import ru.ewc.checklogic.ServerInstance;
3435
import ru.ewc.checklogic.testing.CheckSuite;
@@ -53,17 +54,20 @@ public final class WebPages {
5354
*/
5455
private final String root;
5556

56-
private WebPages(final TemplateRender processors, final String root) {
57+
private final ServerConfiguration config;
58+
59+
private WebPages(final TemplateRender processors, final String root, ServerConfiguration config) {
5760
this.processors = processors;
5861
this.root = root;
62+
this.config = config;
5963
}
6064

61-
public static WebPages create(final TemplateRender processors, final String root) {
62-
return new WebPages(processors, root);
65+
public static WebPages create(final TemplateRender processors, final String root, ServerConfiguration configuration) {
66+
return new WebPages(processors, root, configuration);
6367
}
6468

6569
public static WebPages testable() {
66-
return new WebPages(new MockTemplateRender(), "root folder");
70+
return new WebPages(new MockTemplateRender(), "root folder", new ServerConfiguration());
6771
}
6872

6973
public Response uninitializedPage() {
@@ -76,7 +80,7 @@ public Response testPage() {
7680
);
7781
final long start = System.currentTimeMillis();
7882
final ComputationContext context = ServerContextFactory.create(this.root).context();
79-
final List<TestResult> results = suite.perform(context);
83+
final List<TestResult> results = suite.perform(context, this.config.requestLocatorName());
8084
final String rows = results.stream()
8185
.sorted(Comparator.naturalOrder())
8286
.map(TestResult::asHtmlTableRow)

src/main/java/ru/ewc/checklogic/testing/CheckRuleFragments.java renamed to src/main/java/ru/ewc/checklogic/testing/CheckFile.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@
3333
import ru.ewc.decisions.commands.Assignment;
3434
import ru.ewc.decisions.conditions.Condition;
3535

36-
public class CheckRuleFragments {
36+
public class CheckFile {
3737
private final List<RuleFragments> rules;
3838

39-
public CheckRuleFragments(List<RuleFragments> rules) {
39+
public CheckFile(List<RuleFragments> rules) {
4040
this.rules = rules;
4141
}
4242

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

49-
private static TestResult performAndLog(final ComputationContext ctx, final RuleFragments rule) {
49+
private static TestResult performAndLog(final ComputationContext ctx, final RuleFragments rule, String locator) {
5050
logCheckpoint(ctx, "%s - started".formatted(rule.header()));
5151
final OutputTracker<String> tracker = ctx.startTracking();
5252
List<CheckFailure> failures = new ArrayList<>(1);
@@ -57,10 +57,10 @@ private static TestResult performAndLog(final ComputationContext ctx, final Rule
5757
failures.add(new CheckFailure(check.asString(), check.result()));
5858
}
5959
} else {
60-
CheckRuleFragments.perform(fragment, ctx);
60+
CheckFile.perform(fragment, ctx, locator);
6161
}
6262
}
63-
logCheckpoint(ctx, "%s - %s".formatted(rule.header(), CheckRuleFragments.desc(failures)));
63+
logCheckpoint(ctx, "%s - %s".formatted(rule.header(), CheckFile.desc(failures)));
6464
return new TestResult(
6565
rule.header(),
6666
failures.isEmpty(),
@@ -69,19 +69,18 @@ private static TestResult performAndLog(final ComputationContext ctx, final Rule
6969
);
7070
}
7171

72-
private static void perform(RuleFragment fragment, ComputationContext ctx) {
72+
private static void perform(RuleFragment fragment, ComputationContext ctx, String locator) {
7373
switch (fragment.type()) {
7474
case "ASG" -> new Assignment(fragment.left(), fragment.right()).performIn(ctx);
7575
case "OUT" -> {
7676
if ("execute".equals(fragment.left())) {
7777
ctx.perform(fragment.right());
78+
ctx.resetComputationState(locator);
7879
}
7980
}
80-
case "HDR" -> {
81-
// do nothing
81+
default -> {
82+
// do nothing, because we don't know what to do with such a fragment type
8283
}
83-
default ->
84-
throw new IllegalArgumentException("Unknown fragment type: %s".formatted(fragment.type()));
8584
}
8685
}
8786

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

106105
private static String checkFailureAsHtml(final List<CheckFailure> failures) {
107106
return failures.stream()
108-
.map(CheckRuleFragments::formattedDescriptionFor)
107+
.map(CheckFile::formattedDescriptionFor)
109108
.collect(Collectors.joining());
110109
}
111110

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

-52
This file was deleted.

0 commit comments

Comments
 (0)