Search in sources :

Example 36 with Context

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

the class ExprTest method testUnaryPlusMinus.

@Test
public void testUnaryPlusMinus() {
    Context c = new Context(JsonUtils.decode("{\"a\":5,\"b\":-5,\"c\":\"12\"}"));
    assertEquals(reduce("1 + -a", c), new DoubleNode(-4));
    assertEquals(reduce("1 + -b", c), new DoubleNode(6));
    assertEquals(reduce("1 + +a", c), new DoubleNode(6));
    assertEquals(reduce("1 + +b", c), new DoubleNode(-4));
    assertEquals(reduce("+a - -a + -a + +a", c), new DoubleNode(10));
    assertEquals(reduce("-c + -c", c), new DoubleNode(-24));
    // errors
    assertEquals(reduce("+ -", c), null);
    assertEquals(reduce("! ~", c), null);
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 37 with Context

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

the class ExprTest method testLogical.

@Test
public void testLogical() {
    Context c = ctx();
    // or
    assertEquals(reduce("true || true", c), BooleanNode.TRUE);
    assertEquals(reduce("true || false", c), BooleanNode.TRUE);
    assertEquals(reduce("false || false", c), BooleanNode.FALSE);
    assertEquals(reduce("\"foo\" || \"bar\"", c), BooleanNode.TRUE);
    assertEquals(reduce("0 || \"bar\"", c), BooleanNode.TRUE);
    assertEquals(reduce("0 || \"\"", c), BooleanNode.FALSE);
    // and
    assertEquals(reduce("true && true", c), BooleanNode.TRUE);
    assertEquals(reduce("true && false", c), BooleanNode.FALSE);
    assertEquals(reduce("false && true", c), BooleanNode.FALSE);
    assertEquals(reduce("false && false", c), BooleanNode.FALSE);
    assertEquals(reduce("1 && 1", c), BooleanNode.TRUE);
    assertEquals(reduce("1 && 2", c), BooleanNode.TRUE);
    assertEquals(reduce("1 && 0", c), BooleanNode.FALSE);
    assertEquals(reduce("true && \"foo\"", c), BooleanNode.TRUE);
    assertEquals(reduce("true && \"\"", c), BooleanNode.FALSE);
}
Also used : Context(com.squarespace.template.Context) Test(org.testng.annotations.Test)

Example 38 with Context

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

the class ExprTest method testTypeConversions.

@Test
public void testTypeConversions() {
    Context c = ctx("{\"a\":{\"b\":3.75,\"c\":\"3.1415\",\"flag\":true,\"nil\":null,\"s\":\"foo\"}," + "\"n0\":\"-0x1234\",\"n1\":\"-1.2e21\",\"n2\":\"--1.2\",\"n3\":\"--0x1234\"}");
    assertEquals(reduce("str()", c), null);
    assertEquals(reduce("str(\"\")", c), new TextNode(""));
    assertEquals(reduce("str(\"foobar\")", c), new TextNode("foobar"));
    assertEquals(reduce("str(zzz)", c), new TextNode("null"));
    assertEquals(reduce("str(a.b)", c), new TextNode("3.75"));
    assertEquals(reduce("str(true)", c), new TextNode("true"));
    assertEquals(reduce("str(false)", c), new TextNode("false"));
    assertEquals(reduce("str(null)", c), new TextNode("null"));
    assertEquals(reduce("str(a.flag)", c), new TextNode("true"));
    assertEquals(reduce("str(a.nil)", c), new TextNode("null"));
    assertEquals(reduce("str(a.s)", c), new TextNode("foo"));
    assertEquals(reduce("str(-Infinity)", c), new TextNode("-Infinity"));
    assertEquals(reduce("str(Infinity)", c), new TextNode("Infinity"));
    assertEquals(reduce("str(NaN)", c), new TextNode("NaN"));
    assertEquals(reduce("str(12e20)", c), new TextNode("1.2e+21"));
    assertEquals(reduce("num()", c), null);
    assertEquals(reduce("num(zzz)", c), new DoubleNode(0));
    assertEquals(reduce("num(a.c)", c), new DoubleNode(3.1415));
    assertEquals(reduce("num(\"0xcafe\")", c), new DoubleNode(0xcafe));
    assertEquals(reduce("num(\"0Xcafe\")", c), new DoubleNode(0xcafe));
    assertEquals(reduce("num(\"000123\")", c), new DoubleNode(123));
    assertEquals(reduce("num(\"0x\")", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("num(\"0xfoo\")", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("num(\"123foo\")", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("num(\"-123\")", c), new DoubleNode(-123));
    assertEquals(reduce("num(\"-\")", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("num(\"--123\")", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("num(n0)", c), new DoubleNode(-0x1234));
    assertEquals(reduce("num(n1)", c), new DoubleNode(-1.2e21));
    assertEquals(reduce("num(n2)", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("num(n3)", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("bool()", c), null);
    assertEquals(reduce("bool(zzz)", c), BooleanNode.FALSE);
    assertEquals(reduce("bool(a.b)", c), BooleanNode.TRUE);
    assertEquals(reduce("bool(\"foo\")", c), BooleanNode.TRUE);
    assertEquals(reduce("str(num(bool(true)))", c), new TextNode("1"));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) Test(org.testng.annotations.Test)

Example 39 with Context

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

the class ExprTest method testAdd.

@Test
public void testAdd() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("1 + true", c), new DoubleNode(2));
    assertEquals(reduce("1 + false", c), new DoubleNode(1));
    assertEquals(reduce("1 + null", c), new DoubleNode(1));
    assertEquals(reduce("true + 2", c), new DoubleNode(3));
    assertEquals(reduce("false + 2", c), new DoubleNode(2));
    assertEquals(reduce("null + 2", c), new DoubleNode(2));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 40 with Context

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

the class ExprTest method testConcatenate.

@Test
public void testConcatenate() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("\"foo\" + 1", c), new TextNode("foo1"));
    assertEquals(reduce("\"foo\" + null", c), new TextNode("foonull"));
    assertEquals(reduce("\"foo\" + true", c), new TextNode("footrue"));
    assertEquals(reduce("\"foo\" + false", c), new TextNode("foofalse"));
    assertEquals(reduce("\"\" + false", c), new TextNode("false"));
    assertEquals(reduce("1 + \"foo\"", c), new TextNode("1foo"));
    assertEquals(reduce("null + \"bar\"", c), new TextNode("nullbar"));
    assertEquals(reduce("true + \"bar\"", c), new TextNode("truebar"));
    assertEquals(reduce("false + \"bar\"", c), new TextNode("falsebar"));
    assertEquals(reduce("false + \"\"", c), new TextNode("false"));
}
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