use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testExponent.
@Test
public void testExponent() {
Context c = new Context(JsonUtils.decode("{}"));
assertEquals(reduce("2 ** 3", c), new DoubleNode(8));
assertEquals(reduce("\"foo\" ** 3", c), new DoubleNode(Double.NaN));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testUnsupportedOperators.
@Test
public void testUnsupportedOperators() {
Context c = new Context(JsonUtils.decode("{}"));
Expr e;
// Reduce invalid expressions to ensure that unexpected operators are caught
// during evaluation.
e = new Expr("");
e.reduceExpr(c, asList(num(1), num(2), SEMI));
assertTrue(e.errors().get(0).contains("Unexpected operator"));
e = new Expr("");
e.reduceExpr(c, asList(num(1), num(2), LPRN));
assertTrue(e.errors().get(0).contains("Unexpected operator"));
e = new Expr("");
e.reduceExpr(c, asList(varn("@foo"), num(1), num(2), ADD, LPRN, ASN));
assertTrue(e.errors().get(0).contains("Unexpected operator"));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testFunctionArgsBoundary.
@Test
public void testFunctionArgsBoundary() {
Context c = ctx();
assertEquals(build("max(1, 2) 123"), asList(asList(Tokens.ARGS, num(1), num(2), call("max"), num(123))));
assertEquals(reduce("max(1, 2) 123", c), new DoubleNode(123));
assertEquals(build("max(2) 1"), asList(asList(Tokens.ARGS, num(2), call("max"), num(1))));
assertEquals(reduce("max(2) 1", c), new DoubleNode(1));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testUnaryBinaryLogicalNot.
@Test
public void testUnaryBinaryLogicalNot() {
Context c = new Context(JsonUtils.decode("{\"a\":5,\"b\":false,\"c\":\"foo\"}"));
// binary
assertEquals(reduce("~2", c), new DoubleNode(-3));
assertEquals(reduce("a + ~2", c), new DoubleNode(2));
// logical
assertEquals(reduce("!a", c), BooleanNode.FALSE);
assertEquals(reduce("!!a", c), BooleanNode.TRUE);
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testNumberFormatting.
@Test
public void testNumberFormatting() {
Context c = ctx();
// large magnitude
assertEquals(reduce("str(1e20)", c), new TextNode("100000000000000000000"));
assertEquals(reduce("str(1e21)", c), new TextNode("1e+21"));
assertEquals(reduce("str(1e300)", c), new TextNode("1e+300"));
// small magnitude
assertEquals(reduce("str(1e-5)", c), new TextNode("0.00001"));
assertEquals(reduce("str(1e-6)", c), new TextNode("0.000001"));
assertEquals(reduce("str(1e-7)", c), new TextNode("1e-7"));
assertEquals(reduce("str(1e-20)", c), new TextNode("1e-20"));
assertEquals(reduce("str(1e-21)", c), new TextNode("1e-21"));
assertEquals(reduce("str(1e-300)", c), new TextNode("1e-300"));
// negative large magnitude
assertEquals(reduce("str(-1e20)", c), new TextNode("-100000000000000000000"));
assertEquals(reduce("str(-1e21)", c), new TextNode("-1e+21"));
assertEquals(reduce("str(-1e300)", c), new TextNode("-1e+300"));
// negative small magnitude
assertEquals(reduce("str(-1e-5)", c), new TextNode("-0.00001"));
assertEquals(reduce("str(-1e-6)", c), new TextNode("-0.000001"));
assertEquals(reduce("str(-1e-7)", c), new TextNode("-1e-7"));
assertEquals(reduce("str(-1e-20)", c), new TextNode("-1e-20"));
assertEquals(reduce("str(-1e-21)", c), new TextNode("-1e-21"));
assertEquals(reduce("str(-1e-300)", c), new TextNode("-1e-300"));
}
Aggregations