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