Search in sources :

Example 21 with Context

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");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 22 with Context

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));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 23 with Context

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));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 24 with Context

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"));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 25 with Context

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"));
}
Also used : Context(com.squarespace.template.Context) Test(org.testng.annotations.Test)

Aggregations

Context (com.squarespace.template.Context)73 Test (org.testng.annotations.Test)55 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)21 Instruction (com.squarespace.template.Instruction)14 Variables (com.squarespace.template.Variables)11 TextNode (com.fasterxml.jackson.databind.node.TextNode)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 Arguments (com.squarespace.template.Arguments)5 CodeMaker (com.squarespace.template.CodeMaker)4 Compiler (com.squarespace.template.Compiler)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 CompiledTemplate (com.squarespace.template.CompiledTemplate)2 CodeBuilder (com.squarespace.template.CodeBuilder)1 CodeExecuteException (com.squarespace.template.CodeExecuteException)1 CodeMachine (com.squarespace.template.CodeMachine)1 CodeSyntaxException (com.squarespace.template.CodeSyntaxException)1 ErrorInfo (com.squarespace.template.ErrorInfo)1 Locale (java.util.Locale)1