-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndexTemplateTest.java
81 lines (72 loc) · 2.78 KB
/
IndexTemplateTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.example;
import com.samskivert.mustache.Mustache;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.InputStreamReader;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class IndexTemplateTest {
record TestCase(String name,
TodoList model,
String selector,
List<String> matches) {
@Override
public String toString() {
return name;
}
}
public static TestCase[] indexTestCases() {
return new TestCase[]{
new TestCase(
"all todo items are shown",
new TodoList()
.add("Foo")
.add("Bar"),
"ul.todo-list li",
List.of("Foo", "Bar")),
new TestCase(
"completed items get the 'completed' class",
new TodoList()
.add("Foo")
.addCompleted("Bar"),
"ul.todo-list li.completed",
List.of("Bar")),
new TestCase(
"items left",
new TodoList()
.add("One")
.add("Two")
.addCompleted("Three"),
"span.todo-count",
List.of("2 items left")),
};
}
@ParameterizedTest
@MethodSource("indexTestCases")
void testIndexTemplate(TestCase test) {
var html = renderTemplate("/index.tmpl", test.model);
var document = parseHtml(html);
var selection = document.select(test.selector);
assertThat(selection).hasSize(test.matches.size());
for (int i = 0; i < test.matches.size(); i++) {
assertThat(selection.get(i).text()).isEqualTo(test.matches.get(i));
}
}
// thanks https://stackoverflow.com/a/64465867/164802
private static Document parseHtml(String html) {
var parser = Parser.htmlParser().setTrackErrors(10);
var document = Jsoup.parse(html, "", parser);
assertThat(parser.getErrors()).isEmpty();
return document;
}
@SuppressWarnings({"DataFlowIssue", "SameParameterValue"})
private String renderTemplate(String templateName, Object model) {
var template = Mustache.compiler().compile(
new InputStreamReader(
getClass().getResourceAsStream(templateName)));
return template.execute(model);
}
}