Search in sources :

Example 6 with CompiledTemplates

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

the class DetachStateTest method testDetach_saveRestore.

// ensure that when we call back in, locals are restored
@Test
public void testDetach_saveRestore() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{for $i in range(10)}", "  {$i}", "{/for}");
    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("0123456789");
    output = new TestAppendable();
    output.softLimitReached = true;
    for (int i = 0; i < 10; i++) {
        assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
        assertThat(output.toString()).isEqualTo(String.valueOf(i));
        output.delegate.setLength(0);
    }
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    // last render was empty
    assertThat(output.toString()).isEmpty();
}
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 7 with CompiledTemplates

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

the class LazyClosureCompilerTest method testLetValueNode_captureSyntheticParameter.

// Regression test for a bug where captures of synthetic variables wouldn't be deduped properly
// and we would recapture the same synthetic multiple times.
@Test
public void testLetValueNode_captureSyntheticParameter() {
    // make sure that if we capture a synthetic we only capture it once
    CompiledTemplates templates = compileTemplateBody("{@param l : list<string>}", "{for $s in $l}", // the index function is implemented via a synthetic loop index
    "  {let $bar : index($s) + index($s) /}", "  {$bar}", "{/for}");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    CompiledTemplate template = factory.create(EMPTY_DICT, EMPTY_DICT);
    List<Class<?>> innerClasses = Lists.newArrayList(template.getClass().getDeclaredClasses());
    innerClasses.remove(factory.getClass());
    Class<?> let = Iterables.getOnlyElement(innerClasses);
    assertThat(let.getSimpleName()).isEqualTo("let_bar");
    // the closures capture variables as constructor parameters.
    // in this case since index() always returns an unboxed integer the parameter should be a single
    // int.  In a previous version, we passed 2 ints.
    assertThat(let.getDeclaredConstructors()).hasLength(1);
    Constructor<?> cStruct = let.getDeclaredConstructors()[0];
    assertThat(Arrays.asList(cStruct.getParameterTypes())).isEqualTo(Arrays.asList(int.class));
}
Also used : CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 8 with CompiledTemplates

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

the class BytecodeCompilerTest method testDelCall_delPackageSelections.

@Test
public void testDelCall_delPackageSelections() throws IOException {
    String soyFileContent1 = Joiner.on("\n").join("{namespace ns1}", "", "/***/", "{template .callerTemplate}", "  {delcall myApp.myDelegate}", "    {param boo: 'aaaaaah' /}", "  {/delcall}", "{/template}", "", "/** */", // default implementation (doesn't use $boo)
    "{deltemplate myApp.myDelegate}", "  {@param boo : string}", "  default", "{/deltemplate}", "");
    String soyFileContent2 = Joiner.on("\n").join("{delpackage SecretFeature}", "{namespace ns2}", "", "/** */", // implementation in SecretFeature
    "{deltemplate myApp.myDelegate}", "  {@param boo : string}", "  SecretFeature {$boo}", "{/deltemplate}", "");
    String soyFileContent3 = Joiner.on("\n").join("{delpackage AlternateSecretFeature}", "{namespace ns3}", "", "/** */", // implementation in AlternateSecretFeature
    "{deltemplate myApp.myDelegate}", "  {@param boo : string}", "  AlternateSecretFeature {call .helper data=\"all\" /}", "{/deltemplate}", "");
    String soyFileContent4 = Joiner.on("\n").join("{namespace ns3}", "", "/** */", "{template .helper}", "  {@param boo : string}", "  {$boo}", "{/template}", "");
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent1, soyFileContent2, soyFileContent3, soyFileContent4).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns1.callerTemplate");
    Predicate<String> activePackages = Predicates.alwaysFalse();
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("default");
    activePackages = Predicates.equalTo("SecretFeature");
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("SecretFeature aaaaaah");
    activePackages = Predicates.equalTo("AlternateSecretFeature");
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("AlternateSecretFeature aaaaaah");
    activePackages = Predicates.equalTo("NonexistentFeature");
    assertThat(renderWithContext(factory, getDefaultContext(templates, activePackages))).isEqualTo("default");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 9 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates 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 10 with CompiledTemplates

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

the class BytecodeCompilerTest method testCallBasicNode.

@Test
public void testCallBasicNode() throws IOException {
    CompiledTemplates templates = TemplateTester.compileFile("{namespace ns}", "", "/** */", "{template .callerDataAll}", "  {@param foo : string}", "  {call .callee data=\"all\" /}", "{/template}", "", "/** */", "{template .callerDataExpr}", "  {@param rec : [foo : string]}", "  {call .callee data=\"$rec\" /}", "{/template}", "", "/** */", "{template .callerParams}", "  {@param p1 : string}", "  {call .callee}", "    {param foo : $p1 /}", "    {param boo : 'a' + 1 + 'b' /}", "  {/call}", "{/template}", "", "/** */", "{template .callerParamsAndData}", "  {@param p1 : string}", "  {call .callee data=\"all\"}", "    {param foo : $p1 /}", "  {/call}", "{/template}", "", "/** */", "{template .callee}", "  {@param foo : string}", "  {@param? boo : string}", "Foo: {$foo}{\\n}", "Boo: {$boo}{\\n}", "{/template}", "");
    ParamStore params = new BasicParamStore(2);
    params.setField("foo", StringData.forValue("foo"));
    assertThat(render(templates, params, "ns.callerDataAll")).isEqualTo("Foo: foo\nBoo: null\n");
    params.setField("boo", StringData.forValue("boo"));
    assertThat(render(templates, params, "ns.callerDataAll")).isEqualTo("Foo: foo\nBoo: boo\n");
    assertThat(getTemplateMetadata(templates, "ns.callerDataAll").callees()).asList().containsExactly("ns.callee");
    params = new BasicParamStore(2);
    params.setField("rec", new BasicParamStore(2).setField("foo", StringData.forValue("foo")));
    assertThat(render(templates, params, "ns.callerDataExpr")).isEqualTo("Foo: foo\nBoo: null\n");
    ((ParamStore) params.getField("rec")).setField("boo", StringData.forValue("boo"));
    assertThat(render(templates, params, "ns.callerDataExpr")).isEqualTo("Foo: foo\nBoo: boo\n");
    assertThat(getTemplateMetadata(templates, "ns.callerDataExpr").callees()).asList().containsExactly("ns.callee");
    params = new BasicParamStore(2);
    params.setField("p1", StringData.forValue("foo"));
    assertThat(render(templates, params, "ns.callerParams")).isEqualTo("Foo: foo\nBoo: a1b\n");
    assertThat(getTemplateMetadata(templates, "ns.callerParams").callees()).asList().containsExactly("ns.callee");
    params = new BasicParamStore(2);
    params.setField("p1", StringData.forValue("foo"));
    params.setField("boo", StringData.forValue("boo"));
    assertThat(render(templates, params, "ns.callerParamsAndData")).isEqualTo("Foo: foo\nBoo: boo\n");
    assertThat(getTemplateMetadata(templates, "ns.callerParamsAndData").callees()).asList().containsExactly("ns.callee");
}
Also used : BasicParamStore(com.google.template.soy.data.internal.BasicParamStore) ParamStore(com.google.template.soy.data.internal.ParamStore) BasicParamStore(com.google.template.soy.data.internal.BasicParamStore) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) Test(org.junit.Test)

Aggregations

CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)33 Test (org.junit.Test)30 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)22 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)20 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)14 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)5 SoyRecord (com.google.template.soy.data.SoyRecord)3 SoyDict (com.google.template.soy.data.SoyDict)2 IOException (java.io.IOException)2 Stopwatch (com.google.common.base.Stopwatch)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 ByteString (com.google.protobuf.ByteString)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 EscapeHtmlDirective (com.google.template.soy.coredirectives.EscapeHtmlDirective)1 BasicParamStore (com.google.template.soy.data.internal.BasicParamStore)1 ParamStore (com.google.template.soy.data.internal.ParamStore)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)1