Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
manheychiu1 committed Apr 19, 2024
1 parent e2b4c07 commit 4680096
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,47 @@ public void itPreventsAccidentalExpressions() {
JinjavaInterpreter.popCurrent();
}
}

@Test
public void itOutputsUndefinedVariableError() {
String template = "{% set foo=123 %}{{ foo }}{{ bar }}";

JinjavaInterpreter normalInterpreter = new JinjavaInterpreter(
jinjava,
jinjava.getGlobalContext(),
JinjavaConfig.newBuilder().withExecutionMode(EagerExecutionMode.instance()).build()
);
JinjavaInterpreter outputtingErrorInterpreters = new JinjavaInterpreter(
jinjava,
jinjava.getGlobalContext(),
JinjavaConfig
.newBuilder()
.withFeatureConfig(
FeatureConfig
.newBuilder()
.add(
JinjavaInterpreter.OUTPUT_UNDEFINED_VARIABLES_ERROR,
FeatureStrategies.ACTIVE
)
.build()
)
.withExecutionMode(EagerExecutionMode.instance())
.build()
);

String normalRenderResult = normalInterpreter.render(template);
String outputtingErrorRenderResult = outputtingErrorInterpreters.render(template);
assertThat(normalRenderResult).isEqualTo("123");
assertThat(outputtingErrorRenderResult).isEqualTo("123");
assertThat(normalInterpreter.getErrors()).isEmpty();
assertThat(outputtingErrorInterpreters.getErrors().size()).isEqualTo(1);
assertThat(outputtingErrorInterpreters.getErrors().get(0).getMessage())
.contains("Undefined variable: 'bar'");
assertThat(outputtingErrorInterpreters.getErrors().get(0).getReason())
.isEqualTo(ErrorReason.UNKNOWN);
assertThat(outputtingErrorInterpreters.getErrors().get(0).getSeverity())
.isEqualTo(ErrorType.WARNING);
assertThat(outputtingErrorInterpreters.getErrors().get(0).getCategoryErrors())
.isEqualTo(ImmutableMap.of("variable", "bar"));
}
}

0 comments on commit 4680096

Please sign in to comment.