From 468009609f2bcaca0b3ea7e058ccf92dff189c49 Mon Sep 17 00:00:00 2001 From: Manhey Chiu Date: Fri, 19 Apr 2024 04:47:31 +0100 Subject: [PATCH] Add unit test --- .../interpret/JinjavaInterpreterTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java index e8e9641a1..f866db349 100644 --- a/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java +++ b/src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java @@ -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")); + } }