Search in sources :

Example 61 with Context

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

the class ExprTest method testInvalidEscapes.

@Test
public void testInvalidEscapes() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("'\\UA0000000'", c), new TextNode(" "));
}
Also used : Context(com.squarespace.template.Context) TextNode(com.fasterxml.jackson.databind.node.TextNode) Test(org.testng.annotations.Test)

Example 62 with Context

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

the class ExprTest method testStrings.

@Test
public void testStrings() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("'bar'", c), new TextNode("bar"));
    assertEquals(reduce("\"\\\"bar\\\"\"", c), new TextNode("\"bar\""));
    // incomplete escapes
    assertEquals(reduce("'\\", c), null);
    assertEquals(reduce("'\\'", c), null);
    // unterminated
    assertEquals(reduce("\"\\u000000", c), null);
    // single-character escapes
    assertEquals(reduce("\"\\a\\b\\c\\n\\t\\r\\f\"", c), new TextNode("abc\n\t\r\f"));
    // hex escapes
    assertEquals(reduce("\"\\x0a\\x09\\x20\\x7d\\x22\\x27\"", c), new TextNode("\n\t }\"'"));
    // ascii control code replacement
    assertEquals(reduce("\"\\x00\\x01\\x02\\x19\\x18\"", c), new TextNode("     "));
    // unicode escapes
    assertEquals(reduce("\"\\u2019\"", c), new TextNode("\u2019"));
    assertEquals(reduce("\"\\U0001f600\"", c), new TextNode("\uD83D\uDE00"));
    assertEquals(reduce("\"\\U0001f600\\U0001f600\"", c), new TextNode("\uD83D\uDE00\uD83D\uDE00"));
    assertEquals(reduce("\"\\u000a\\u0009\\u0020\\u007d\\u0022\\u0027\"", c), new TextNode("\n\t }\"'"));
    // unicode escape out-of-range replacement
    assertEquals(reduce("\"\\u0003\\u000f\\u0019\\u0021\"", c), new TextNode("   !"));
    assertEquals(reduce("\"\\U00222222\"", c), new TextNode(" "));
    // ascii control code replacement
    assertEquals(reduce("\"\\u0000\\u0001\\u0018\\u0019\"", c), new TextNode("    "));
}
Also used : Context(com.squarespace.template.Context) TextNode(com.fasterxml.jackson.databind.node.TextNode) Test(org.testng.annotations.Test)

Example 63 with Context

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

the class ExprTest method testModulus.

@Test
public void testModulus() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("5 % -5", c), new DoubleNode(0));
    assertEquals(reduce("5 % -4", c), new DoubleNode(1));
    assertEquals(reduce("5 % -3", c), new DoubleNode(2));
    assertEquals(reduce("5 % -2", c), new DoubleNode(1));
    assertEquals(reduce("5 % -1", c), new DoubleNode(0));
    assertEquals(reduce("5 % 0", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("5 % 1", c), new DoubleNode(0));
    assertEquals(reduce("5 % 2", c), new DoubleNode(1));
    assertEquals(reduce("5 % 3", c), new DoubleNode(2));
    assertEquals(reduce("5 % 4", c), new DoubleNode(1));
    assertEquals(reduce("5 % 5", c), new DoubleNode(0));
    assertEquals(reduce("Infinity % 10", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("10 % Infinity", c), new DoubleNode(10));
    assertEquals(reduce("Infinity % Infinity", c), new DoubleNode(Double.NaN));
    assertEquals(reduce("5 % 3.21", c), new DoubleNode(1.79));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 64 with Context

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

the class ExprTest method testShift.

@Test
public void testShift() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("14 >> -1", c), new DoubleNode(0));
    assertEquals(reduce("14 >> 0", c), new DoubleNode(14));
    assertEquals(reduce("14 >> 1", c), new DoubleNode(7));
    assertEquals(reduce("14 >> 2", c), new DoubleNode(3));
    assertEquals(reduce("14 >> 3", c), new DoubleNode(1));
    assertEquals(reduce("14 >> 4", c), new DoubleNode(0));
    assertEquals(reduce("14 << -1", c), new DoubleNode(0));
    assertEquals(reduce("14 << 0", c), new DoubleNode(14));
    assertEquals(reduce("14 << 1", c), new DoubleNode(28));
    assertEquals(reduce("14 << 2", c), new DoubleNode(56));
    assertEquals(reduce("14 << 3", c), new DoubleNode(112));
    assertEquals(reduce("14 << 4", c), new DoubleNode(224));
    // non-numbers
    assertEquals(reduce("\"foo\" >> 4", c), new DoubleNode(0));
    assertEquals(reduce("\"foo\" << 4", c), new DoubleNode(0));
    assertEquals(reduce("true << 4", c), new DoubleNode(16));
    assertEquals(reduce("null << 4", c), new DoubleNode(0));
}
Also used : Context(com.squarespace.template.Context) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) Test(org.testng.annotations.Test)

Example 65 with Context

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

the class ExprTest method testEquality.

@Test
public void testEquality() {
    Context c = new Context(JsonUtils.decode("{}"));
    assertEquals(reduce("1 == 1", c), BooleanNode.TRUE);
    assertEquals(reduce("1 == 2", c), BooleanNode.FALSE);
    assertEquals(reduce("1 == NaN", c), BooleanNode.FALSE);
    assertEquals(reduce("NaN == NaN", c), BooleanNode.FALSE);
    assertEquals(reduce("true == 1", c), BooleanNode.TRUE);
    assertEquals(reduce("false == 1", c), BooleanNode.FALSE);
    assertEquals(reduce("\"1\" == 1", c), BooleanNode.TRUE);
    assertEquals(reduce("1 != 1", c), BooleanNode.FALSE);
    assertEquals(reduce("1 != 2", c), BooleanNode.TRUE);
    assertEquals(reduce("\"1\" != 1", c), BooleanNode.FALSE);
}
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