Search in sources :

Example 36 with SoyFileSetNode

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

the class RewriteGlobalsPassTest method testResolveAlias.

@Test
public void testResolveAlias() {
    String template = "" + "{namespace ns}\n" + "\n" + "{alias foo.bar.baz as global}\n" + "{alias global.with.sugar}\n" + "\n" + "{template .t}\n" + "  {global}\n" + "  {global.with.field}\n" + "  {sugar}\n" + "  {sugar.with.field}\n" + "  {unregistered}\n" + "{/template}";
    SoyFileSetNode soytree = SoyFileSetParserBuilder.forFileContents(template).allowUnboundGlobals(true).parse().fileSet();
    ImmutableList.Builder<String> actual = ImmutableList.builder();
    for (SoyNode child : soytree.getChild(0).getChild(0).getChildren()) {
        actual.add(child.toSourceString());
    }
    assertThat(actual.build()).containsExactly("{foo.bar.baz}", "{foo.bar.baz.with.field}", "{global.with.sugar}", "{global.with.sugar.with.field}", "{unregistered}").inOrder();
}
Also used : SoyNode(com.google.template.soy.soytree.SoyNode) ImmutableList(com.google.common.collect.ImmutableList) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 37 with SoyFileSetNode

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

the class SimplifyVisitorTest method testCombineConsecutiveRawTextNodes.

@Test
public void testCombineConsecutiveRawTextNodes() throws Exception {
    String soyCode = "{@param boo : ?}\n" + "blah{$boo}blah" + "{for $i in range(5)}" + "  blah{$boo}blah" + "{/for}";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).parse().fileSet();
    TemplateNode template = soyTree.getChild(0).getChild(0);
    ForNonemptyNode forNode = (ForNonemptyNode) ((ForNode) template.getChild(3)).getChild(0);
    forNode.addChild(new RawTextNode(0, "bleh", SourceLocation.UNKNOWN));
    forNode.addChild(new RawTextNode(0, "bluh", SourceLocation.UNKNOWN));
    template.addChild(0, new RawTextNode(0, "bleh", SourceLocation.UNKNOWN));
    template.addChild(0, new RawTextNode(0, "bluh", SourceLocation.UNKNOWN));
    assertThat(template.numChildren()).isEqualTo(6);
    assertThat(forNode.numChildren()).isEqualTo(5);
    SimplifyVisitor simplifyVisitor = SimplifyVisitor.create();
    simplifyVisitor.simplify(soyTree, new TemplateRegistry(soyTree, ErrorReporter.exploding()));
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(forNode.numChildren()).isEqualTo(3);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("bluhblehblah");
    assertThat(((RawTextNode) forNode.getChild(2)).getRawText()).isEqualTo("blahblehbluh");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) ForNonemptyNode(com.google.template.soy.soytree.ForNonemptyNode) Test(org.junit.Test)

Example 38 with SoyFileSetNode

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

the class RenderVisitorTest method renderWithDataAndMsgBundle.

/**
 * Renders the given input string (should be a template body) and returns the result.
 *
 * @param templateBody The input string to render (should be a template body).
 * @param data The soy data as a map of variables to objects.
 * @param msgBundle The bundle of translated messages.
 * @return The rendered result.
 * @throws Exception If there's an error.
 */
private String renderWithDataAndMsgBundle(String templateBody, SoyRecord data, @Nullable SoyMsgBundle msgBundle) throws Exception {
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(templateBody).errorReporter(boom).parse().fileSet();
    TemplateNode templateNode = (TemplateNode) SharedTestUtils.getNode(soyTree);
    StringBuilder outputSb = new StringBuilder();
    RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), outputSb, null, data, TEST_IJ_DATA, Predicates.<String>alwaysFalse(), msgBundle, xidRenamingMap, cssRenamingMap, false);
    rv.exec(templateNode);
    return outputSb.toString();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

Example 39 with SoyFileSetNode

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

the class CombineConsecutiveRawTextNodesPassTest method testPathologicalPerformance.

// There used to be a pathological performance issue when merging many raw text nodes, this stress
// test would have timed out under the old implementation but now succeeds quickly.
// Before the fix this test took > 2 minutes
// After the fix it was down to about 1.5s
@Test
public void testPathologicalPerformance() {
    String testFileContent = "{namespace boo}{template .foo}{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = soyTree.getChild(0).getChild(0);
    // Things like this like this could happen in templates with a large number of html tags (e.g.
    // in a literal block). since this is how they would be desugared.
    final int numCopies = 100_000;
    for (int i = 0; i < numCopies; i++) {
        template.addChild(new RawTextNode(0, "<", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "div", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, " ", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "class", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "=", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "'", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "foo", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "'", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, ">", template.getSourceLocation()));
    }
    new CombineConsecutiveRawTextNodesPass().run(soyTree);
    assertThat(template.numChildren()).isEqualTo(1);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo(Strings.repeat("<div class='foo'>", numCopies));
}
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 40 with SoyFileSetNode

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

the class CombineConsecutiveRawTextNodesPassTest method testCombineConsecutiveRawTextNodes.

@Test
public void testCombineConsecutiveRawTextNodes() {
    String testFileContent = "{namespace boo}\n" + "\n" + "/** @param goo */\n" + "{template .foo}\n" + "  Blah{$goo}blah\n" + "{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    template.addChild(new RawTextNode(0, "bleh", template.getSourceLocation()));
    template.addChild(new RawTextNode(0, "bluh", template.getSourceLocation()));
    assertThat(template.numChildren()).isEqualTo(5);
    new CombineConsecutiveRawTextNodesPass().run(soyTree);
    assertThat(template.numChildren()).isEqualTo(3);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("Blah");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("blahblehbluh");
}
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) 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