Search in sources :

Example 6 with GlobalNode

use of com.google.template.soy.exprtree.GlobalNode in project closure-templates by google.

the class ParseExpressionTest method testParseProtoInitCall.

@Test
public void testParseProtoInitCall() throws Exception {
    ExprNode expr = assertThatExpression("my.Proto(a: 1, b: glo.bal, c: fn('str'))").isValidExpression();
    ProtoInitNode protoFn = (ProtoInitNode) expr;
    assertThat(protoFn.getProtoName()).isEqualTo("my.Proto");
    assertThat(protoFn.getParamNames()).containsExactly("a", "b", "c").inOrder();
    assertThat(protoFn.numChildren()).isEqualTo(3);
    assertThat(((IntegerNode) protoFn.getChild(0)).getValue()).isEqualTo(1);
    assertThat(((GlobalNode) protoFn.getChild(1)).getName()).isEqualTo("glo.bal");
    assertThat(((StringNode) ((FunctionNode) protoFn.getChild(2)).getChild(0)).getValue()).isEqualTo("str");
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) ProtoInitNode(com.google.template.soy.exprtree.ProtoInitNode) StringNode(com.google.template.soy.exprtree.StringNode) GlobalNode(com.google.template.soy.exprtree.GlobalNode) Test(org.junit.Test)

Example 7 with GlobalNode

use of com.google.template.soy.exprtree.GlobalNode in project closure-templates by google.

the class TemplateNodeTest method testValidVariant.

@Test
public void testValidVariant() {
    // Variant is a string literal: There's no expression and the value is already resolved.
    TemplateDelegateNode node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"'abc'\"}", "{/deltemplate}"));
    assertEquals("namespace.boo", node.getDelTemplateName());
    assertEquals("abc", node.getDelTemplateVariant());
    assertEquals("abc", node.getDelTemplateKey().variant());
    // Variant is a global, that was not yet resolved.
    node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"test.GLOBAL_CONSTANT\"}", "{/deltemplate}"));
    assertEquals("namespace.boo", node.getDelTemplateName());
    assertEquals("test.GLOBAL_CONSTANT", node.getDelTemplateVariant());
    assertEquals("test.GLOBAL_CONSTANT", node.getDelTemplateKey().variant());
    // Verify the global expression.
    List<ExprRootNode> exprs = node.getExprList();
    assertEquals(1, exprs.size());
    ExprRootNode expr = exprs.get(0);
    assertEquals("test.GLOBAL_CONSTANT", expr.toSourceString());
    assertEquals(1, expr.numChildren());
    assertTrue(expr.getRoot() instanceof GlobalNode);
    // Substitute the global expression.
    expr.replaceChild(0, new IntegerNode(123, expr.getRoot().getSourceLocation()));
    // Check the new values.
    assertEquals("123", node.getDelTemplateVariant());
    assertEquals("123", node.getDelTemplateKey().variant());
    // Resolve a global to a string.
    node = (TemplateDelegateNode) parse(join("{namespace ns}", "{deltemplate namespace.boo variant=\"test.GLOBAL_CONSTANT\"}", "{/deltemplate}"));
    node.getExprList().get(0).replaceChild(0, new StringNode("variant", QuoteStyle.SINGLE, node.getSourceLocation()));
    assertEquals("variant", node.getDelTemplateVariant());
    assertEquals("variant", node.getDelTemplateKey().variant());
}
Also used : IntegerNode(com.google.template.soy.exprtree.IntegerNode) StringNode(com.google.template.soy.exprtree.StringNode) GlobalNode(com.google.template.soy.exprtree.GlobalNode) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode) Test(org.junit.Test)

Example 8 with GlobalNode

use of com.google.template.soy.exprtree.GlobalNode in project closure-templates by google.

the class ExpressionSubject method isValidGlobalNamed.

void isValidGlobalNamed(String name) {
    GlobalNode globalNode = (GlobalNode) parseExpression();
    if (errorReporter.hasErrors()) {
        fail("is valid global", errorReporter.getErrors());
    }
    String actualName = globalNode.getName();
    if (!actualName.equals(name)) {
        failWithBadResults("is global named", name, "has name", actualName);
    }
    String actualSourceString = globalNode.toSourceString();
    if (!actualSourceString.equals(name)) {
        failWithBadResults("is global named", name, "has source string", actualSourceString);
    }
}
Also used : GlobalNode(com.google.template.soy.exprtree.GlobalNode)

Aggregations

GlobalNode (com.google.template.soy.exprtree.GlobalNode)8 ExprNode (com.google.template.soy.exprtree.ExprNode)4 IntegerNode (com.google.template.soy.exprtree.IntegerNode)4 StringNode (com.google.template.soy.exprtree.StringNode)4 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)2 ProtoInitNode (com.google.template.soy.exprtree.ProtoInitNode)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1 PrimitiveData (com.google.template.soy.data.restricted.PrimitiveData)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 DataAccessNode (com.google.template.soy.exprtree.DataAccessNode)1 PrimitiveNode (com.google.template.soy.exprtree.ExprNode.PrimitiveNode)1 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1