Skip to content

Commit

Permalink
feat(test): visual enhancements for test results page
Browse files Browse the repository at this point in the history
Closes: #39
  • Loading branch information
nergal-perm committed Aug 1, 2024
1 parent d2bff97 commit a4d75a7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/main/java/ru/ewc/checklogic/FullServerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ public void perform(final String command, final Map<String, String> args) {
try {
this.context.perform(command);
this.context = new ComputationContext(this.state, this.tables, this.commands);
} catch (NullPointerException exception) {
throw new DecitaException("Command file for '%s' not found".formatted(command));
} catch (Throwable exception) {
throw new IllegalStateException(
"Command file for '%s' not found".formatted(command),
exception
);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ru/ewc/checklogic/TestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public String result() {

public String asHtmlTableRow() {
return String.format(
"<tr><td>%s</td><td>%s</td><td>%s</td></tr>",
"<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td></tr>",
this.successful ? "table-success" : "table-danger",
this.file,
this.result(),
this.error
this.error.replace("\n", "<br>").replace(" ", "&nbsp;&nbsp;&nbsp;&nbsp;")
);
}
}
23 changes: 20 additions & 3 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.renomad.minum.web.Response;
import java.io.File;
import java.nio.file.Files;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -70,12 +72,28 @@ public Response noTestsFolder() {
}

public Response testPage() {
final String results = FileUtils.readFileNames(this.root)
final long start = System.currentTimeMillis();
final List<TestResult> testResults = FileUtils.readFileNames(this.root)
.map(this::performTest)
.toList();
final String results = testResults.stream()
.sorted(Comparator.comparing(TestResult::result))
.map(TestResult::asHtmlTableRow)
.collect(Collectors.joining());
final double executionTime = (System.currentTimeMillis() - start) / 1000.0;
return Response.htmlOk(
this.templateNamed("templates/test.html", Map.of("tests", "%s".formatted(results)))
this.templateNamed(
"templates/test.html",
Map.of(
"tests", "%s".formatted(results),
"stats", "%d test(s) performed in %.3f second(s), %d passed, %d failed".formatted(
testResults.size(),
executionTime,
testResults.stream().filter(TestResult::successful).count(),
testResults.stream().filter(result -> !result.successful()).count()
)
)
)
);
}

Expand Down Expand Up @@ -124,7 +142,6 @@ private TestResult performTest(final TestData test) {
} catch (IllegalStateException exception) {
result = new TestResult(test.toString(), false, exception.getMessage());
}

return result;
}

Expand Down
19 changes: 12 additions & 7 deletions src/main/resources/templates/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@
</head>
<body class="container">
<h1>Test results</h1>
<table>
<tr>
<th>Test</th>
<th>Result</th>
<th>Message</th>
</tr>
{{ tests }}
<table class="table caption-top">
<caption>{{ stats }}</caption>
<thead>
<tr>
<th scope="col">Test</th>
<th scope="col">Result</th>
<th scope="col">Message</th>
</tr>
</thead>
<tbody>
{{ tests }}
</tbody>
</table>
</body>
</html>

0 comments on commit a4d75a7

Please sign in to comment.