/** * Take all resolved strings from a context object and apply them to this context. * Useful for passing resolved values up a tag hierarchy. * * @param context - context object to apply resolved values from. */ public void addResolvedFrom(Context context) { context.getResolvedExpressions().forEach(this::addResolvedExpression); context.getResolvedFunctions().forEach(this::addResolvedFunction); context.getResolvedValues().forEach(this::addResolvedValue); }
@Test public void itStoresResolvedFunctions() { context.put("datetime", 12345); final JinjavaConfig config = JinjavaConfig.newBuilder().build(); String template = "{% for i in range(1, 5) %}{{i}} {% endfor %}\n{{ unixtimestamp(datetime) }}"; final RenderResult renderResult = jinjava.renderForResult(template, context, config); assertThat(renderResult.getOutput()).isEqualTo("1 2 3 4 \n12000"); assertThat(renderResult.getContext().getResolvedFunctions()).hasSameElementsAs(ImmutableSet.of(":range", ":unixtimestamp")); }
@Test public void itStoresResolvedFunctions() { context.put("datetime", 12345); final JinjavaConfig config = JinjavaConfig.newBuilder().build(); String template = "{% for i in range(1, 5) %}{{i}} {% endfor %}\n{{ unixtimestamp(datetime) }}"; final RenderResult renderResult = jinjava.renderForResult(template, context, config); assertThat(renderResult.getOutput()).isEqualTo("1 2 3 4 \n12000"); assertThat(renderResult.getContext().getResolvedFunctions()).hasSameElementsAs(ImmutableSet.of(":range", ":unixtimestamp")); }
@Test public void itAddsResolvedValuesFromAnotherContextObject() { Context donor = new Context(); donor.addResolvedValue(RESOLVED_VALUE); donor.addResolvedFunction(RESOLVED_FUNCTION); donor.addResolvedExpression(RESOLVED_EXPRESSION); assertThat(context.getResolvedValues()).doesNotContain(RESOLVED_VALUE); assertThat(context.getResolvedFunctions()).doesNotContain(RESOLVED_FUNCTION); assertThat(context.getResolvedExpressions()).doesNotContain(RESOLVED_EXPRESSION); context.addResolvedFrom(donor); assertThat(context.getResolvedValues()).contains(RESOLVED_VALUE); assertThat(context.getResolvedFunctions()).contains(RESOLVED_FUNCTION); assertThat(context.getResolvedExpressions()).contains(RESOLVED_EXPRESSION); }
@Test public void itAddsResolvedValuesFromAnotherContextObject() { Context donor = new Context(); donor.addResolvedValue(RESOLVED_VALUE); donor.addResolvedFunction(RESOLVED_FUNCTION); donor.addResolvedExpression(RESOLVED_EXPRESSION); assertThat(context.getResolvedValues()).doesNotContain(RESOLVED_VALUE); assertThat(context.getResolvedFunctions()).doesNotContain(RESOLVED_FUNCTION); assertThat(context.getResolvedExpressions()).doesNotContain(RESOLVED_EXPRESSION); context.addResolvedFrom(donor); assertThat(context.getResolvedValues()).contains(RESOLVED_VALUE); assertThat(context.getResolvedFunctions()).contains(RESOLVED_FUNCTION); assertThat(context.getResolvedExpressions()).contains(RESOLVED_EXPRESSION); }
@Test public void itRecursivelyAddsValuesUpTheContextChain() { Context child = new Context(context); child.addResolvedValue(RESOLVED_VALUE); child.addResolvedFunction(RESOLVED_FUNCTION); child.addResolvedExpression(RESOLVED_EXPRESSION); assertThat(context.getResolvedValues()).contains(RESOLVED_VALUE); assertThat(context.getResolvedFunctions()).contains(RESOLVED_FUNCTION); assertThat(context.getResolvedExpressions()).contains(RESOLVED_EXPRESSION); }
@Test public void itRecursivelyAddsValuesUpTheContextChain() { Context child = new Context(context); child.addResolvedValue(RESOLVED_VALUE); child.addResolvedFunction(RESOLVED_FUNCTION); child.addResolvedExpression(RESOLVED_EXPRESSION); assertThat(context.getResolvedValues()).contains(RESOLVED_VALUE); assertThat(context.getResolvedFunctions()).contains(RESOLVED_FUNCTION); assertThat(context.getResolvedExpressions()).contains(RESOLVED_EXPRESSION); }