Search in sources :

Example 41 with SoyFileSetNode

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

the class CombineConsecutiveRawTextNodesPassTest method testCombineConsecutiveRawTextNodes_preserveSourceLocations.

@Test
public void testCombineConsecutiveRawTextNodes_preserveSourceLocations() {
    String testFileContent = "{namespace boo}{template .foo}\nbl{nil}ah\n{/template}";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    assertThat(template.numChildren()).isEqualTo(1);
    RawTextNode node = (RawTextNode) template.getChild(0);
    assertThat(node.getRawText()).isEqualTo("blah");
    assertThat(node.getSourceLocation().getBeginPoint()).isEqualTo(Point.create(2, 1));
    assertThat(node.getSourceLocation().getEndPoint()).isEqualTo(Point.create(2, 9));
    // we also know the locations of individual characters
    assertThat(node.locationOf(2)).isEqualTo(Point.create(2, 8));
    // split it up into 1 node per character
    // arbitrary
    int newId = 1;
    RawTextNode c1 = node.substring(newId, 0, 1);
    RawTextNode c2 = node.substring(newId, 1, 2);
    RawTextNode c3 = node.substring(newId, 2, 3);
    RawTextNode c4 = node.substring(newId, 3, 4);
    template.removeChild(node);
    template.addChildren(Arrays.asList(c1, c2, c3, c4));
    assertThat(template.numChildren()).isEqualTo(4);
    new CombineConsecutiveRawTextNodesPass().run(soyTree);
    assertThat(template.numChildren()).isEqualTo(1);
    node = (RawTextNode) template.getChild(0);
    // all the data is preserved across the join operation
    assertThat(node.getRawText()).isEqualTo("blah");
    assertThat(node.getSourceLocation().getBeginPoint()).isEqualTo(Point.create(2, 1));
    assertThat(node.getSourceLocation().getEndPoint()).isEqualTo(Point.create(2, 9));
    assertThat(node.locationOf(2)).isEqualTo(Point.create(2, 8));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) Point(com.google.template.soy.base.SourceLocation.Point) Test(org.junit.Test)

Example 42 with SoyFileSetNode

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

the class ClearSoyDocStringsVisitorTest method testClearSoyDocStrings.

@Test
public void testClearSoyDocStrings() {
    String testFileContent = "{namespace boo}\n" + "\n" + "/**\n" + " * blah blah blah\n" + " *\n" + " * @param goo blah blah\n" + " */\n" + "{template .foo}\n" + "  {$goo}\n" + "{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    assertThat(template.getSoyDoc()).contains("blah");
    assertThat(template.getSoyDocDesc()).contains("blah");
    assertThat(template.getParams().get(0).desc()).contains("blah");
    new ClearSoyDocStringsVisitor().exec(soyTree);
    assertThat(template.getSoyDoc()).isNull();
    assertThat(template.getSoyDocDesc()).isNull();
    assertThat(template.getParams().get(0).desc()).isNull();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 43 with SoyFileSetNode

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

the class ContentSecurityPolicyNonceInjectionPassTest method assertInjected.

/**
 * 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 assertInjected(String expectedOutput, String input) {
    String namespace = "{namespace ns}\n\n";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(namespace + input).runAutoescaper(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 44 with SoyFileSetNode

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

the class FindIjParamsVisitorTest method testTwoPathsToSameTemplate.

@Test
public void testTwoPathsToSameTemplate() {
    // aaa -> {bbb, ccc}, ccc -> bbb.
    String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  {call .bbb /} {$ij.boo} {call .ccc /} {$ij.foo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + "  {$ij.boo} {$ij.goo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + "  {$ij.boo} {$ij.moo + $ij.woo} {call .bbb /}\n" + "{/template}\n";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(fileContent).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, FAIL);
    TemplateNode aaa = soyTree.getChild(0).getChild(0);
    TemplateNode bbb = soyTree.getChild(0).getChild(1);
    TemplateNode ccc = soyTree.getChild(0).getChild(2);
    // Test with exec(aaa).
    // Exercises: processCalleeHelper case 4 with incorporateCalleeVisitInfo case 1 (ccc -> bbb).
    FindIjParamsVisitor visitor = new FindIjParamsVisitor(templateRegistry);
    visitor.exec(aaa);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(2);
    assertThat(visitor.exec(ccc).ijParamToCalleesMultimap).hasSize(5);
    assertThat(visitor.exec(ccc).ijParamToCalleesMultimap.keySet()).hasSize(4);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap).hasSize(7);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap.keySet()).hasSize(5);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 45 with SoyFileSetNode

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

the class FindIjParamsVisitorTest method testTwoPathsToSameRecursiveCycle.

@Test
public void testTwoPathsToSameRecursiveCycle() {
    // aaa -> {bbb, ccc}, bbb -> ddd, ccc -> ddd, ddd -> bbb.
    String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  {$ij.boo} {$ij.foo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + "  {$ij.boo} {$ij.goo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + "  {$ij.boo} {$ij.moo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ddd}\n" + "  {$ij.boo} {$ij.too} {call .bbb /}\n" + "{/template}\n";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(fileContent).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, FAIL);
    TemplateNode aaa = soyTree.getChild(0).getChild(0);
    TemplateNode bbb = soyTree.getChild(0).getChild(1);
    TemplateNode ccc = soyTree.getChild(0).getChild(2);
    TemplateNode ddd = soyTree.getChild(0).getChild(3);
    // Test with exec(aaa).
    // Exercises: processCalleeHelper case 4 with incorporateCalleeVisitInfo case 4 (ccc -> ddd).
    FindIjParamsVisitor visitor = new FindIjParamsVisitor(templateRegistry);
    visitor.exec(aaa);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(4);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap.keySet()).hasSize(3);
    assertThat(visitor.exec(ddd).ijParamToCalleesMultimap).hasSize(4);
    assertThat(visitor.exec(ddd).ijParamToCalleesMultimap.keySet()).hasSize(3);
    assertThat(visitor.exec(ccc).ijParamToCalleesMultimap).hasSize(6);
    assertThat(visitor.exec(ccc).ijParamToCalleesMultimap.keySet()).hasSize(4);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap).hasSize(8);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap.keySet()).hasSize(5);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) 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