use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testStrictEquality.
@Test
public void testStrictEquality() {
Context c = new Context(JsonUtils.decode("{}"));
assertEquals(reduce("1 === 1", c), BooleanNode.TRUE);
assertEquals(reduce("1 === 2", c), BooleanNode.FALSE);
assertEquals(reduce("1 === NaN", c), BooleanNode.FALSE);
assertEquals(reduce("NaN === NaN", c), BooleanNode.FALSE);
assertEquals(reduce("true === 1", c), BooleanNode.FALSE);
assertEquals(reduce("false === 1", c), BooleanNode.FALSE);
assertEquals(reduce("\"1\" === 1", c), BooleanNode.FALSE);
assertEquals(reduce("1 !== 1", c), BooleanNode.FALSE);
assertEquals(reduce("1 !== 2", c), BooleanNode.TRUE);
assertEquals(reduce("\"1\" !== 1", c), BooleanNode.TRUE);
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testUnsupportedValues.
@Test
public void testUnsupportedValues() {
// objects and array types are not supported for operations
Context c = new Context(JsonUtils.decode("{\"obj\":{\"a\":1,\"b\":\"foo\"},\"arr\":[1,2,3]}"));
assertEquals(reduce("1 + obj", c), null);
assertEquals(reduce("1 + arr", c), null);
assertEquals(reduce("num(obj)", c), null);
assertEquals(reduce("str(obj)", c), null);
assertEquals(reduce("bool(obj)", c), null);
assertEquals(reduce("num(arr)", c), null);
assertEquals(reduce("str(arr)", c), null);
assertEquals(reduce("bool(arr)", c), null);
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testBasic.
@Test
public void testBasic() {
String e;
Context c;
c = new Context(JsonUtils.decode("{}"));
e = "";
assertEquals(parse(e), emptyList());
assertEquals(build(e), emptyList());
assertEquals(reduce(e, c), null);
// if no operators are applied, the result is the top of the RPN stack
e = "1 2 3 4 5";
assertEquals(parse(e), asList(num(1), num(2), num(3), num(4), num(5)));
assertEquals(build(e), asList(asList(num(1), num(2), num(3), num(4), num(5))));
assertEquals(reduce(e, c), new DoubleNode(5));
e = "1+2";
assertEquals(parse(e), asList(num(1), ADD, num(2)));
assertEquals(build(e), asList(asList(num(1), num(2), ADD)));
assertEquals(reduce(e, c), new DoubleNode(3));
e = "1-2";
assertEquals(parse(e), asList(num(1), SUB, num(2)));
assertEquals(build(e), asList(asList(num(1), num(2), SUB)));
assertEquals(reduce(e, c), new DoubleNode(-1));
e = "1 + 2";
assertEquals(parse(e), asList(num(1), ADD, num(2)));
assertEquals(build(e), asList(asList(num(1), num(2), ADD)));
assertEquals(reduce(e, c), new DoubleNode(3));
e = "1 + -3";
assertEquals(parse(e), asList(num(1), ADD, MINUS, num(3)));
assertEquals(build(e), asList(asList(num(1), num(3), MINUS, ADD)));
assertEquals(reduce(e, c), new DoubleNode(-2));
e = "a.b.c * -1";
assertEquals(parse(e), asList(varn("a.b.c"), MUL, MINUS, num(1)));
assertEquals(build(e), asList(asList(varn("a.b.c"), num(1), MINUS, MUL)));
c = new Context(JsonUtils.decode("{\"a\": {\"b\": {\"c\": 5 }}}"));
assertEquals(reduce(e, c), new DoubleNode(-5));
e = "\"foo\" == '\u2018bar\u2019'";
assertEquals(parse(e), asList(str("foo"), EQ, str("\u2018bar\u2019")));
assertEquals(build(e), asList(asList(str("foo"), str("\u2018bar\u2019"), EQ)));
assertEquals(reduce(e, c), BooleanNode.FALSE);
e = " max ( 1 , 2 ) ";
assertEquals(parse(e), asList(call("max"), LPRN, num(1), COMMA, num(2), RPRN));
assertEquals(build(e), asList(asList(Tokens.ARGS, num(1), num(2), call("max"))));
assertEquals(reduce(e, c), new DoubleNode(2));
e = "1 === 1";
assertEquals(parse(e), asList(num(1), SEQ, num(1)));
assertEquals(build(e), asList(asList(num(1), num(1), SEQ)));
assertEquals(reduce(e, c), BooleanNode.TRUE);
e = "false == true";
assertEquals(parse(e), asList(FALSE, EQ, TRUE));
assertEquals(build(e), asList(asList(FALSE, TRUE, EQ)));
e = "null || 1";
assertEquals(parse(e), asList(NULL, LOR, num(1)));
assertEquals(build(e), asList(asList(NULL, num(1), LOR)));
e = "(1 + (2 * (7 - (3 / 4))))";
assertEquals(parse(e), asList(LPRN, num(1), ADD, LPRN, num(2), MUL, LPRN, num(7), SUB, LPRN, num(3), DIV, num(4), RPRN, RPRN, RPRN, RPRN));
assertEquals(build(e), asList(asList(num(1), num(2), num(7), num(3), num(4), DIV, SUB, MUL, ADD)));
assertEquals(reduce(e, c), new DoubleNode(13.5));
e = "7+8;4";
assertEquals(parse(e), asList(num(7), ADD, num(8), SEMI, num(4)));
assertEquals(build(e), asList(asList(num(7), num(8), ADD), asList(num(4))));
e = "@a = 8; @b = 4 / @a; @c = 3 * @b; @c";
assertEquals(parse(e), asList(varn("@a"), ASN, num(8), SEMI, varn("@b"), ASN, num(4), DIV, varn("@a"), SEMI, varn("@c"), ASN, num(3), MUL, varn("@b"), SEMI, varn("@c")));
assertEquals(build(e), asList(asList(varn("@a"), num(8), ASN), asList(varn("@b"), num(4), varn("@a"), DIV, ASN), asList(varn("@c"), num(3), varn("@b"), MUL, ASN), asList(varn("@c"))));
assertEquals(reduce(e, c), new DoubleNode(1.5));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testDivide.
@Test
public void testDivide() {
Context c = new Context(JsonUtils.decode("{}"));
assertEquals(reduce("5 / 2", c), new DoubleNode(2.5));
assertEquals(reduce("\"foo\" / 2", c), new DoubleNode(Double.NaN));
assertEquals(reduce("5 / 0", c), new DoubleNode(Double.NaN));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testNesting.
@Test
public void testNesting() {
Context c = new Context(JsonUtils.decode("{}"));
assertEquals(reduce("((1 + 2) * 3) ** 2", c), new DoubleNode(81));
// errant right parens
assertEquals(reduce("1 )", c), null);
assertEquals(reduce("1 + ) 2", c), null);
}
Aggregations