Search in sources :

Example 1 with TemplateMetadata

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

the class BytecodeCompilerTest method testParamFields.

@Test
public void testParamFields() throws Exception {
    CompiledTemplate.Factory multipleParams = TemplateTester.compileTemplateBody("{@param foo : string}", "{@param baz : string}", "{@inject bar : string}", "{$foo + $baz + $bar}").getTemplateFactory("ns.foo");
    SoyDict params = SoyValueConverterUtility.newDict("foo", StringData.forValue("foo"), "bar", StringData.forValue("bar"), "baz", StringData.forValue("baz"));
    CompiledTemplate template = multipleParams.create(params, params);
    assertThat(getField("foo", template)).isEqualTo(StringData.forValue("foo"));
    assertThat(getField("bar", template)).isEqualTo(StringData.forValue("bar"));
    assertThat(getField("baz", template)).isEqualTo(StringData.forValue("baz"));
    TemplateMetadata templateMetadata = template.getClass().getAnnotation(TemplateMetadata.class);
    assertThat(templateMetadata.injectedParams()).asList().containsExactly("bar");
    assertThat(templateMetadata.callees()).isEmpty();
    assertThat(templateMetadata.delCallees()).isEmpty();
}
Also used : SoyDict(com.google.template.soy.data.SoyDict) TemplateMetadata(com.google.template.soy.jbcsrc.shared.TemplateMetadata) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 2 with TemplateMetadata

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

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

the class BytecodeCompilerTest method testBasicFunctionality.

@Test
public void testBasicFunctionality() {
    // make sure we don't break standard reflection access
    CompiledTemplate.Factory factory = TemplateTester.compileTemplateBody("hello world").getTemplateFactory("ns.foo");
    assertThat(factory.getClass().getName()).isEqualTo("com.google.template.soy.jbcsrc.gen.ns.foo$Factory");
    assertThat(factory.getClass().getSimpleName()).isEqualTo("Factory");
    CompiledTemplate templateInstance = factory.create(EMPTY_DICT, EMPTY_DICT);
    Class<? extends CompiledTemplate> templateClass = templateInstance.getClass();
    assertThat(templateClass.getName()).isEqualTo("com.google.template.soy.jbcsrc.gen.ns.foo");
    assertThat(templateClass.getSimpleName()).isEqualTo("foo");
    TemplateMetadata templateMetadata = templateClass.getAnnotation(TemplateMetadata.class);
    assertThat(templateMetadata.contentKind()).isEqualTo("HTML");
    assertThat(templateInstance.kind()).isEqualTo(ContentKind.HTML);
    assertThat(templateMetadata.injectedParams()).isEmpty();
    assertThat(templateMetadata.callees()).isEmpty();
    assertThat(templateMetadata.delCallees()).isEmpty();
    // ensure that the factory is an inner class of the template.
    assertThat(factory.getClass().getEnclosingClass()).isEqualTo(templateClass);
    assertThat(factory.getClass().getDeclaringClass()).isEqualTo(templateClass);
    assertThat(templateClass.getDeclaredClasses()).asList().contains(factory.getClass());
}
Also used : TemplateMetadata(com.google.template.soy.jbcsrc.shared.TemplateMetadata) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 4 with TemplateMetadata

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

the class TemplateCompiler method generateTemplateMetadata.

/**
 * Writes a {@link TemplateMetadata} to the generated class.
 */
private void generateTemplateMetadata() {
    SanitizedContentKind contentKind = template.node().getContentKind();
    String kind = contentKind == null ? "" : contentKind.name();
    // using linked hash sets below for determinism
    Set<String> uniqueIjs = new LinkedHashSet<>();
    for (VarRefNode var : getAllNodesOfType(template.node(), VarRefNode.class)) {
        if (var.isInjected()) {
            uniqueIjs.add(var.getName());
        }
    }
    Set<String> callees = new LinkedHashSet<>();
    for (CallBasicNode call : getAllNodesOfType(template.node(), CallBasicNode.class)) {
        callees.add(call.getCalleeName());
    }
    Set<String> delCallees = new LinkedHashSet<>();
    for (CallDelegateNode call : getAllNodesOfType(template.node(), CallDelegateNode.class)) {
        delCallees.add(call.getDelCalleeName());
    }
    TemplateMetadata.DelTemplateMetadata deltemplateMetadata;
    if (template.node().getKind() == SoyNode.Kind.TEMPLATE_DELEGATE_NODE) {
        TemplateDelegateNode delegateNode = (TemplateDelegateNode) template.node();
        deltemplateMetadata = createDelTemplateMetadata(delegateNode.getDelPackageName() == null ? "" : delegateNode.getDelPackageName(), delegateNode.getDelTemplateName(), delegateNode.getDelTemplateVariant());
    } else {
        deltemplateMetadata = createDefaultDelTemplateMetadata();
    }
    TemplateMetadata metadata = createTemplateMetadata(kind, uniqueIjs, callees, delCallees, deltemplateMetadata);
    TEMPLATE_METADATA_REF.write(metadata, writer);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind) CallBasicNode(com.google.template.soy.soytree.CallBasicNode) TemplateMetadata(com.google.template.soy.jbcsrc.shared.TemplateMetadata) CallDelegateNode(com.google.template.soy.soytree.CallDelegateNode)

Aggregations

TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)4 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 Test (org.junit.Test)3 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)1 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)1 SoyDict (com.google.template.soy.data.SoyDict)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)1 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)1 CallBasicNode (com.google.template.soy.soytree.CallBasicNode)1 CallDelegateNode (com.google.template.soy.soytree.CallDelegateNode)1 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)1 LinkedHashSet (java.util.LinkedHashSet)1