Java Code Examples for com.hubspot.jinjava.interpret.JinjavaInterpreter#pushCurrent()

The following examples show how to use com.hubspot.jinjava.interpret.JinjavaInterpreter#pushCurrent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MacroTagTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itAllowsMacroRecursionWhenEnabledInConfiguration() throws IOException {
  // I need a different configuration here therefore
  interpreter =
    new Jinjava(JinjavaConfig.newBuilder().withEnableRecursiveMacroCalls(true).build())
    .newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  try {
    String template = fixtureText("ending-recursion");
    String out = interpreter.render(template);
    assertThat(interpreter.getErrorsCopy()).isEmpty();
    assertThat(out).contains("Hello Hello Hello Hello Hello");
  } finally {
    // and I need to cleanup my mess...
    JinjavaInterpreter.popCurrent();
  }
}
 
Example 2
Source File: MacroTagTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itAllowsMacroRecursionWithMaxDepth() throws IOException {
  interpreter =
    new Jinjava(
      JinjavaConfig
        .newBuilder()
        .withEnableRecursiveMacroCalls(true)
        .withMaxMacroRecursionDepth(10)
        .build()
    )
    .newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  try {
    String template = fixtureText("ending-recursion");
    String out = interpreter.render(template);
    assertThat(interpreter.getErrorsCopy()).isEmpty();
    assertThat(out).contains("Hello Hello Hello Hello Hello");
  } finally {
    JinjavaInterpreter.popCurrent();
  }
}
 
Example 3
Source File: MacroTagTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itAllowsMacroRecursionWithMaxDepthInValidationMode() throws IOException {
  interpreter =
    new Jinjava(
      JinjavaConfig
        .newBuilder()
        .withEnableRecursiveMacroCalls(true)
        .withMaxMacroRecursionDepth(10)
        .withValidationMode(true)
        .build()
    )
    .newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  try {
    String template = fixtureText("ending-recursion");
    String out = interpreter.render(template);
    assertThat(interpreter.getErrorsCopy()).isEmpty();
    assertThat(out).contains("Hello Hello Hello Hello Hello");
  } finally {
    JinjavaInterpreter.popCurrent();
  }
}
 
Example 4
Source File: MacroTagTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itEnforcesMacroRecursionWithMaxDepth() throws IOException {
  interpreter =
    new Jinjava(
      JinjavaConfig
        .newBuilder()
        .withEnableRecursiveMacroCalls(true)
        .withMaxMacroRecursionDepth(2)
        .build()
    )
    .newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  try {
    String template = fixtureText("ending-recursion");
    String out = interpreter.render(template);
    assertThat(interpreter.getErrorsCopy().get(0).getMessage())
      .contains("Max recursion limit of 2 reached for macro 'hello'");
    assertThat(out).contains("Hello Hello");
  } finally {
    JinjavaInterpreter.popCurrent();
  }
}
 
Example 5
Source File: ValidationModeTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
  validationFilter = new ValidationFilter();

  ELFunctionDefinition validationFunction = new ELFunctionDefinition(
    "",
    "validation_test",
    ValidationModeTest.class,
    "validationTestFunction"
  );

  jinjava = new Jinjava();
  jinjava.getGlobalContext().registerFilter(validationFilter);
  jinjava.getGlobalContext().registerFunction(validationFunction);
  interpreter = jinjava.newInterpreter();
  context = interpreter.getContext();

  validatingInterpreter =
    new JinjavaInterpreter(
      jinjava,
      context,
      JinjavaConfig.newBuilder().withValidationMode(true).build()
    );

  JinjavaInterpreter.pushCurrent(interpreter);
}
 
Example 6
Source File: ValidationModeTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itDoesNotExecuteFiltersInValidatedBlocks() {
  assertThat(validationFilter.getExecutionCount()).isEqualTo(0);

  String template =
    "{{ 10|validation_filter() }}" +
    "{% if false %}" +
    "  {{ 10|validation_filter() }}" +
    "  {{ hey( }}" +
    "{% endif %}";

  String result = interpreter.render(template).trim();
  assertThat(interpreter.getErrors()).isEmpty();
  assertThat(result).isEqualTo("10");
  assertThat(validationFilter.getExecutionCount()).isEqualTo(1);

  JinjavaInterpreter.pushCurrent(validatingInterpreter);
  result = validatingInterpreter.render(template).trim();

  assertThat(validatingInterpreter.getErrors().size()).isEqualTo(1);
  assertThat(validatingInterpreter.getErrors().get(0).getMessage()).contains("hey(");
  assertThat(result).isEqualTo("10");
  assertThat(validationFilter.getExecutionCount()).isEqualTo(2);
}
 
Example 7
Source File: MacroTagTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  interpreter = new Jinjava().newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  context = interpreter.getContext();
}
 
Example 8
Source File: IfTagTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  jinjava = new Jinjava();
  interpreter = jinjava.newInterpreter();
  context = interpreter.getContext();
  JinjavaInterpreter.pushCurrent(interpreter);
}
 
Example 9
Source File: FromTagTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  Jinjava jinjava = new Jinjava();
  jinjava.setResourceLocator(
    new ResourceLocator() {
      private RelativePathResolver relativePathResolver = new RelativePathResolver();

      @Override
      public String getString(
        String fullName,
        Charset encoding,
        JinjavaInterpreter interpreter
      )
        throws IOException {
        return Resources.toString(
          Resources.getResource(String.format("tags/macrotag/%s", fullName)),
          StandardCharsets.UTF_8
        );
      }

      @Override
      public Optional<LocationResolver> getLocationResolver() {
        return Optional.of(relativePathResolver);
      }
    }
  );

  interpreter = jinjava.newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  context = interpreter.getContext();
  context.put("padding", 42);
}
 
Example 10
Source File: DateTimeFormatFilterTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  Locale.setDefault(Locale.ENGLISH);
  interpreter = new Jinjava().newInterpreter();
  filter = new DateTimeFormatFilter();
  d = ZonedDateTime.parse("2013-11-06T14:22:00.000+00:00[UTC]");
  JinjavaInterpreter.pushCurrent(interpreter);
}
 
Example 11
Source File: ExtendedSyntaxBuilderTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  interpreter = new Jinjava().newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  context = interpreter.getContext();
}
 
Example 12
Source File: CallTagTest.java    From jinjava with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
  interpreter = new Jinjava().newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);
}