Search in sources :

Example 1 with FloatNode

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

the class InternalValueUtilsTest method testConvertPrimitiveExprToData.

@Test
public void testConvertPrimitiveExprToData() {
    assertTrue(InternalValueUtils.convertPrimitiveExprToData(new NullNode(SourceLocation.UNKNOWN)) instanceof NullData);
    assertTrue(InternalValueUtils.convertPrimitiveExprToData(new BooleanNode(true, SourceLocation.UNKNOWN)).booleanValue());
    assertEquals(-1, InternalValueUtils.convertPrimitiveExprToData(new IntegerNode(-1, SourceLocation.UNKNOWN)).integerValue());
    assertEquals(6.02e23, InternalValueUtils.convertPrimitiveExprToData(new FloatNode(6.02e23, SourceLocation.UNKNOWN)).floatValue(), 0.0);
    assertEquals("foo", InternalValueUtils.convertPrimitiveExprToData(new StringNode("foo", QuoteStyle.SINGLE, SourceLocation.UNKNOWN)).stringValue());
}
Also used : NullData(com.google.template.soy.data.restricted.NullData) 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) NullNode(com.google.template.soy.exprtree.NullNode) Test(org.junit.Test)

Example 2 with FloatNode

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

the class ParseExpressionTest method testParseFunctionCall.

@Test
public void testParseFunctionCall() throws Exception {
    ExprNode expr = assertThatExpression("isFirst($x)").isValidExpression();
    FunctionNode isFirstFn = (FunctionNode) expr;
    assertThat(isFirstFn.getFunctionName()).isEqualTo("isFirst");
    assertThat(isFirstFn.numChildren()).isEqualTo(1);
    assertThat(isFirstFn.getChild(0).toSourceString()).isEqualTo("$x");
    expr = assertThatExpression("round(3.14159, 2)").isValidExpression();
    FunctionNode roundFn = (FunctionNode) expr;
    assertThat(roundFn.getFunctionName()).isEqualTo("round");
    assertThat(roundFn.numChildren()).isEqualTo(2);
    assertThat(((FloatNode) roundFn.getChild(0)).getValue()).isEqualTo(3.14159);
    assertThat(((IntegerNode) roundFn.getChild(1)).getValue()).isEqualTo(2);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) FloatNode(com.google.template.soy.exprtree.FloatNode) Test(org.junit.Test)

Example 3 with FloatNode

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

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

Aggregations

FloatNode (com.google.template.soy.exprtree.FloatNode)4 IntegerNode (com.google.template.soy.exprtree.IntegerNode)4 Test (org.junit.Test)4 BooleanNode (com.google.template.soy.exprtree.BooleanNode)3 ExprNode (com.google.template.soy.exprtree.ExprNode)3 StringNode (com.google.template.soy.exprtree.StringNode)2 NullData (com.google.template.soy.data.restricted.NullData)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 NullNode (com.google.template.soy.exprtree.NullNode)1 ConditionalOpNode (com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode)1 MinusOpNode (com.google.template.soy.exprtree.OperatorNodes.MinusOpNode)1 NotOpNode (com.google.template.soy.exprtree.OperatorNodes.NotOpNode)1 NullCoalescingOpNode (com.google.template.soy.exprtree.OperatorNodes.NullCoalescingOpNode)1 OrOpNode (com.google.template.soy.exprtree.OperatorNodes.OrOpNode)1