Java Code Examples for com.hubspot.jinjava.interpret.Context#Library

The following examples show how to use com.hubspot.jinjava.interpret.Context#Library . 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: ExpressionResolverTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itBlocksDisabledTags() {
  Map<Context.Library, Set<String>> disabled = ImmutableMap.of(
    Context.Library.TAG,
    ImmutableSet.of("raw")
  );
  assertThat(interpreter.render("{% raw %}foo{% endraw %}")).isEqualTo("foo");

  try (
    JinjavaInterpreter.InterpreterScopeClosable c = interpreter.enterScope(disabled)
  ) {
    interpreter.render("{% raw %} foo {% endraw %}");
  }

  TemplateError e = interpreter.getErrorsCopy().get(0);
  assertThat(e.getItem()).isEqualTo(ErrorItem.TAG);
  assertThat(e.getReason()).isEqualTo(ErrorReason.DISABLED);
  assertThat(e.getMessage()).contains("'raw' is disabled in this context");
}
 
Example 2
Source File: ExpressionResolverTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itBlocksDisabledTagsInIncludes() {
  final String jinja = "top {% include \"tags/includetag/raw.html\" %}";

  Map<Context.Library, Set<String>> disabled = ImmutableMap.of(
    Context.Library.TAG,
    ImmutableSet.of("raw")
  );
  assertThat(interpreter.render(jinja)).isEqualTo("top before raw after\n");

  try (
    JinjavaInterpreter.InterpreterScopeClosable c = interpreter.enterScope(disabled)
  ) {
    interpreter.render(jinja);
  }
  TemplateError e = interpreter.getErrorsCopy().get(0);
  assertThat(e.getItem()).isEqualTo(ErrorItem.TAG);
  assertThat(e.getReason()).isEqualTo(ErrorReason.DISABLED);
  assertThat(e.getMessage()).contains("'raw' is disabled in this context");
}
 
Example 3
Source File: ExpressionResolverTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itBlocksDisabledFilters() {
  Map<Context.Library, Set<String>> disabled = ImmutableMap.of(
    Context.Library.FILTER,
    ImmutableSet.of("truncate")
  );
  assertThat(interpreter.resolveELExpression("\"hey\"|truncate(2)", -1))
    .isEqualTo("h...");

  try (
    JinjavaInterpreter.InterpreterScopeClosable c = interpreter.enterScope(disabled)
  ) {
    interpreter.resolveELExpression("\"hey\"|truncate(2)", -1);
    TemplateError e = interpreter.getErrorsCopy().get(0);
    assertThat(e.getItem()).isEqualTo(ErrorItem.FILTER);
    assertThat(e.getReason()).isEqualTo(ErrorReason.DISABLED);
    assertThat(e.getMessage()).contains("truncate' is disabled in this context");
  }
}
 
Example 4
Source File: ExpressionResolverTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itBlocksDisabledFunctions() {
  Map<Context.Library, Set<String>> disabled = ImmutableMap.of(
    Library.FUNCTION,
    ImmutableSet.of(":range")
  );

  String template = "hi {% for i in range(1, 3) %}{{i}} {% endfor %}";

  String rendered = jinjava.render(template, context);
  assertEquals("hi 1 2 ", rendered);

  final JinjavaConfig config = JinjavaConfig
    .newBuilder()
    .withDisabled(disabled)
    .build();

  final RenderResult renderResult = jinjava.renderForResult(template, context, config);
  assertEquals("hi ", renderResult.getOutput());
  TemplateError e = renderResult.getErrors().get(0);
  assertThat(e.getItem()).isEqualTo(ErrorItem.FUNCTION);
  assertThat(e.getReason()).isEqualTo(ErrorReason.DISABLED);
  assertThat(e.getMessage()).contains("':range' is disabled in this context");
}
 
Example 5
Source File: ExpressionResolverTest.java    From jinjava with Apache License 2.0 6 votes vote down vote up
@Test
public void itBlocksDisabledExpTests() {
  Map<Context.Library, Set<String>> disabled = ImmutableMap.of(
    Context.Library.EXP_TEST,
    ImmutableSet.of("even")
  );
  assertThat(interpreter.render("{% if 2 is even %}yes{% endif %}")).isEqualTo("yes");

  try (
    JinjavaInterpreter.InterpreterScopeClosable c = interpreter.enterScope(disabled)
  ) {
    interpreter.render("{% if 2 is even %}yes{% endif %}");
    TemplateError e = interpreter.getErrorsCopy().get(0);
    assertThat(e.getItem()).isEqualTo(ErrorItem.EXPRESSION_TEST);
    assertThat(e.getReason()).isEqualTo(ErrorReason.DISABLED);
    assertThat(e.getMessage()).contains("even' is disabled in this context");
  }
}
 
Example 6
Source File: JinjavaConfig.java    From jinjava with Apache License 2.0 5 votes vote down vote up
private JinjavaConfig(
  Charset charset,
  Locale locale,
  ZoneId timeZone,
  int maxRenderDepth,
  Map<Context.Library, Set<String>> disabled,
  boolean trimBlocks,
  boolean lstripBlocks,
  boolean enableRecursiveMacroCalls,
  int maxMacroRecursionDepth,
  boolean failOnUnknownTokens,
  long maxOutputSize,
  boolean nestedInterpretationEnabled,
  RandomNumberGeneratorStrategy randomNumberGenerator,
  boolean validationMode,
  long maxStringLength,
  InterpreterFactory interpreterFactory,
  TokenScannerSymbols tokenScannerSymbols,
  ELResolver elResolver
) {
  this.charset = charset;
  this.locale = locale;
  this.timeZone = timeZone;
  this.maxRenderDepth = maxRenderDepth;
  this.disabled = disabled;
  this.trimBlocks = trimBlocks;
  this.lstripBlocks = lstripBlocks;
  this.enableRecursiveMacroCalls = enableRecursiveMacroCalls;
  this.maxMacroRecursionDepth = maxMacroRecursionDepth;
  this.failOnUnknownTokens = failOnUnknownTokens;
  this.maxOutputSize = maxOutputSize;
  this.nestedInterpretationEnabled = nestedInterpretationEnabled;
  this.randomNumberGenerator = randomNumberGenerator;
  this.validationMode = validationMode;
  this.maxStringLength = maxStringLength;
  this.interpreterFactory = interpreterFactory;
  this.tokenScannerSymbols = tokenScannerSymbols;
  this.elResolver = elResolver;
}
 
Example 7
Source File: JinjavaConfig.java    From jinjava with Apache License 2.0 4 votes vote down vote up
public Builder withDisabled(Map<Context.Library, Set<String>> disabled) {
  this.disabled = disabled;
  return this;
}