Skip to content

Commit

Permalink
fix(test): running a test without Arrange section outputs informative…
Browse files Browse the repository at this point in the history
… fail reason

Closes: #53
  • Loading branch information
nergal-perm committed Jul 31, 2024
1 parent 807d016 commit 5615348
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/main/java/ru/ewc/checklogic/StateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ private Map<String, Locator> locatorsFromFile() {
if (this.src != null) {
final Map<String, Map<String, Object>> raw =
(Map<String, Map<String, Object>>) new Yaml().loadAll(this.src).iterator().next();
if (raw == null) {
throw new IllegalStateException(
"There is no Arrange section in the test file, you should add one"
);
}
raw.forEach((name, data) -> locators.put(name, new InMemoryStorage(data)));
}
return locators;
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,30 @@ private String templateNamed(final String template, final Map<String, String> va
@SneakyThrows
private TestResult performTest(final TestData test) {
final SoftAssertions softly = new SoftAssertions();
final FullServerContext target = new ServerContextFactory(this.root)
.fromStateFile(Files.newInputStream(new File(test.file()).toPath()));
TestResult result;
final FullServerContext target;
try {
if (!test.command().isEmpty()) {
target.perform(test.command());
target = new ServerContextFactory(this.root)
.fromStateFile(Files.newInputStream(new File(test.file()).toPath()));
try {
if (!test.command().isEmpty()) {
target.perform(test.command());
}
for (final String locator : test.expectations().keySet()) {
softly
.assertThat(target.stateFor(locator, test.expectations().get(locator)))
.describedAs(String.format("State for entity '%s'", locator))
.containsExactlyInAnyOrderEntriesOf(test.expectations().get(locator));
}
softly.assertAll();
result = new TestResult(test.toString(), true, "");
} catch (final AssertionError error) {
result = new TestResult(test.toString(), false, error.getMessage());
}
for (final String locator : test.expectations().keySet()) {
softly
.assertThat(target.stateFor(locator, test.expectations().get(locator)))
.describedAs(String.format("State for entity '%s'", locator))
.containsExactlyInAnyOrderEntriesOf(test.expectations().get(locator));
}
softly.assertAll();
result = new TestResult(test.toString(), true, "");
} catch (final AssertionError error) {
result = new TestResult(test.toString(), false, error.getMessage());
} catch (IllegalStateException exception) {
result = new TestResult(test.toString(), false, exception.getMessage());
}

return result;
}

Expand Down

0 comments on commit 5615348

Please sign in to comment.