Search in sources :

Example 6 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class BytecodeCompilerTest method testDelCall_delVariant.

@Test
public void testDelCall_delVariant() throws IOException {
    String soyFileContent1 = Joiner.on("\n").join("{namespace ns1}", "", "/***/", "{template .callerTemplate}", "  {@param variant : string}", "  {delcall ns1.del variant=\"$variant\" allowemptydefault=\"true\"/}", "{/template}", "", "/** */", "{deltemplate ns1.del variant=\"'v1'\"}", "  v1", "{/deltemplate}", "", "/** */", "{deltemplate ns1.del variant=\"'v2'\"}", "  v2", "{/deltemplate}", "");
    CompiledTemplates templates = compileFiles(soyFileContent1);
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns1.callerTemplate");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
    assertThat(factory.create(TemplateTester.asRecord(ImmutableMap.of("variant", "v1")), EMPTY_DICT).render(builder, context)).isEqualTo(RenderResult.done());
    assertThat(builder.getAndClearBuffer()).isEqualTo("v1");
    assertThat(factory.create(TemplateTester.asRecord(ImmutableMap.of("variant", "v2")), EMPTY_DICT).render(builder, context)).isEqualTo(RenderResult.done());
    assertThat(builder.getAndClearBuffer()).isEqualTo("v2");
    assertThat(factory.create(TemplateTester.asRecord(ImmutableMap.of("variant", "unknown")), EMPTY_DICT).render(builder, context)).isEqualTo(RenderResult.done());
    assertThat(builder.toString()).isEmpty();
    TemplateMetadata templateMetadata = getTemplateMetadata(templates, "ns1.callerTemplate");
    assertThat(templateMetadata.callees()).isEmpty();
    assertThat(templateMetadata.delCallees()).asList().containsExactly("ns1.del");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) TemplateMetadata(com.google.template.soy.jbcsrc.shared.TemplateMetadata) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 7 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class VeLoggingTest method renderTemplate.

private void renderTemplate(Map<String, ?> params, OutputAppendable output, String... templateBodyLines) throws IOException {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents("{namespace ns}\n" + "{template .foo}\n" + Joiner.on("\n").join(templateBodyLines) + "\n{/template}").typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(config).addSoyFunction(new DepthFunction()).runAutoescaper(true).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    RenderContext ctx = TemplateTester.getDefaultContext(templates).toBuilder().hasLogger(true).build();
    RenderResult result = templates.getTemplateFactory("ns.foo").create(TemplateTester.asRecord(params), EMPTY_DICT).render(output, ctx);
    assertThat(result).isEqualTo(RenderResult.done());
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) SoyFileSetParserBuilder(com.google.template.soy.SoyFileSetParserBuilder) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 8 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class DetachStateTest method testDetach_multipleNodes.

@Test
public void testDetach_multipleNodes() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("hello", // get merged
    "{' '}", "world");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    CompiledTemplate template = factory.create(EMPTY_DICT, EMPTY_DICT);
    // Basic stuff works
    TestAppendable output = new TestAppendable();
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("hello world");
    output = new TestAppendable();
    output.softLimitReached = true;
    // detached!!!
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello ");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello world");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    // nothing was added
    assertThat(output.toString()).isEqualTo("hello world");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 9 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class DetachStateTest method testDetachOnUnResolvedProvider.

@Test
public void testDetachOnUnResolvedProvider() throws IOException {
    SettableFuture<String> future = SettableFuture.create();
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param foo : string}", "prefix{sp}{$foo}{sp}suffix");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    CompiledTemplate template = factory.create(asRecord(ImmutableMap.of("foo", future)), EMPTY_DICT);
    BufferingAppendable output = LoggingAdvisingAppendable.buffering();
    RenderResult result = template.render(output, context);
    assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
    assertThat(result.future()).isEqualTo(future);
    assertThat(output.toString()).isEqualTo("prefix ");
    // No progress is made, our caller is an idiot and didn't wait for the future
    result = template.render(output, context);
    assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
    assertThat(result.future()).isEqualTo(future);
    assertThat(output.toString()).isEqualTo("prefix ");
    future.set("future");
    result = template.render(output, context);
    assertThat(result).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("prefix future suffix");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 10 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class DetachStateTest method testDetachOnParamTransclusion.

@Test
public void testDetachOnParamTransclusion() throws IOException {
    CompiledTemplates templates = TemplateTester.compileFile("{namespace ns}", "", "/** */", "{template .caller}", "  {@param callerParam : string}", "  {call .callee}", "    {param calleeParam kind=\"text\"}", "      prefix {$callerParam} suffix", "    {/param}", "  {/call}", "{/template}", "", "/** */", "{template .callee}", "  {@param calleeParam : string}", "  {$calleeParam}", "{/template}", "");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.caller");
    RenderContext context = getDefaultContext(templates);
    SettableFuture<String> param = SettableFuture.create();
    SoyRecord params = asRecord(ImmutableMap.of("callerParam", param));
    CompiledTemplate template = factory.create(params, EMPTY_DICT);
    BufferingAppendable output = LoggingAdvisingAppendable.buffering();
    assertThat(template.render(output, context)).isEqualTo(RenderResult.continueAfter(param));
    assertThat(output.toString()).isEqualTo("prefix ");
    param.set("foo");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("prefix foo suffix");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) SoyRecord(com.google.template.soy.data.SoyRecord) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)20 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)20 Test (org.junit.Test)19 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)16 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)13 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)6 SoyDict (com.google.template.soy.data.SoyDict)2 SoyRecord (com.google.template.soy.data.SoyRecord)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1