use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testConstants.
@Test
public void testConstants() {
Context c = ctx();
assertEquals(reduce("PI", c), new DoubleNode(Expr.PI));
assertEquals(reduce("E", c), new DoubleNode(Expr.E));
assertEquals(reduce("-E", c), new DoubleNode(-Expr.E));
assertEquals(reduce("Infinity", c), new DoubleNode(Double.POSITIVE_INFINITY));
assertEquals(reduce("-Infinity", c), new DoubleNode(Double.NEGATIVE_INFINITY));
assertEquals(reduce("NaN", c), new DoubleNode(Double.NaN));
assertEquals(reduce("true", c), BooleanNode.TRUE);
assertEquals(reduce("false", c), BooleanNode.FALSE);
assertEquals(reduce("null", c), NullNode.getInstance());
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testMultiply.
@Test
public void testMultiply() {
Context c = new Context(JsonUtils.decode("{}"));
assertEquals(reduce("5 * 0.5", c), new DoubleNode(2.5));
assertEquals(reduce("5 * true", c), new DoubleNode(5));
assertEquals(reduce("5 * false", c), new DoubleNode(0));
assertEquals(reduce("5 * null", c), new DoubleNode(0));
assertEquals(reduce("5 * \"2\"", c), new DoubleNode(10));
assertEquals(reduce("5 * \"\"", c), new DoubleNode(0));
assertEquals(reduce("0.5 * 5", c), new DoubleNode(2.5));
assertEquals(reduce("true * 5", c), new DoubleNode(5));
assertEquals(reduce("false * 5", c), new DoubleNode(0));
assertEquals(reduce("null * 5", c), new DoubleNode(0));
assertEquals(reduce("\"2\" * 5", c), new DoubleNode(10));
assertEquals(reduce("\"\" * 5", c), new DoubleNode(0));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testComparisons.
@Test
public void testComparisons() {
Context c = ctx();
assertEquals(reduce("1 < 2", c), BooleanNode.TRUE);
assertEquals(reduce("2 < 2", c), BooleanNode.FALSE);
assertEquals(reduce("3 < 2", c), BooleanNode.FALSE);
assertEquals(reduce("1 <= 0", c), BooleanNode.FALSE);
assertEquals(reduce("1 <= 1", c), BooleanNode.TRUE);
assertEquals(reduce("1 <= 2", c), BooleanNode.TRUE);
assertEquals(reduce("1 > 2", c), BooleanNode.FALSE);
assertEquals(reduce("2 > 2", c), BooleanNode.FALSE);
assertEquals(reduce("3 > 2", c), BooleanNode.TRUE);
assertEquals(reduce("1 >= 0", c), BooleanNode.TRUE);
assertEquals(reduce("1 >= 1", c), BooleanNode.TRUE);
assertEquals(reduce("1 >= 2", c), BooleanNode.FALSE);
assertEquals(reduce("0 < \"1\"", c), BooleanNode.TRUE);
assertEquals(reduce("0 < \"0x01\"", c), BooleanNode.TRUE);
assertEquals(reduce("5 < \"1\"", c), BooleanNode.FALSE);
assertEquals(reduce("5 < \"0x01\"", c), BooleanNode.FALSE);
// booleans convert to numbers
assertEquals(reduce("0 < true", c), BooleanNode.TRUE);
assertEquals(reduce("0 < false", c), BooleanNode.FALSE);
assertEquals(reduce("0 <= true", c), BooleanNode.TRUE);
assertEquals(reduce("0 <= false", c), BooleanNode.TRUE);
// non-numeric strings are cast to NaN
assertEquals(reduce("0 < \"foo\"", c), BooleanNode.FALSE);
assertEquals(reduce("0 < \"foo\"", c), BooleanNode.FALSE);
// any portion of the string that is non-numeric fails
assertEquals(reduce("0 < \"1foo\"", c), BooleanNode.FALSE);
// strings are compared by unicode code points
assertEquals(reduce("\"a\" < \"b\"", c), BooleanNode.TRUE);
assertEquals(reduce("\"a\" < \"a\"", c), BooleanNode.FALSE);
assertEquals(reduce("\"b\" < \"a\"", c), BooleanNode.FALSE);
assertEquals(reduce("\"a\" <= \"b\"", c), BooleanNode.TRUE);
assertEquals(reduce("\"a\" <= \"a\"", c), BooleanNode.TRUE);
assertEquals(reduce("\"b\" <= \"a\"", c), BooleanNode.FALSE);
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testLimits.
@Test
public void testLimits() {
Expr e;
JsonNode r;
Context c;
JsonNode o = JsonUtils.decode("{\"foo\": \"123456789\"}");
ExprOptions opts = new ExprOptions();
opts.maxStringLen(10);
opts.maxTokens(10);
// reduce phase string concatenation limit
c = new Context(o);
e = new Expr("foo + \"a\"", opts);
e.build();
r = e.reduce(c);
assertEquals(r, new TextNode("123456789a"));
c = new Context(o);
e = new Expr("foo + \"ab\"", opts);
e.build();
r = e.reduce(c);
assertEquals(r, null);
assertTrue(c.getErrors().get(0).getMessage().contains("maximum string"));
// parse phase token limit
c = new Context(o);
e = new Expr("1 + 3 + 5 + 7 + 9", opts);
e.build();
r = e.reduce(c);
assertEquals(r, new DoubleNode(25));
c = new Context(o);
e = new Expr("1 + 3 + 5 + 7 + 9 + 11", opts);
e.build();
r = e.reduce(c);
assertEquals(r, null);
assertTrue(e.errors().get(0).contains("maximum number of allowed tokens"));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testReferences.
@Test
public void testReferences() {
Context c = new Context(JsonUtils.decode("{\"a\": 123}"));
assertEquals(reduce("@", c), null);
assertEquals(reduce("@.a", c), new DoubleNode(123));
}
Aggregations