Skip to content

Commit

Permalink
Fix duplicated output on subtemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddewie committed Apr 26, 2024
1 parent c26d322 commit 1e14d41
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ fn evaluate_template(data: &Value, record: Pair<Rule>) -> Result<(String, bool),
}
}
Rule::template => {
let (evaluation, gobble_inner) = evaluate_template(&data, if_inner)?;
if gobble_inner {
result = result.trim_end_matches(&[' ', '\t']).to_string();
if valid {
let (evaluation, gobble_inner) = evaluate_template(&data, if_inner)?;
if gobble_inner {
result = result.trim_end_matches(&[' ', '\t']).to_string();
}
result.push_str(evaluation.as_str())
}
result.push_str(evaluation.as_str())
}
_ => unreachable!(),
}
Expand Down
19 changes: 19 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,23 @@ fn test_assert() {
\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z INFO template\] Parsing configuration using JSON format
\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z ERROR template\] ERROR: Could not render template: Assertion failed: Expected value 'true' but found 'false': This is not OK
$"#).unwrap());
}

#[test]
fn if_elif_with_subtemplate() {
let mut cmd = Command::cargo_bin("template").unwrap();
let assert = cmd
.arg("--template")
.arg("tests/template/if_elif_with_subtemplate.template")
.arg("--configuration")
.arg("tests/configuration/hello_world.yml")
.assert();

assert
.success()
.stdout("Hello\n")
.stderr(is_match(r#"^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z INFO template\] Using template file 'tests/template/if_elif_with_subtemplate.template'
\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z INFO template\] Using configuration file 'tests/configuration/hello_world.yml'
\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z INFO template\] Parsing configuration using YAML format
$"#).unwrap());
}
5 changes: 5 additions & 0 deletions tests/template/if_elif_with_subtemplate.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if match %}
-{% value %}-
{% else %}
{% value %}
{% end %}

0 comments on commit 1e14d41

Please sign in to comment.