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();
}
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");
}
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());
}
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);
}
Aggregations