Search in sources :

Example 66 with Context

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

Example 67 with Context

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

Example 68 with Context

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

Example 69 with Context

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

Example 70 with Context

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