Search in sources :

Example 71 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class ExprTest method testSubtract.

@Test
public void testSubtract() {
    Context c = new Context(JsonUtils.decode("{\"a\": 5}"));
    assertEquals(reduce("a - 1", c), new DoubleNode(4));
    assertEquals(reduce("\"foo\" - 1", c), new DoubleNode(Double.NaN));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 72 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class ExprTest method testAssignment.

@Test
public void testAssignment() {
    Expr e;
    Context c = new Context(JsonUtils.createObjectNode());
    c.setVar("@c", new DoubleNode(3));
    e = new Expr("@a = 6; @b = 2 * @a; @b / @c");
    e.build();
    assertEquals(c.getErrors(), emptyList());
    assertEquals(e.reduce(c), new DoubleNode(4));
    assertEquals(c.resolve(new Object[] { "@a" }), new DoubleNode(6));
    assertEquals(c.resolve(new Object[] { "@b" }), new DoubleNode(12));
    // invalid assignments
    e = new Expr("a = 1");
    assertEquals(e.tokens().elems(), asList(varn("a"), ASN, num(1)));
    e.build();
    assertEquals(e.reduce(c), null);
    e = new Expr("a.b.c = 1");
    assertEquals(e.tokens().elems(), asList(varn("a.b.c"), ASN, num(1)));
    e.build();
    assertEquals(e.reduce(c), null);
    e = new Expr("* = /");
    assertEquals(e.tokens().elems(), asList(MUL, ASN, DIV));
    e.build();
    assertEquals(e.reduce(c), null);
    e = new Expr("1 = 1");
    assertEquals(e.tokens().elems(), asList(num(1), ASN, num(1)));
    e.build();
    assertEquals(e.reduce(c), null);
    // '@' cannot be assigned
    e = new Expr("@ = 1");
    assertEquals(e.tokens().elems(), asList(varn("@"), ASN, num(1)));
    e.build();
    assertEquals(e.reduce(c), null);
    e = new Expr("@=@=@=3");
    e.build();
    assertEquals(e.reduce(c), null);
    e = new Expr("=@");
    e.build();
    assertEquals(e.reduce(c), null);
    e = new Expr("5=@");
    e.build();
    assertEquals(e.reduce(c), null);
    e = new Expr("5=5");
    e.build();
    assertEquals(e.reduce(c), null);
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 73 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class ExprTest method testFunctionCalls.

@Test
public void testFunctionCalls() {
    Context c = ctx();
    Expr e;
    // min() takes the minimum of all of its numeric arguments
    assertEquals(reduce("min ( )", c), null);
    assertEquals(reduce("min(\"foo\")", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("min(\"foo\", 5, -5)", c), new DoubleNode(-5));
    assertEquals(reduce("min(5, -3, 10, -100, 17, 1000)", c), new DoubleNode(-100));
    // max() takes the maximum of all of its numeric arguments
    assertEquals(reduce("max(\"foo\", 5, -5)", c), new DoubleNode(5));
    // abs() operates only on the first argument that converts cleanly to a number
    assertEquals(reduce("abs()", c), null);
    assertEquals(reduce("abs(-5, -10, -20, -30)", c), new DoubleNode(5));
    assertEquals(reduce("abs(\"foo\")", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("abs(\"-12\")", c), new DoubleNode(12));
    // bad functions
    e = new Expr("1 + a(1, 2)");
    assertTrue(e.errors().get(0).contains("Invalid function"));
    // a variable reference next to a parenthesis will stop
    // accepting tokens, terminating the expression early
    c = ctx("{\"a\": 123}");
    assertEquals(reduce("1 + a (2, 3, 4)", c), null);
    assertEquals(reduce("foobar ( 1, 2, 3 )", c), null);
    // missing nodes will reduce to null
    assertEquals(reduce("missing", c), NullNode.getInstance());
    // invalid function reference
    assertEquals(reduce("@(3)", c), new DoubleNode(3));
    assertEquals(reduce("@.a(3)", c), new DoubleNode(3));
    assertEquals(reduce("@.@(3)", c), new DoubleNode(3));
}
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