Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fixture test templates "pretty" #1203

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/test/java/com/hubspot/jinjava/ExpectedNodeInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ public ExpectedNodeInterpreter(JinjavaInterpreter interpreter, Tag tag, String p
public String assertExpectedOutput(String name) {
TagNode tagNode = (TagNode) fixture(name);
String output = tag.interpret(tagNode, interpreter);
assertThat(output.trim()).isEqualTo(expected(name).trim());
assertThat(ExpectedTemplateInterpreter.prettify(output.trim()))
.isEqualTo(ExpectedTemplateInterpreter.prettify(expected(name).trim()));
return output;
}

public Node fixture(String name) {
try {
return new TreeParser(
interpreter,
Resources.toString(
Resources.getResource(String.format("%s/%s.jinja", path, name)),
StandardCharsets.UTF_8
ExpectedTemplateInterpreter.simplify(
Resources.toString(
Resources.getResource(String.format("%s/%s.jinja", path, name)),
StandardCharsets.UTF_8
)
)
)
.buildTree()
Expand All @@ -49,9 +52,11 @@ public Node fixture(String name) {

public String expected(String name) {
try {
return Resources.toString(
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
StandardCharsets.UTF_8
return ExpectedTemplateInterpreter.simplify(
Resources.toString(
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
StandardCharsets.UTF_8
)
);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
62 changes: 51 additions & 11 deletions src/test/java/com/hubspot/jinjava/ExpectedTemplateInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ExpectedTemplateInterpreter {
private Jinjava jinjava;
private JinjavaInterpreter interpreter;
private String path;
private boolean sensibleCurrentPath = false;

public ExpectedTemplateInterpreter(
Jinjava jinjava,
Expand All @@ -26,15 +27,35 @@ public ExpectedTemplateInterpreter(
this.path = path;
}

public static ExpectedTemplateInterpreter withSensibleCurrentPath(
Jinjava jinjava,
JinjavaInterpreter interpreter,
String path
) {
return new ExpectedTemplateInterpreter(jinjava, interpreter, path, true);
}

private ExpectedTemplateInterpreter(
Jinjava jinjava,
JinjavaInterpreter interpreter,
String path,
boolean sensibleCurrentPath
) {
this.jinjava = jinjava;
this.interpreter = interpreter;
this.path = path;
this.sensibleCurrentPath = sensibleCurrentPath;
}

public String assertExpectedOutput(String name) {
String template = getFixtureTemplate(name);
String output = JinjavaInterpreter.getCurrent().render(template);
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
.as("Ensure no deferred nodes were created")
.isEmpty();
assertThat(output.trim()).isEqualTo(expected(name).trim());
assertThat(JinjavaInterpreter.getCurrent().render(output).trim())
.isEqualTo(expected(name).trim());
assertThat(prettify(output.trim())).isEqualTo(prettify(expected(name).trim()));
assertThat(prettify(JinjavaInterpreter.getCurrent().render(output).trim()))
.isEqualTo(prettify(expected(name).trim()));
return output;
}

Expand All @@ -44,7 +65,7 @@ public String assertExpectedOutputNonIdempotent(String name) {
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
.as("Ensure no deferred nodes were created")
.isEmpty();
assertThat(output.trim()).isEqualTo(expected(name).trim());
assertThat(prettify(output.trim())).isEqualTo(prettify(expected(name).trim()));
return output;
}

Expand Down Expand Up @@ -105,7 +126,7 @@ public String assertExpectedNonEagerOutput(String name) {
assertThat(JinjavaInterpreter.getCurrent().getContext().getDeferredNodes())
.as("Ensure no deferred nodes were created")
.isEmpty();
assertThat(output.trim()).isEqualTo(expected(name).trim());
assertThat(prettify(output.trim())).isEqualTo(prettify(expected(name).trim()));
}
} finally {
JinjavaInterpreter.popCurrent();
Expand All @@ -114,11 +135,24 @@ public String assertExpectedNonEagerOutput(String name) {
return output;
}

static String prettify(String string) {
return string.replaceAll("([}%]})([^\\s])", "$1\\\\\n$2");
}

public String getFixtureTemplate(String name) {
try {
return Resources.toString(
Resources.getResource(String.format("%s/%s.jinja", path, name)),
StandardCharsets.UTF_8
if (sensibleCurrentPath) {
JinjavaInterpreter
.getCurrent()
.getContext()
.getCurrentPathStack()
.push(String.format("%s/%s.jinja", path, name), 0, 0);
}
return simplify(
Resources.toString(
Resources.getResource(String.format("%s/%s.jinja", path, name)),
StandardCharsets.UTF_8
)
);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -127,15 +161,21 @@ public String getFixtureTemplate(String name) {

private String expected(String name) {
try {
return Resources.toString(
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
StandardCharsets.UTF_8
return simplify(
Resources.toString(
Resources.getResource(String.format("%s/%s.expected.jinja", path, name)),
StandardCharsets.UTF_8
)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

static String simplify(String prettified) {
return prettified.replaceAll("\\\\\n\\s*", "");
}

public String getDeferredFixtureTemplate(String templateLocation) {
try {
return Resources.toString(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{% set a_list = ['a', 'b'] %}{% for __ignored__ in [0] %}
{% set b_list = a_list %}{% if deferred %}
{% set a_list = ['a', 'b'] %}\
{% for __ignored__ in [0] %}
{% set b_list = a_list %}\
{% if deferred %}
{% set b_list = [deferred] %}
{% endif %}{% do b_list.append(deferred ? 'B' : '') %}
B: {{ b_list }}.{% endfor %}{% do a_list.append(deferred ? 'A' : '') %}
A: {{ a_list }}.
{% endif %}\
{% do b_list.append(deferred ? 'B' : '') %}
B: {{ b_list }}\
.{% endfor %}\
{% do a_list.append(deferred ? 'A' : '') %}
A: {{ a_list }}\
.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
0
META
{% for meta in deferred %}
{{ meta }}{% endfor %}
{{ meta }}\
{% endfor %}
META
{% set meta = [] %}{% if deferred %}
{% set meta = [] %}\
{% if deferred %}
{% do meta.append(1) %}
{% endif %}
{{ meta }}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
{% do %}{% set current_path = 'filters.jinja' %}{% set __temp_import_alias_854547461__ = {} %}{% for __ignored__ in [0] %}
{% set bar = deferred %}{% do __temp_import_alias_854547461__.update({'bar': bar}) %}
{% do %}\
{% set current_path = 'filters.jinja' %}\
{% set __temp_import_alias_854547461__ = {} %}\
{% for __ignored__ in [0] %}
{% set bar = deferred %}\
{% do __temp_import_alias_854547461__.update({'bar': bar}) %}

{% set filters = {} %}{% do __temp_import_alias_854547461__.update({'filters': filters}) %}{% do filters.update(deferred) %}
{% do __temp_import_alias_854547461__.update({'bar': bar,'foo': 123,'import_resource_path': 'filters.jinja','filters': filters}) %}{% endfor %}{% set filters = __temp_import_alias_854547461__ %}{% set current_path = '' %}{% enddo %}
{% set filters = {} %}\
{% do __temp_import_alias_854547461__.update({'filters': filters}) %}\
{% do filters.update(deferred) %}
{% do __temp_import_alias_854547461__.update({'bar': bar,'foo': 123,'import_resource_path': 'filters.jinja','filters': filters}) %}\
{% endfor %}\
{% set filters = __temp_import_alias_854547461__ %}\
{% set current_path = '' %}\
{% enddo %}

{{ filters }}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{% do %}{% set list1 = ['b'] %}{% set list0 = ['a'] %}{% set list2 = ['c'] %}
{% do %}\
{% set list1 = ['b'] %}\
{% set list0 = ['a'] %}\
{% set list2 = ['c'] %}


{% set list2 = ['c'] %}{% do list2.append(deferred) %}
{% set list2 = ['c'] %}\
{% do list2.append(deferred) %}
{% unless deferred %}
{% set list0 = ['a', 'a'] %}
{% endunless %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% set my_list = [] %}{% for __ignored__ in [0] %}
{% set my_list = [] %}\
{% for __ignored__ in [0] %}
{% for j in deferred %}
{% do my_list.append(0) %}
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{% macro repeat(val) %}
{{ val }}
{{ caller() }}
{% endmacro %}{% call repeat(deferred) %}
{% endmacro %}\
{% call repeat(deferred) %}
macro 1
{% macro repeat(val) %}
{{ val }}
{{ caller() }}
{% endmacro %}{% call repeat(deferred + 1) %}
{% endmacro %}\
{% call repeat(deferred + 1) %}
macro2
{% endcall %}
{% endcall %}
3 changes: 2 additions & 1 deletion src/test/resources/eager/defers-caller.expected.jinja
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Jack says:
How do I get a {{ deferred }}?
How do I get a {{ deferred }}\
?
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
1
{% set bar = [1] %}{% set foo = '1' %}{% if deferred %}
{% set foo %}2{% do bar.append(2) %}{% endset %}
{% set bar = [1] %}\
{% set foo = '1' %}\
{% if deferred %}
{% set foo %}\
2{% do bar.append(2) %}\
{% endset %}
{% endif %}
Bar: {{ bar }}
Foo: {{ foo }}
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
{% for __ignored__ in [0] %}
{% macro render(content, query) %}{% if query %}{{ content.foo }}{% endif %}{% endmacro %}{% set content = {'foo': 'a'} %}{{ render(content, deferred) }}
{% macro render(content, query) %}\
{% if query %}\
{{ content.foo }}\
{% endif %}\
{% endmacro %}\
{% set content = {'foo': 'a'} %}\
{{ render(content, deferred) }}

{% macro render(content, query) %}{% if query %}{{ content.foo }}{% endif %}{% endmacro %}{% set content = {'foo': 'b'} %}{{ render(content, deferred) }}
{% macro render(content, query) %}\
{% if query %}\
{{ content.foo }}\
{% endif %}\
{% endmacro %}\
{% set content = {'foo': 'b'} %}\
{{ render(content, deferred) }}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Is ([]),
Macro: [10]
Is ([10]),{% set my_list = [10] %}{% macro macro_append(num) %}{% do my_list.append(num) %}Macro: {{ my_list }}{% endmacro %}{% do macro_append(deferred) %}
Is ({{ my_list }}),
{% macro macro_append(num) %}{% do my_list.append(num) %}Macro: {{ my_list }}{% endmacro %}{% print macro_append(deferred2) %}
Is ([10]),{% set my_list = [10] %}\
{% macro macro_append(num) %}\
{% do my_list.append(num) %}\
Macro: {{ my_list }}\
{% endmacro %}\
{% do macro_append(deferred) %}
Is ({{ my_list }}\
),
{% macro macro_append(num) %}\
{% do my_list.append(num) %}\
Macro: {{ my_list }}\
{% endmacro %}\
{% print macro_append(deferred2) %}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2
{% macro plus(foo, add) %}{{ foo + (filter:int.filter(add, ____int3rpr3t3r____)) }}{% endmacro %}{{ plus(deferred, 1.1) }}{% set deferred = deferred + 2 %}
{% macro plus(foo, add) %}{{ foo + (filter:int.filter(add, ____int3rpr3t3r____)) }}{% endmacro %}{{ plus(deferred, 3.1) }}
{% macro plus(foo, add) %}\
{{ foo + (filter:int.filter(add, ____int3rpr3t3r____)) }}\
{% endmacro %}\
{{ plus(deferred, 1.1) }}\
{% set deferred = deferred + 2 %}
{% macro plus(foo, add) %}\
{{ foo + (filter:int.filter(add, ____int3rpr3t3r____)) }}\
{% endmacro %}\
{{ plus(deferred, 3.1) }}
7 changes: 6 additions & 1 deletion src/test/resources/eager/defers-macro-in-for.expected.jinja
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{% set my_list = [] %}{% macro macro_append(num) %}{% do my_list.append(num) %}{{ my_list }}{% endmacro %}{% for item in filter:split.filter(macro_append(deferred), ____int3rpr3t3r____, ',', 2) %}
{% set my_list = [] %}\
{% macro macro_append(num) %}\
{% do my_list.append(num) %}\
{{ my_list }}\
{% endmacro %}\
{% for item in filter:split.filter(macro_append(deferred), ____int3rpr3t3r____, ',', 2) %}
{{ item }}
{% endfor %}
7 changes: 6 additions & 1 deletion src/test/resources/eager/defers-macro-in-if.expected.jinja
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{% set my_list = [] %}{% macro macro_append(num) %}{% do my_list.append(num) %}{{ my_list }}{% endmacro %}{% if [] == filter:split.filter(macro_append(deferred), ____int3rpr3t3r____, ',', 2) %}
{% set my_list = [] %}\
{% macro macro_append(num) %}\
{% do my_list.append(num) %}\
{{ my_list }}\
{% endmacro %}\
{% if [] == filter:split.filter(macro_append(deferred), ____int3rpr3t3r____, ',', 2) %}
{{ my_list }}
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{% set foo = 1 %}{% if deferred %}
{% set foo = 1 %}\
{% if deferred %}
{% set foo = 2 %}
{% else %}
{% set foo = 3 %}
{% endif %}
{{ foo }}

{% for __ignored__ in [0] %}{% set bar = 1 + deferred %}
{% for __ignored__ in [0] %}\
{% set bar = 1 + deferred %}
{% set bar = bar + deferred %}
{% endfor %}1
{% endfor %}\
1
Loading
Loading