use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class StreamingPrintDirectivesTest method testStreamingCall.
@Test
public void testStreamingCall() throws IOException {
// As of right now only a few directives support streaming, but this includes |escapeHtml and
// |escapeJsString, so we should be able to transitively stream through all of that.
CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .foo}", " {call .bar data=\"all\"/}", "{/template}", "", "{template .bar}", " <script>var x=\"{call .baz data=\"all\" /}\";</script>", "{/template}", "", "{template .baz kind=\"text\"}", " {@param future : ?}", " \"{$future}\" ", "{/template}", "");
RenderContext context = getDefaultContext(templates);
BufferingAppendable output = BufferingAppendable.buffering();
SettableFuture<String> future = SettableFuture.create();
CompiledTemplate template = templates.getTemplateFactory("ns.foo").create(SoyValueConverterUtility.newDict("future", future), EMPTY_DICT);
template.render(output, context);
assertThat(output.getAndClearBuffer()).isEqualTo("<script>var x=\"\\x22");
future.set("hello");
template.render(output, context);
assertThat(output.getAndClearBuffer()).isEqualTo("hello\\x22\";</script>");
}
use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class BytecodeCompilerTest method testForeachLoopLineNumbers.
// There used to be missing information from the line numbers assigned to the list expressions
// in foreach loop which would 'blame' the previous statement, causing much confusion. Make sure
// it is accurate.
@Test
public void testForeachLoopLineNumbers() throws Exception {
CompiledTemplates templates = TemplateTester.compileFile(Joiner.on("\n").join("{namespace ns}", "", "{template .foo}", " {@param list : ?}", " {@param? opt : ?}", "{if not $opt}", // if statement.
" {for $foo in $list}", " {$foo}{if not isLast($foo)}{sp}{/if}", " {/for}", "{/if}", "{/template}"));
assertThat(render(templates, asRecord(ImmutableMap.of("list", ImmutableList.of(1, 2))), "ns.foo")).isEqualTo("1 2");
ListenableFuture<?> failed = Futures.immediateFailedFuture(new RuntimeException("boom"));
// failed future, the template should show a different line number
try {
render(templates, asRecord(ImmutableMap.of("list", failed)), "ns.foo");
fail();
} catch (Exception e) {
assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(7);
}
}
use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class BytecodeCompilerTest method testBasicFunctionality_privateTemplate.
@Test
public void testBasicFunctionality_privateTemplate() {
// make sure you can't access factories for priate tempaltes
CompiledTemplates templates = TemplateTester.compileFile("{namespace ns}{template .foo visibility=\"private\"}hello world{/template}");
try {
templates.getTemplateFactory("ns.foo");
fail();
} catch (IllegalArgumentException expected) {
}
// we can still access metadata
assertThat(templates.getTemplateContentKind("ns.foo")).hasValue(ContentKind.HTML);
}
use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class BytecodeCompilerTest method testParamValidation.
@Test
public void testParamValidation() throws Exception {
CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param foo : int}", "{$foo ?: -1}");
CompiledTemplate.Factory singleParam = templates.getTemplateFactory("ns.foo");
RenderContext context = getDefaultContext(templates);
BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
SoyDict params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
singleParam.create(params, EMPTY_DICT).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("1");
singleParam.create(EMPTY_DICT, EMPTY_DICT).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("-1");
templates = TemplateTester.compileTemplateBody("{@inject foo : int}", "{$foo}");
CompiledTemplate.Factory singleIj = templates.getTemplateFactory("ns.foo");
context = getDefaultContext(templates);
params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("1");
params = SoyValueConverterUtility.newDict();
singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
assertThat(builder.getAndClearBuffer()).isEqualTo("null");
}
use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.
the class BytecodeCompilerTest method testDebugSoyTemplateInfo.
@Test
public void testDebugSoyTemplateInfo() throws IOException {
String soyFileContent = Joiner.on("\n").join("{namespace ns}", "", "{template .html}", " <div>foo</div>", "{/template}", "", "{template .text kind=\"text\"}", " foo", "{/template}", "", "{template .htmlNoTag}", " foo", "{/template}");
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent).addHtmlAttributesForDebugging(true).parse().fileSet();
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
// HTML templates
CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.html");
assertThat(renderWithContext(factory, getDefaultContext(templates))).isEqualTo("<div>foo</div>");
// If debugSoyTemplateInfo is enabled, we should render additional HTML comments.
assertThat(renderWithContext(factory, getDefaultContextWithDebugInfo(templates))).isEqualTo("<div data-debug-soy=\"ns.html no-path:4\">foo</div>");
// We should never render these comments for templates with kind="text".
factory = templates.getTemplateFactory("ns.text");
assertThat(renderWithContext(factory, getDefaultContext(templates))).isEqualTo("foo");
assertThat(renderWithContext(factory, getDefaultContextWithDebugInfo(templates))).isEqualTo("foo");
factory = templates.getTemplateFactory("ns.htmlNoTag");
assertThat(renderWithContext(factory, getDefaultContext(templates))).isEqualTo("foo");
assertThat(renderWithContext(factory, getDefaultContextWithDebugInfo(templates))).isEqualTo("foo");
}
Aggregations