Search in sources :

Example 91 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class AddDebugAttributesPassTest method assertRewritten.

/**
 * Returns the contextually rewritten and injected source.
 *
 * <p>The Soy tree may have multiple files, but only the source code for the first is returned.
 */
private void assertRewritten(String expectedOutput, String input) {
    String namespace = "{namespace ns}\n\n";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(namespace + input).addHtmlAttributesForDebugging(true).parse().fileSet();
    StringBuilder src = new StringBuilder();
    src.append(soyTree.getChild(0).toSourceString());
    String output = src.toString().trim();
    if (output.startsWith("{namespace ns")) {
        output = output.substring(output.indexOf('}') + 1).trim();
    }
    assertThat(output).isEqualTo(expectedOutput);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

Example 92 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class AddHtmlCommentsForDebugPassTest method runPass.

/**
 * Parses the given input as a Soy file content, runs the AddHtmlCommentsForDebug pass and returns
 * a map that contains pairs of template names and rewritten template body.
 */
private static ImmutableMap<String, String> runPass(String input, String fileName) {
    String soyFile = "{namespace ns}" + input;
    IncrementingIdGenerator nodeIdGen = new IncrementingIdGenerator();
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soyFile, SoyFileKind.SRC, fileName)).errorReporter(boom).parse().fileSet();
    TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
    // We need to run HtmlRewritePass to produce HTML nodes. Otherwise we will not add any comments.
    new HtmlRewritePass(boom).run(soyTree.getChild(0), nodeIdGen);
    new AddHtmlCommentsForDebugPass().run(soyTree, registry);
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (TemplateNode node : soyTree.getChild(0).getChildren()) {
        StringBuilder sb = new StringBuilder();
        node.appendSourceStringForChildren(sb);
        String templateName = (node instanceof TemplateDelegateNode) ? ((TemplateDelegateNode) node).getDelTemplateName() : node.getTemplateName();
        builder.put(templateName, sb.toString());
    }
    return builder.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 93 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class VeLogInstrumentationVisitorTest method testLoggingFunctionsInsertingLetBlocks.

@Test
public void testLoggingFunctionsInsertingLetBlocks() {
    // Test for using print node as attribute name.
    assertThatSourceString(runPass("{let $foo : 'data-ved' /}" + "{velog Bar}<div><span {$foo}={currentVed()}></span></div>{/velog}")).isEqualTo("{let $foo : 'data-ved' /}{velog Bar}" + "<div{$$velog(2, null)}>" + "<span" + "{let $soy_logging_function_attribute_0}{$foo}{/let} " + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}" + "></span>" + "</div>" + "{/velog}");
    // Test for multiple logging functions.
    assertThatSourceString(runPass("{@param foo: string}{@param bar: string}" + "{velog Bar}<div>" + "<span {$foo}={currentVed()} {$bar}={currentVed(1)}></span>" + "</div>{/velog}")).isEqualTo("{velog Bar}" + "<div{$$velog(2, null)}>" + "<span" + "{let $soy_logging_function_attribute_0}{$foo}{/let} " + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}" + "{let $soy_logging_function_attribute_1}{$bar}{/let} " + "{$soy_logging_function_attribute_1}=\"placeholder\"" + "{$$loggingFunction('currentVed', [1], $soy_logging_function_attribute_1, 1)}" + ">" + "</span>" + "</div>" + "{/velog}");
    // Test that counters work fine for nested tags.
    assertThatSourceString(runPass("{@param foo: string}{@param bar: string}" + "{velog Bar}<div>" + "<span " + "{$foo}={currentVed()} " + "{let $baz kind=\"html\"}<input>{/let} " + "{$bar}={currentVed(1)}" + "></span>" + "</div>{/velog}")).isEqualTo("{velog Bar}" + "<div{$$velog(2, null)}>" + "<span" + "{let $soy_logging_function_attribute_0}{$foo}{/let} " + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}" + "{let $baz kind=\"html\"}<input>{/let}" + "{let $soy_logging_function_attribute_1}{$bar}{/let} " + "{$soy_logging_function_attribute_1}=\"placeholder\"" + "{$$loggingFunction('currentVed', [1], $soy_logging_function_attribute_1, 1)}" + ">" + "</span>" + "</div>" + "{/velog}");
    // Test for calling another template.
    SoyFileSetNode node = runPass("{velog Bar}<div>" + "<span {call .attr}{param foo : 'data-ved' /}{/call}></span>" + "</div>{/velog}" + "{/template}" + "" + "{template .attr kind=\"attributes\"}" + "{@param foo : string}" + "{$foo}={currentVed()}");
    StringBuilder sb = new StringBuilder();
    node.getChild(0).getChild(0).appendSourceStringForChildren(sb);
    assertThat(sb.toString()).isEqualTo("{velog Bar}" + "<div{$$velog(2, null)}>" + "<span {call .attr}{param foo : 'data-ved' /}{/call}>" + "</span>" + "</div>" + "{/velog}");
    sb = new StringBuilder();
    node.getChild(0).getChild(1).appendSourceStringForChildren(sb);
    assertThat(sb.toString()).isEqualTo("{let $soy_logging_function_attribute_0}{$foo}{/let}" + "{$soy_logging_function_attribute_0}=\"placeholder\"" + "{$$loggingFunction('currentVed', [], $soy_logging_function_attribute_0, 0)}");
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 94 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class InsertMsgsVisitorTest method testFallbackMsgsUsingSoySource.

@Test
public void testFallbackMsgsUsingSoySource() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(FALLBACK_TEST_FILE_CONTENT).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    // Before.
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(((MsgFallbackGroupNode) template.getChild(0)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(1)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(2)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(3)).numChildren()).isEqualTo(2);
    // Execute the visitor.
    new InsertMsgsVisitor(null, /* msgBundle */
    FAIL).insertMsgs(template);
    // After.
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("noTrans1");
    assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("trans1");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("noTrans1");
    assertThat(((RawTextNode) template.getChild(3)).getRawText()).isEqualTo("trans1");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) Test(org.junit.Test)

Example 95 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class InsertMsgsVisitorTest method testBasicMsgsUsingSoySource.

@Test
public void testBasicMsgsUsingSoySource() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(BASIC_TEST_FILE_CONTENT).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    // Before.
    assertThat(template.numChildren()).isEqualTo(5);
    MsgNode msg = ((MsgFallbackGroupNode) template.getChild(2)).getChild(0);
    assertThat(msg.numChildren()).isEqualTo(5);
    MsgHtmlTagNode msgHtmlTag2 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(2)).getChild(0);
    assertThat(msgHtmlTag2.numChildren()).isEqualTo(3);
    MsgHtmlTagNode msgHtmlTag4 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(4)).getChild(0);
    assertThat(msgHtmlTag4.numChildren()).isEqualTo(1);
    assertThat(((RawTextNode) msgHtmlTag4.getChild(0)).getRawText()).isEqualTo("</a>");
    // Execute the visitor.
    new InsertMsgsVisitor(null, /* msgBundle */
    FAIL).insertMsgs(template);
    // After.
    assertThat(template.numChildren()).isEqualTo(12);
    assertThat(((PrintNode) template.getChild(0)).getExpr().toSourceString()).isEqualTo("$boo");
    assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("scary ");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("random");
    assertThat(((PrintNode) template.getChild(3)).getExpr().toSourceString()).isEqualTo("$foo");
    assertThat(((RawTextNode) template.getChild(4)).getRawText()).isEqualTo("<a href=\"");
    assertThat(((PrintNode) template.getChild(5)).getExpr().toSourceString()).isEqualTo("$goo");
    assertThat(((RawTextNode) template.getChild(6)).getRawText()).isEqualTo("\">");
    assertThat(((RawTextNode) template.getChild(7)).getRawText()).isEqualTo("slimy");
    assertThat(((RawTextNode) template.getChild(8)).getRawText()).isEqualTo("</a>");
    assertThat(((RawTextNode) template.getChild(9)).getRawText()).isEqualTo(" ");
    assertThat(((RawTextNode) template.getChild(10)).getRawText()).isEqualTo("dairy");
    assertThat(((PrintNode) template.getChild(11)).getExpr().toSourceString()).isEqualTo("$moo");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Aggregations

SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)106 Test (org.junit.Test)81 TemplateNode (com.google.template.soy.soytree.TemplateNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)29 ErrorReporter (com.google.template.soy.error.ErrorReporter)19 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)13 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 RawTextNode (com.google.template.soy.soytree.RawTextNode)8 MsgNode (com.google.template.soy.soytree.MsgNode)7 SoyNode (com.google.template.soy.soytree.SoyNode)6 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)5 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)5 PrintNode (com.google.template.soy.soytree.PrintNode)5 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)4 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 PluginResolver (com.google.template.soy.soyparse.PluginResolver)3 ImmutableList (com.google.common.collect.ImmutableList)2