Search in sources :

Example 31 with ExprNode

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

the class ParseExpressionTest method testParsePrimitives.

@Test
public void testParsePrimitives() throws Exception {
    ExprNode expr = assertThatExpression("null").isValidExpression();
    assertThat(expr).isInstanceOf(NullNode.class);
    expr = assertThatExpression("true").isValidExpression();
    assertThat(((BooleanNode) expr).getValue()).isTrue();
    expr = assertThatExpression("false").isValidExpression();
    assertThat(((BooleanNode) expr).getValue()).isFalse();
    expr = assertThatExpression("26").isValidExpression();
    assertThat(((IntegerNode) expr).getValue()).isEqualTo(26);
    expr = assertThatExpression("0xCAFE").isValidExpression();
    assertThat(((IntegerNode) expr).getValue()).isEqualTo(0xCAFE);
    expr = assertThatExpression("3.14").isValidExpression();
    assertThat(((FloatNode) expr).getValue()).isEqualTo(3.14);
    expr = assertThatExpression("3e-3").isValidExpression();
    assertThat(((FloatNode) expr).getValue()).isEqualTo(3e-3);
    expr = assertThatExpression("'Aa`! \\n \\r \\t \\\\ \\\' \"'").isValidExpression();
    assertThat(((StringNode) expr).getValue()).isEqualTo("Aa`! \n \r \t \\ \' \"");
    expr = assertThatExpression("'\\u2222 \\uEEEE \\u9EC4 \\u607A'").isValidExpression();
    assertThat(((StringNode) expr).getValue()).isEqualTo("\u2222 \uEEEE \u9EC4 \u607A");
    expr = assertThatExpression("'\u2222 \uEEEE \u9EC4 \u607A'").isValidExpression();
    assertThat(((StringNode) expr).getValue()).isEqualTo("\u2222 \uEEEE \u9EC4 \u607A");
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FloatNode(com.google.template.soy.exprtree.FloatNode) StringNode(com.google.template.soy.exprtree.StringNode) BooleanNode(com.google.template.soy.exprtree.BooleanNode) Test(org.junit.Test)

Example 32 with ExprNode

use of com.google.template.soy.exprtree.ExprNode 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 33 with ExprNode

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

the class ParseExpressionTest method testParseDataReference.

@Test
public void testParseDataReference() throws Exception {
    SourceLocation loc = SourceLocation.UNKNOWN;
    ExprNode dataRef = assertThatExpression("$boo").isValidExpression();
    assertNodeEquals(new VarRefNode("boo", loc, false, null), dataRef);
    dataRef = assertThatExpression("$boo.foo").isValidExpression();
    assertNodeEquals(new FieldAccessNode(new VarRefNode("boo", loc, false, null), "foo", loc, false), dataRef);
    dataRef = assertThatExpression("$boo[0][$foo]").isValidExpression();
    assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, false, null), new IntegerNode(0, loc), loc, false), new VarRefNode("foo", loc, false, null), loc, false), dataRef);
    dataRef = assertThatExpression("$boo?[0]?[$foo]").isValidExpression();
    assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, false, null), new IntegerNode(0, loc), loc, true), new VarRefNode("foo", loc, false, null), loc, true), dataRef);
    dataRef = assertThatExpression("$ij.boo?[0][$ij.foo]").isValidExpression();
    assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, true, null), new IntegerNode(0, loc), loc, true), new VarRefNode("foo", loc, true, null), loc, false), dataRef);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ExprNode(com.google.template.soy.exprtree.ExprNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) ItemAccessNode(com.google.template.soy.exprtree.ItemAccessNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) Test(org.junit.Test)

Example 34 with ExprNode

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

the class ParseExpressionTest method testParseOperators.

@Test
public void testParseOperators() throws Exception {
    ExprNode expr = assertThatExpression("-11").isValidExpression();
    IntegerNode negInt = (IntegerNode) expr;
    assertThat(negInt.getValue()).isEqualTo(-11);
    expr = assertThatExpression("not false").isValidExpression();
    NotOpNode notOp = (NotOpNode) expr;
    assertThat(((BooleanNode) notOp.getChild(0)).getValue()).isFalse();
    expr = assertThatExpression("90 -14.75").isValidExpression();
    MinusOpNode minusOp = (MinusOpNode) expr;
    assertThat(((IntegerNode) minusOp.getChild(0)).getValue()).isEqualTo(90);
    assertThat(((FloatNode) minusOp.getChild(1)).getValue()).isEqualTo(14.75);
    expr = assertThatExpression("$a or true").isValidExpression();
    OrOpNode orOp = (OrOpNode) expr;
    assertThat(orOp.getChild(0).toSourceString()).isEqualTo("$a");
    assertThat(((BooleanNode) orOp.getChild(1)).getValue()).isTrue();
    expr = assertThatExpression("$a ?: $b ?: $c").isValidExpression();
    NullCoalescingOpNode nullCoalOp0 = (NullCoalescingOpNode) expr;
    assertThat(nullCoalOp0.getChild(0).toSourceString()).isEqualTo("$a");
    NullCoalescingOpNode nullCoalOp1 = (NullCoalescingOpNode) nullCoalOp0.getChild(1);
    assertThat(nullCoalOp1.getChild(0).toSourceString()).isEqualTo("$b");
    assertThat(nullCoalOp1.getChild(1).toSourceString()).isEqualTo("$c");
    expr = assertThatExpression("$a?:$b==null?0*1:0x1").isValidExpression();
    NullCoalescingOpNode nullCoalOp = (NullCoalescingOpNode) expr;
    assertThat(nullCoalOp.getChild(0).toSourceString()).isEqualTo("$a");
    ConditionalOpNode condOp = (ConditionalOpNode) nullCoalOp.getChild(1);
    assertThat(condOp.getChild(0)).isInstanceOf(EqualOpNode.class);
    assertThat(condOp.getChild(1)).isInstanceOf(TimesOpNode.class);
    assertThat(condOp.getChild(2)).isInstanceOf(IntegerNode.class);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ConditionalOpNode(com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode) NullCoalescingOpNode(com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) MinusOpNode(com.google.template.soy.exprtree.OperatorNodes.MinusOpNode) OrOpNode(com.google.template.soy.exprtree.OperatorNodes.OrOpNode) FloatNode(com.google.template.soy.exprtree.FloatNode) NotOpNode(com.google.template.soy.exprtree.OperatorNodes.NotOpNode) BooleanNode(com.google.template.soy.exprtree.BooleanNode) Test(org.junit.Test)

Example 35 with ExprNode

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

the class MsgSubstUnitBaseVarNameUtilsTest method assertNaiveBaseNameForExpr.

private void assertNaiveBaseNameForExpr(String expected, String exprText) {
    ExprNode exprRoot = parse(exprText);
    String actual = MsgSubstUnitBaseVarNameUtils.genNaiveBaseNameForExpr(exprRoot, "FALLBACK");
    assertEquals(expected, actual);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode)

Aggregations

ExprNode (com.google.template.soy.exprtree.ExprNode)38 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)7 IntegerNode (com.google.template.soy.exprtree.IntegerNode)7 StringNode (com.google.template.soy.exprtree.StringNode)7 FunctionNode (com.google.template.soy.exprtree.FunctionNode)5 Test (org.junit.Test)5 ImmutableList (com.google.common.collect.ImmutableList)4 SoyValue (com.google.template.soy.data.SoyValue)4 GlobalNode (com.google.template.soy.exprtree.GlobalNode)4 VarRefNode (com.google.template.soy.exprtree.VarRefNode)4 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)4 SoyType (com.google.template.soy.types.SoyType)4 LinkedHashMap (java.util.LinkedHashMap)4 AbstractParentExprNode (com.google.template.soy.exprtree.AbstractParentExprNode)3 FloatNode (com.google.template.soy.exprtree.FloatNode)3 SourceLocation (com.google.template.soy.base.SourceLocation)2 ErrorReporter (com.google.template.soy.error.ErrorReporter)2 BooleanNode (com.google.template.soy.exprtree.BooleanNode)2 PrimitiveNode (com.google.template.soy.exprtree.ExprNode.PrimitiveNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2