forked from spullara/mustache.java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecTest.java
More file actions
197 lines (180 loc) · 5.71 KB
/
SpecTest.java
File metadata and controls
197 lines (180 loc) · 5.71 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.github.mustachejava;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import static junit.framework.Assert.assertFalse;
/**
* Specification tests
*/
public class SpecTest {
@Test
public void interpolations() throws IOException {
run(getSpec("interpolation.yml"));
}
@Test
public void sections() throws IOException {
run(getSpec("sections.yml"));
}
@Test
public void delimiters() throws IOException {
run(getSpec("delimiters.yml"));
}
@Test
public void inverted() throws IOException {
run(getSpec("inverted.yml"));
}
@Test
public void partials() throws IOException {
run(getSpec("partials.yml"));
}
@Test
public void lambdas() throws IOException {
run(getSpec("~lambdas.yml"));
}
// @Test — need this to appear in the spec repository to enable
public void inheritance() throws IOException {
run(getSpec("inheritance.yml"));
}
private void run(JsonNode spec) {
int fail = 0;
int success = 0;
int whitespace = 0;
Map<String, Object> functionMap = new HashMap<String, Object>() {{
put("Interpolation", new Object() {
Function lambda() {
return input -> "world";
}
});
put("Interpolation - Expansion", new Object() {
Function lambda() {
return input -> "{{planet}}";
}
});
put("Interpolation - Alternate Delimiters", new Object() {
Function lambda() {
return input -> "|planet| => {{planet}}";
}
});
put("Interpolation - Multiple Calls", new Object() {
int calls = 0;
Function lambda() {
return input -> String.valueOf(++calls);
}
});
put("Escaping", new Object() {
Function lambda() {
return input -> ">";
}
});
put("Section", new Object() {
Function lambda() {
return new TemplateFunction() {
@Override
public String apply(String input) {
return input.equals("{{x}}") ? "yes" : "no";
}
};
}
});
put("Section - Expansion", new Object() {
Function lambda() {
return new TemplateFunction() {
@Override
public String apply(String input) {
return input + "{{planet}}" + input;
}
};
}
});
put("Section - Alternate Delimiters", new Object() {
Function lambda() {
return new TemplateFunction() {
@Override
public String apply(String input) {
return input + "{{planet}} => |planet|" + input;
}
};
}
});
put("Section - Multiple Calls", new Object() {
Function lambda() {
return new Function<String, String>() {
@Override
public String apply(String input) {
return "__" + input + "__";
}
};
}
});
put("Inverted Section", new Object() {
Function lambda() {
return input -> false;
}
});
}};
for (final JsonNode test : spec.get("tests")) {
boolean failed = false;
final DefaultMustacheFactory CF = createMustacheFactory(test);
String file = test.get("name").asText();
System.out.print("Running " + file + " - " + test.get("desc").asText());
StringReader template = new StringReader(test.get("template").asText());
JsonNode data = test.get("data");
try {
Mustache compile = CF.compile(template, file);
StringWriter writer = new StringWriter();
compile.execute(writer, new Object[]{new ObjectMapper().readValue(data.toString(), Map.class), functionMap.get(file)});
String expected = test.get("expected").asText();
if (transformOutput(writer.toString()).equals(transformOutput(expected))) {
System.out.print(": success");
if (writer.toString().equals(expected)) {
System.out.println("!");
} else {
whitespace++;
System.out.println(", whitespace differences.");
}
} else {
System.out.println(": failed!");
System.out.println(expected + " != " + writer.toString());
System.out.println(test);
failed = true;
}
} catch (Throwable e) {
System.out.println(": exception");
e.printStackTrace();
System.out.println(test);
failed = true;
}
if (failed) fail++;
else success++;
}
System.out.println("Success: " + success + " Whitespace: " + whitespace + " Fail: " + fail);
assertFalse(fail > 0);
}
protected String transformOutput(String output) {
return output.replaceAll("\\s+", "");
}
protected DefaultMustacheFactory createMustacheFactory(final JsonNode test) {
return new DefaultMustacheFactory("/spec/specs") {
@Override
public Reader getReader(String resourceName) {
JsonNode partial = test.get("partials").get(resourceName);
return new StringReader(partial == null ? "" : partial.asText());
}
};
}
private JsonNode getSpec(String spec) throws IOException {
return new YAMLFactory(new YAMLMapper()).createParser(new InputStreamReader(
SpecTest.class.getResourceAsStream(
"/spec/specs/" + spec))).readValueAsTree();
}
}