Skip to content

Commit aca9fd0

Browse files
committed
Add ability to make eager test fixtures pretty
1 parent c3b56b8 commit aca9fd0

1 file changed

Lines changed: 51 additions & 11 deletions

File tree

src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ExpectedTemplateInterpreter {
1515
private Jinjava jinjava;
1616
private JinjavaInterpreter interpreter;
1717
private String path;
18+
private boolean sensibleCurrentPath = false;
1819

1920
public ExpectedTemplateInterpreter(
2021
Jinjava jinjava,
@@ -26,15 +27,35 @@ public ExpectedTemplateInterpreter(
2627
this.path = path;
2728
}
2829

30+
public static ExpectedTemplateInterpreter withSensibleCurrentPath(
31+
Jinjava jinjava,
32+
JinjavaInterpreter interpreter,
33+
String path
34+
) {
35+
return new ExpectedTemplateInterpreter(jinjava, interpreter, path, true);
36+
}
37+
38+
private ExpectedTemplateInterpreter(
39+
Jinjava jinjava,
40+
JinjavaInterpreter interpreter,
41+
String path,
42+
boolean sensibleCurrentPath
43+
) {
44+
this.jinjava = jinjava;
45+
this.interpreter = interpreter;
46+
this.path = path;
47+
this.sensibleCurrentPath = sensibleCurrentPath;
48+
}
49+
2950
public String assertExpectedOutput(String name) {
3051
String template = getFixtureTemplate(name);
3152
String output = JinjavaInterpreter.getCurrent().render(template);
3253
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
3354
.as("Ensure no deferred nodes were created")
3455
.isEmpty();
35-
assertThat(output.trim()).isEqualTo(expected(name).trim());
36-
assertThat(JinjavaInterpreter.getCurrent().render(output).trim())
37-
.isEqualTo(expected(name).trim());
56+
assertThat(prettify(output.trim())).isEqualTo(prettify(expected(name).trim()));
57+
assertThat(prettify(JinjavaInterpreter.getCurrent().render(output).trim()))
58+
.isEqualTo(prettify(expected(name).trim()));
3859
return output;
3960
}
4061

@@ -44,7 +65,7 @@ public String assertExpectedOutputNonIdempotent(String name) {
4465
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
4566
.as("Ensure no deferred nodes were created")
4667
.isEmpty();
47-
assertThat(output.trim()).isEqualTo(expected(name).trim());
68+
assertThat(prettify(output.trim())).isEqualTo(prettify(expected(name).trim()));
4869
return output;
4970
}
5071

@@ -105,7 +126,7 @@ public String assertExpectedNonEagerOutput(String name) {
105126
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
106127
.as("Ensure no deferred nodes were created")
107128
.isEmpty();
108-
assertThat(output.trim()).isEqualTo(expected(name).trim());
129+
assertThat(prettify(output.trim())).isEqualTo(prettify(expected(name).trim()));
109130
}
110131
} finally {
111132
JinjavaInterpreter.popCurrent();
@@ -114,11 +135,24 @@ public String assertExpectedNonEagerOutput(String name) {
114135
return output;
115136
}
116137

138+
private String prettify(String string) {
139+
return string.replaceAll("([}%]})([^\\s])", "$1\\\\\n$2");
140+
}
141+
117142
public String getFixtureTemplate(String name) {
118143
try {
119-
return Resources.toString(
120-
Resources.getResource(String.format("%s/%s.jinja", path, name)),
121-
StandardCharsets.UTF_8
144+
if (sensibleCurrentPath) {
145+
JinjavaInterpreter
146+
.getCurrent()
147+
.getContext()
148+
.getCurrentPathStack()
149+
.push(String.format("%s/%s.jinja", path, name), 0, 0);
150+
}
151+
return simplify(
152+
Resources.toString(
153+
Resources.getResource(String.format("%s/%s.jinja", path, name)),
154+
StandardCharsets.UTF_8
155+
)
122156
);
123157
} catch (IOException e) {
124158
throw new RuntimeException(e);
@@ -127,15 +161,21 @@ public String getFixtureTemplate(String name) {
127161

128162
private String expected(String name) {
129163
try {
130-
return Resources.toString(
131-
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
132-
StandardCharsets.UTF_8
164+
return simplify(
165+
Resources.toString(
166+
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
167+
StandardCharsets.UTF_8
168+
)
133169
);
134170
} catch (IOException e) {
135171
throw new RuntimeException(e);
136172
}
137173
}
138174

175+
private String simplify(String prettified) {
176+
return prettified.replaceAll("\\\\\n\\s*", "");
177+
}
178+
139179
public String getDeferredFixtureTemplate(String templateLocation) {
140180
try {
141181
return Resources.toString(

0 commit comments

Comments
 (0)