use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testSubtract.
@Test
public void testSubtract() {
Context c = new Context(JsonUtils.decode("{\"a\": 5}"));
assertEquals(reduce("a - 1", c), new DoubleNode(4));
assertEquals(reduce("\"foo\" - 1", c), new DoubleNode(Double.NaN));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testAssignment.
@Test
public void testAssignment() {
Expr e;
Context c = new Context(JsonUtils.createObjectNode());
c.setVar("@c", new DoubleNode(3));
e = new Expr("@a = 6; @b = 2 * @a; @b / @c");
e.build();
assertEquals(c.getErrors(), emptyList());
assertEquals(e.reduce(c), new DoubleNode(4));
assertEquals(c.resolve(new Object[] { "@a" }), new DoubleNode(6));
assertEquals(c.resolve(new Object[] { "@b" }), new DoubleNode(12));
// invalid assignments
e = new Expr("a = 1");
assertEquals(e.tokens().elems(), asList(varn("a"), ASN, num(1)));
e.build();
assertEquals(e.reduce(c), null);
e = new Expr("a.b.c = 1");
assertEquals(e.tokens().elems(), asList(varn("a.b.c"), ASN, num(1)));
e.build();
assertEquals(e.reduce(c), null);
e = new Expr("* = /");
assertEquals(e.tokens().elems(), asList(MUL, ASN, DIV));
e.build();
assertEquals(e.reduce(c), null);
e = new Expr("1 = 1");
assertEquals(e.tokens().elems(), asList(num(1), ASN, num(1)));
e.build();
assertEquals(e.reduce(c), null);
// '@' cannot be assigned
e = new Expr("@ = 1");
assertEquals(e.tokens().elems(), asList(varn("@"), ASN, num(1)));
e.build();
assertEquals(e.reduce(c), null);
e = new Expr("@=@=@=3");
e.build();
assertEquals(e.reduce(c), null);
e = new Expr("=@");
e.build();
assertEquals(e.reduce(c), null);
e = new Expr("5=@");
e.build();
assertEquals(e.reduce(c), null);
e = new Expr("5=5");
e.build();
assertEquals(e.reduce(c), null);
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testFunctionCalls.
@Test
public void testFunctionCalls() {
Context c = ctx();
Expr e;
// min() takes the minimum of all of its numeric arguments
assertEquals(reduce("min ( )", c), null);
assertEquals(reduce("min(\"foo\")", c), new DoubleNode(Double.NaN));
assertEquals(reduce("min(\"foo\", 5, -5)", c), new DoubleNode(-5));
assertEquals(reduce("min(5, -3, 10, -100, 17, 1000)", c), new DoubleNode(-100));
// max() takes the maximum of all of its numeric arguments
assertEquals(reduce("max(\"foo\", 5, -5)", c), new DoubleNode(5));
// abs() operates only on the first argument that converts cleanly to a number
assertEquals(reduce("abs()", c), null);
assertEquals(reduce("abs(-5, -10, -20, -30)", c), new DoubleNode(5));
assertEquals(reduce("abs(\"foo\")", c), new DoubleNode(Double.NaN));
assertEquals(reduce("abs(\"-12\")", c), new DoubleNode(12));
// bad functions
e = new Expr("1 + a(1, 2)");
assertTrue(e.errors().get(0).contains("Invalid function"));
// a variable reference next to a parenthesis will stop
// accepting tokens, terminating the expression early
c = ctx("{\"a\": 123}");
assertEquals(reduce("1 + a (2, 3, 4)", c), null);
assertEquals(reduce("foobar ( 1, 2, 3 )", c), null);
// missing nodes will reduce to null
assertEquals(reduce("missing", c), NullNode.getInstance());
// invalid function reference
assertEquals(reduce("@(3)", c), new DoubleNode(3));
assertEquals(reduce("@.a(3)", c), new DoubleNode(3));
assertEquals(reduce("@.@(3)", c), new DoubleNode(3));
}
Aggregations