Search in sources :

Example 26 with CompiledTemplates

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>");
}
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) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 27 with CompiledTemplates

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);
    }
}
Also used : CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) IOException(java.io.IOException) Test(org.junit.Test)

Example 28 with CompiledTemplates

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);
}
Also used : CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) Test(org.junit.Test)

Example 29 with CompiledTemplates

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");
}
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) SoyDict(com.google.template.soy.data.SoyDict) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 30 with CompiledTemplates

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");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)33 Test (org.junit.Test)30 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)22 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)20 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)14 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)5 SoyRecord (com.google.template.soy.data.SoyRecord)3 SoyDict (com.google.template.soy.data.SoyDict)2 IOException (java.io.IOException)2 Stopwatch (com.google.common.base.Stopwatch)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 ByteString (com.google.protobuf.ByteString)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 EscapeHtmlDirective (com.google.template.soy.coredirectives.EscapeHtmlDirective)1 BasicParamStore (com.google.template.soy.data.internal.BasicParamStore)1 ParamStore (com.google.template.soy.data.internal.ParamStore)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)1