Search in sources :

Example 36 with TemplateNode

use of com.google.template.soy.soytree.TemplateNode 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 37 with TemplateNode

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

the class RenderVisitorTest method renderTemplateInFile.

private String renderTemplateInFile(ParseResult parseResult, String templateName, SoyRecord data, SoyRecord ijData, Predicate<String> activeDelPackageNames, StringBuilder outputSb) {
    TemplateRegistry templateRegistry = parseResult.registry();
    RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), outputSb, templateRegistry, data, ijData, activeDelPackageNames, null, xidRenamingMap, cssRenamingMap, false);
    TemplateNode templateNode = templateRegistry.getBasicTemplate(templateName);
    rv.exec(templateNode);
    return outputSb.toString();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry)

Example 38 with TemplateNode

use of com.google.template.soy.soytree.TemplateNode 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 39 with TemplateNode

use of com.google.template.soy.soytree.TemplateNode 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)

Example 40 with TemplateNode

use of com.google.template.soy.soytree.TemplateNode 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)

Aggregations

TemplateNode (com.google.template.soy.soytree.TemplateNode)89 Test (org.junit.Test)59 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)19 ErrorReporter (com.google.template.soy.error.ErrorReporter)11 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)10 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)9 RawTextNode (com.google.template.soy.soytree.RawTextNode)9 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)8 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)7 FunctionNode (com.google.template.soy.exprtree.FunctionNode)6 PrintNode (com.google.template.soy.soytree.PrintNode)5 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 SourceLocation (com.google.template.soy.base.SourceLocation)4 StringNode (com.google.template.soy.exprtree.StringNode)4 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 ImmutableList (com.google.common.collect.ImmutableList)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)3 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)3