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