Search in sources :

Example 31 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class BytecodeCompilerTest method testExpressionLineNumbers.

@Test
public void testExpressionLineNumbers() throws Exception {
    CompiledTemplates templates = TemplateTester.compileFile(Joiner.on("\n").join("{namespace ns}", "", "{template .foo}", "  {@param p1 : ?}", "  {@param p2 : ?}", "  {@param p3 : ?}", "  {@param p4 : ?}", // This is a single expression split across multiple lines
    "{$p1", " + $p2", " + $p3", " + $p4", "}", "{/template}"));
    assertThat(render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", 2, "p3", 3, "p4", 4)), "ns.foo")).isEqualTo("10");
    ListenableFuture<?> failed = Futures.immediateFailedFuture(new RuntimeException("boom"));
    // failed future, the template should show a different line number
    try {
        render(templates, asRecord(ImmutableMap.of("p1", failed, "p2", 2, "p3", 3, "p4", 4)), "ns.foo");
        fail();
    } catch (Exception e) {
        assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(8);
    }
    try {
        render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", failed, "p3", 3, "p4", 4)), "ns.foo");
        fail();
    } catch (Exception e) {
        assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(9);
    }
    try {
        render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", 2, "p3", failed, "p4", 4)), "ns.foo");
        fail();
    } catch (Exception e) {
        assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(10);
    }
    try {
        render(templates, asRecord(ImmutableMap.of("p1", 1, "p2", 2, "p3", 3, "p4", failed)), "ns.foo");
        fail();
    } catch (Exception e) {
        assertThat(getTemplateLineNumber("ns.foo", e)).isEqualTo(11);
    }
}
Also used : CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) IOException(java.io.IOException) Test(org.junit.Test)

Example 32 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class BytecodeCompilerTest method compileFiles.

private CompiledTemplates compileFiles(String... soyFileContents) {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContents).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    return templates;
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 33 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates in project closure-templates by google.

the class BytecodeCompilerTest method testDelCallEscaping_separateCompilation.

// Tests for a bug where we would overescape deltemplates at the call site when the strict
// content kind of the deltemplate was unknown at compile time.
@Test
public void testDelCallEscaping_separateCompilation() throws IOException {
    String soyFileContent1 = Joiner.on("\n").join("{namespace ns}", "", "{template .callerTemplate}", "  {delcall myApp.myDelegate/}", "{/template}", "");
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent1).parse().fileSet();
    // apply an escaping directive to the callsite, just like the autoescaper would
    CallDelegateNode cdn = SoyTreeUtils.getAllNodesOfType(soyTree.getChild(0), CallDelegateNode.class).get(0);
    cdn.setEscapingDirectives(ImmutableList.of(new EscapeHtmlDirective()));
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
    CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
    CompiledTemplate.Factory caller = templates.getTemplateFactory("ns.callerTemplate");
    try {
        renderWithContext(caller, getDefaultContext(templates));
        fail();
    } catch (IllegalArgumentException iae) {
        assertThat(iae).hasMessageThat().isEqualTo("Found no active impl for delegate call to \"myApp.myDelegate\" (and delcall does " + "not set allowemptydefault=\"true\").");
    }
    String soyFileContent2 = Joiner.on("\n").join("{namespace ns2}", "", "{deltemplate myApp.myDelegate}", "  <span>Hello</span>", "{/deltemplate}", "");
    CompiledTemplates templatesWithDeltemplate = compileFiles(soyFileContent2);
    // By passing an alternate context, we ensure the deltemplate selector contains the delegate
    assertThat(renderWithContext(caller, getDefaultContext(templatesWithDeltemplate))).isEqualTo("<span>Hello</span>");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) EscapeHtmlDirective(com.google.template.soy.coredirectives.EscapeHtmlDirective) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CallDelegateNode(com.google.template.soy.soytree.CallDelegateNode) 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