Search in sources :

Example 26 with Context

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

the class ExprTest method testExponent.

@Test
public void testExponent() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("2 ** 3", c), new DoubleNode(8));
    assertEquals(reduce("\"foo\" ** 3", 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 27 with Context

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

the class ExprTest method testUnsupportedOperators.

@Test
public void testUnsupportedOperators() {
    Context c = new Context(JsonUtils.decode("{}"));
    Expr e;
    // Reduce invalid expressions to ensure that unexpected operators are caught
    // during evaluation.
    e = new Expr("");
    e.reduceExpr(c, asList(num(1), num(2), SEMI));
    assertTrue(e.errors().get(0).contains("Unexpected operator"));
    e = new Expr("");
    e.reduceExpr(c, asList(num(1), num(2), LPRN));
    assertTrue(e.errors().get(0).contains("Unexpected operator"));
    e = new Expr("");
    e.reduceExpr(c, asList(varn("@foo"), num(1), num(2), ADD, LPRN, ASN));
    assertTrue(e.errors().get(0).contains("Unexpected operator"));
}
Also used : Context(com.squarespace.template.Context) Test(org.testng.annotations.Test)

Example 28 with Context

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

the class ExprTest method testFunctionArgsBoundary.

@Test
public void testFunctionArgsBoundary() {
    Context c = ctx();
    assertEquals(build("max(1, 2) 123"), asList(asList(Tokens.ARGS, num(1), num(2), call("max"), num(123))));
    assertEquals(reduce("max(1, 2) 123", c), new DoubleNode(123));
    assertEquals(build("max(2) 1"), asList(asList(Tokens.ARGS, num(2), call("max"), num(1))));
    assertEquals(reduce("max(2) 1", c), new DoubleNode(1));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 29 with Context

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

the class ExprTest method testUnaryBinaryLogicalNot.

@Test
public void testUnaryBinaryLogicalNot() {
    Context c = new Context(JsonUtils.decode("{\"a\":5,\"b\":false,\"c\":\"foo\"}"));
    // binary
    assertEquals(reduce("~2", c), new DoubleNode(-3));
    assertEquals(reduce("a + ~2", c), new DoubleNode(2));
    // logical
    assertEquals(reduce("!a", c), BooleanNode.FALSE);
    assertEquals(reduce("!!a", c), BooleanNode.TRUE);
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 30 with Context

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

the class ExprTest method testNumberFormatting.

@Test
public void testNumberFormatting() {
    Context c = ctx();
    // large magnitude
    assertEquals(reduce("str(1e20)", c), new TextNode("100000000000000000000"));
    assertEquals(reduce("str(1e21)", c), new TextNode("1e+21"));
    assertEquals(reduce("str(1e300)", c), new TextNode("1e+300"));
    // small magnitude
    assertEquals(reduce("str(1e-5)", c), new TextNode("0.00001"));
    assertEquals(reduce("str(1e-6)", c), new TextNode("0.000001"));
    assertEquals(reduce("str(1e-7)", c), new TextNode("1e-7"));
    assertEquals(reduce("str(1e-20)", c), new TextNode("1e-20"));
    assertEquals(reduce("str(1e-21)", c), new TextNode("1e-21"));
    assertEquals(reduce("str(1e-300)", c), new TextNode("1e-300"));
    // negative large magnitude
    assertEquals(reduce("str(-1e20)", c), new TextNode("-100000000000000000000"));
    assertEquals(reduce("str(-1e21)", c), new TextNode("-1e+21"));
    assertEquals(reduce("str(-1e300)", c), new TextNode("-1e+300"));
    // negative small magnitude
    assertEquals(reduce("str(-1e-5)", c), new TextNode("-0.00001"));
    assertEquals(reduce("str(-1e-6)", c), new TextNode("-0.000001"));
    assertEquals(reduce("str(-1e-7)", c), new TextNode("-1e-7"));
    assertEquals(reduce("str(-1e-20)", c), new TextNode("-1e-20"));
    assertEquals(reduce("str(-1e-21)", c), new TextNode("-1e-21"));
    assertEquals(reduce("str(-1e-300)", c), new TextNode("-1e-300"));
}
Also used : Context(com.squarespace.template.Context) TextNode(com.fasterxml.jackson.databind.node.TextNode) 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