use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ContentFormattersTest method testAbsUrl.
@Test
public void testAbsUrl() throws CodeException {
String template = "{a|AbsUrl}";
String json = "{\"base-url\": \"http://foobar.com/foo\", \"a\": \"abc\"}";
Instruction code = compiler().compile(template).code();
Context ctx = compiler().newExecutor().code(code).json(json).execute();
String result = eval(ctx);
assertEquals(result, "http://foobar.com/foo/abc");
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testSequences.
@Test
public void testSequences() {
Context c = new Context(JsonUtils.decode("{}"));
assertEquals(reduce("1; 2; 3", c), new DoubleNode(3));
assertEquals(reduce("1, 2, 3", c), new DoubleNode(3));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testBitwise.
@Test
public void testBitwise() {
Context c = ctx();
// or
assertEquals(reduce("0x02 | 0x01", c), new DoubleNode(3));
// and
assertEquals(reduce("0x02 & 0x01", c), new DoubleNode(0));
assertEquals(reduce("0x02 & 0x02", c), new DoubleNode(2));
assertEquals(reduce("0x02 & 0x07", c), new DoubleNode(2));
assertEquals(reduce("0x03 & 0x07", c), new DoubleNode(3));
// xor
assertEquals(reduce("0x02 ^ 0x01", c), new DoubleNode(3));
assertEquals(reduce("0x02 ^ 0x02", c), new DoubleNode(0));
assertEquals(reduce("0x02 ^ 0x03", c), new DoubleNode(1));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testNumbers.
@Test
public void testNumbers() {
Context c = new Context(JsonUtils.decode("{}"));
Expr e;
assertEquals(reduce("0x01", c), new DoubleNode(0x01));
assertEquals(reduce("0x012345678", c), new DoubleNode(0x012345678));
assertEquals(reduce("0x111111111111111111111", c), new DoubleNode(1.2895208742556044e+24));
assertEquals(reduce("0x11111111111111111", c), new DoubleNode(1.9676527011956855e+19));
assertEquals(reduce("1e20", c), new DoubleNode(1e20));
assertEquals(reduce("1e22", c), new DoubleNode(1e22));
// ensure exponent / sign state is correctly managed
assertEquals(reduce("1e20+1e20", c), new DoubleNode(2e20));
assertEquals(reduce("1e+20+1e+20", c), new DoubleNode(2e20));
assertEquals(reduce("1e-20+1e+20", c), new DoubleNode(1e20));
assertEquals(reduce("1e-20+1e20", c), new DoubleNode(1e20));
// invalid number sequences
e = new Expr(".1");
assertTrue(e.errors().get(0).contains("Unexpected char"));
e = new Expr("0x");
assertTrue(e.errors().get(0).contains("hex number"));
e = new Expr("0X");
assertTrue(e.errors().get(0).contains("hex number"));
e = new Expr("1..2");
assertTrue(e.errors().get(0).contains("Duplicate decimal point"));
e = new Expr("12e10.1");
assertTrue(e.errors().get(0).contains("decimal point in exponent"));
e = new Expr("0.0.0.");
assertTrue(e.errors().get(0).contains("Duplicate decimal point in number"));
e = new Expr("1e");
assertTrue(e.errors().get(0).contains("exponent"));
e = new Expr("1ee");
assertTrue(e.errors().get(0).contains("exponent"));
e = new Expr("1e-");
assertTrue(e.errors().get(0).contains("exponent"));
e = new Expr("1e--1");
assertTrue(e.errors().get(0).contains("exponent"));
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ExprTest method testReduceErrors.
@Test
public void testReduceErrors() {
Context c = new Context(JsonUtils.decode("{}"));
reduce("1 - max(max(abs())) - 1", c);
assertTrue(c.getErrors().get(0).getMessage().contains("calling function abs"));
assertTrue(c.getErrors().get(1).getMessage().contains("unexpected token on stack"));
}
Aggregations