Search in sources :

Example 1 with MinusOpNode

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

the class AbstractExprNodeVisitorTest method testNotImplemented.

@Test
public void testNotImplemented() {
    MinusOpNode expr = new MinusOpNode(LOC);
    expr.addChild(new FloatNode(17.0, LOC));
    VarRefNode dataRef = new VarRefNode("boo", LOC, false, null);
    expr.addChild(dataRef);
    IncompleteEvalVisitor iev = new IncompleteEvalVisitor(ImmutableMap.of("boo", 13.0));
    try {
        iev.exec(expr);
        fail();
    } catch (UnsupportedOperationException uoe) {
    // Test passes.
    }
}
Also used : MinusOpNode(com.google.template.soy.exprtree.OperatorNodes.MinusOpNode) Test(org.junit.Test)

Example 2 with MinusOpNode

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

the class AbstractExprNodeVisitorTest method testInterfaceImplementation.

@Test
public void testInterfaceImplementation() {
    MinusOpNode expr = new MinusOpNode(LOC);
    expr.addChild(new IntegerNode(17, LOC));
    VarRefNode dataRef = new VarRefNode("boo", LOC, false, null);
    expr.addChild(dataRef);
    IncompleteEvalVisitor iev = new IncompleteEvalVisitor(ImmutableMap.of("boo", 13.0));
    assertThat(iev.exec(expr)).isWithin(0.0).of(4.0);
    expr.replaceChild(0, new IntegerNode(34, LOC));
    assertThat(iev.exec(expr)).isWithin(0.0).of(21.0);
}
Also used : MinusOpNode(com.google.template.soy.exprtree.OperatorNodes.MinusOpNode) Test(org.junit.Test)

Example 3 with MinusOpNode

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

the class AbstractOperatorNodeTest method testToSourceString1.

@Test
public void testToSourceString1() {
    // Test expression: $x - -$x - (-($x - $x) - $x)
    // 
    // The expression tree looks like this:
    // [MinusOpNode] n0
    // [MinusOpNode] n1
    // [VarRefNode] $x
    // [NegativeOpNode] n3
    // [VarRefNode] $x
    // [MinusOpNode] n2
    // [NegativeOpNode] n4
    // [MinusOpNode] n5
    // [VarRefNode] $x
    // [VarRefNode] $x
    // [VarRefNode] $x
    // Root n0.
    MinusOpNode n0 = new MinusOpNode(X);
    // Children of n0.
    MinusOpNode n1 = new MinusOpNode(X);
    MinusOpNode n2 = new MinusOpNode(X);
    n0.addChild(n1);
    n0.addChild(n2);
    // Children of n1.
    NegativeOpNode n3 = new NegativeOpNode(X);
    n1.addChild(x);
    n1.addChild(n3);
    // Child of n3.
    n3.addChild(x.copy(new CopyState()));
    // Children of n2.
    NegativeOpNode n4 = new NegativeOpNode(X);
    n2.addChild(n4);
    n2.addChild(x.copy(new CopyState()));
    // Child of n4.
    MinusOpNode n5 = new MinusOpNode(X);
    n4.addChild(n5);
    // Children of n5.
    n5.addChild(x.copy(new CopyState()));
    n5.addChild(x.copy(new CopyState()));
    assertThat(n0.toSourceString()).isEqualTo("$x - -$x - (-($x - $x) - $x)");
}
Also used : NegativeOpNode(com.google.template.soy.exprtree.OperatorNodes.NegativeOpNode) MinusOpNode(com.google.template.soy.exprtree.OperatorNodes.MinusOpNode) CopyState(com.google.template.soy.basetree.CopyState) Test(org.junit.Test)

Example 4 with MinusOpNode

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

MinusOpNode (com.google.template.soy.exprtree.OperatorNodes.MinusOpNode)4 Test (org.junit.Test)4 CopyState (com.google.template.soy.basetree.CopyState)1 BooleanNode (com.google.template.soy.exprtree.BooleanNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 FloatNode (com.google.template.soy.exprtree.FloatNode)1 IntegerNode (com.google.template.soy.exprtree.IntegerNode)1 ConditionalOpNode (com.google.template.soy.exprtree.OperatorNodes.ConditionalOpNode)1 NegativeOpNode (com.google.template.soy.exprtree.OperatorNodes.NegativeOpNode)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