Search in sources :

Example 21 with PrintNode

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

the class ResolvePackageRelativeCssNamesVisitorTest method testUnprefixedNode.

@Test
public void testUnprefixedNode() {
    TemplateNode template = compileTemplate("{namespace boo cssbase=\"some.test.package\"}\n\n" + "/** Test template. */\n" + "{template .foo}\n" + "  <p class=\"{css('AAA')}\">\n" + "{/template}\n");
    PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
    FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
    assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("AAA");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 22 with PrintNode

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

the class InferenceEngineTest method assertTransitions.

private static void assertTransitions(SanitizedContentKind kind, String src) {
    TemplateNode template = SoyFileSetParserBuilder.forFileContents("{namespace ns}\n{template .foo" + (kind == SanitizedContentKind.HTML ? "" : " kind=\"" + Ascii.toLowerCase(kind.toString()) + '"') + " stricthtml=\"false\"}" + src + "{/template}").addSoyFunction(new AssertFunction()).desugarHtmlNodes(false).parse().fileSet().getChild(0).getChild(0);
    Inferences inferences = new Inferences(new IncrementingIdGenerator(), ImmutableListMultimap.<String, TemplateNode>of());
    InferenceEngine.inferTemplateEndContext(template, Context.getStartContextForContentKind(kind), inferences, ErrorReporter.exploding());
    for (PrintNode print : SoyTreeUtils.getAllNodesOfType(template, PrintNode.class)) {
        if (print.getExpr().getChild(0) instanceof FunctionNode) {
            FunctionNode fn = (FunctionNode) print.getExpr().getChild(0);
            if (fn.getSoyFunction() instanceof AssertFunction) {
                assertWithMessage("expected print node at " + print.getSourceLocation() + " to have context").that(contextString(inferences.getContextForNode(print))).isEqualTo(((StringNode) fn.getChild(0)).getValue());
            }
        }
    }
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) PrintNode(com.google.template.soy.soytree.PrintNode)

Example 23 with PrintNode

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

the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnWithEscapeHtml.

@Test
public void testAutoescapeOnWithEscapeHtml() throws Exception {
    String testPrintTags = "{'<br>' |escapeHtml}{'<br>' |noAutoescape |escapeHtml}{'<br>' |escapeHtml |noAutoescape}";
    SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
    List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
    // Before.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChildren()).hasSize(2);
    assertThat(printNodes.get(2).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    performAutoescape(soyTree);
    // After. Note that noAutoescape remains to filter against ContentKind.TEXT.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChildren()).hasSize(2);
    assertThat(printNodes.get(2).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 24 with PrintNode

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

the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnWithNoAutoescape.

@Test
public void testAutoescapeOnWithNoAutoescape() throws Exception {
    String testPrintTags = "{'<br>' |noAutoescape}{'<br>' |noAutoescape |noAutoescape}";
    SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
    List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
    // Before.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    performAutoescape(soyTree);
    // After. Note that noAutoescape remains to filter against ContentKind.TEXT.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 25 with PrintNode

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

the class PreevalVisitorTest method preeval.

// -----------------------------------------------------------------------------------------------
// Helpers.
/**
 * Evaluates the given expression and returns the result.
 *
 * @param expression The expression to preevaluate.
 * @return The expression result.
 */
private static SoyValue preeval(String expression, String... params) {
    String header = "";
    for (String param : params) {
        header += "{@param " + param + " : ?}\n";
    }
    PrintNode code = (PrintNode) SoyFileSetParserBuilder.forTemplateContents(header + "{" + expression + "}").parse().fileSet().getChild(0).getChild(0).getChild(0);
    ExprRootNode expr = code.getExpr();
    Environment env = TestingEnvironment.createForTest(SoyValueConverterUtility.newDict("boo", 8), ImmutableMap.<String, SoyValueProvider>of());
    return new PreevalVisitor(env).exec(expr);
}
Also used : TestingEnvironment(com.google.template.soy.sharedpasses.render.TestingEnvironment) Environment(com.google.template.soy.sharedpasses.render.Environment) PrintNode(com.google.template.soy.soytree.PrintNode) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Aggregations

PrintNode (com.google.template.soy.soytree.PrintNode)25 Test (org.junit.Test)15 FunctionNode (com.google.template.soy.exprtree.FunctionNode)8 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)8 StringNode (com.google.template.soy.exprtree.StringNode)6 TemplateNode (com.google.template.soy.soytree.TemplateNode)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 RawTextNode (com.google.template.soy.soytree.RawTextNode)4 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)3 IfCondNode (com.google.template.soy.soytree.IfCondNode)3 IfNode (com.google.template.soy.soytree.IfNode)3 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)3 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 SourceLocation (com.google.template.soy.base.SourceLocation)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 IntegerNode (com.google.template.soy.exprtree.IntegerNode)2 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)2 SoyFunction (com.google.template.soy.shared.restricted.SoyFunction)2