Search in sources :

Example 16 with Context

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

the class CorePredicatesTest method testEqual.

@Test
public void testEqual() throws CodeException {
    CodeMaker mk = maker();
    assertTrue(EQUAL, context("3"), mk.args(" 3"));
    assertTrue(EQUAL, context("\"hello\""), mk.args(" \"hello\""));
    assertTrue(EQUAL, context("[1, 2, 3]"), mk.args(":[1, 2, 3]"));
    assertTrue(EQUAL, context("{\"foo\": \"bar\"}"), mk.args(" {\"foo\":\"bar\"}"));
    assertFalse(EQUAL, context("-1"), mk.args(" 3"));
    assertFalse(EQUAL, context("\"hello\""), mk.args(" \"goodbye\""));
    assertFalse(EQUAL, context("[1, 2, 3]"), mk.args(":1"));
    assertFalse(EQUAL, context("{\"foo\":\"bar\"}"), mk.args(":1"));
    // Compare 2 variables
    String template = "{.equal? a b}yes{.or}no{.end}";
    Context ctx = execute(template, "{\"a\": 1, \"b\": 1}");
    assertEquals(ctx.buffer().toString(), "yes");
    ctx = execute(template, "{\"a\": 1, \"b\": 2}");
    assertEquals(ctx.buffer().toString(), "no");
    // Compare variable with JSON
    template = "{.equal? 1 b}yes{.or}no{.end}";
    ctx = execute(template, "{\"b\": 1}");
    assertEquals(ctx.buffer().toString(), "yes");
    ctx = execute(template, "{\"b\": 2}");
    assertEquals(ctx.buffer().toString(), "no");
}
Also used : Context(com.squarespace.template.Context) CodeMaker(com.squarespace.template.CodeMaker) Test(org.testng.annotations.Test)

Example 17 with Context

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

the class CorePredicatesTest method testDebug.

@Test
public void testDebug() throws CodeException {
    String[] key = new String[] { "a", "b" };
    Context ctx = context("{\"debug\": true, \"a\": {\"b\": 1}}");
    ctx.pushSection(key);
    assertTrue(CorePredicates.DEBUG, ctx);
    for (String json : new String[] { "{\"a\": {\"b\": 1}}", "{\"debug\": 0, \"a\": {\"b\": 1}}" }) {
        ctx = context(json);
        ctx.pushSection(key);
        assertFalse(CorePredicates.DEBUG, ctx);
    }
}
Also used : Context(com.squarespace.template.Context) Test(org.testng.annotations.Test)

Example 18 with Context

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

the class CorePredicatesTest method testOdd.

@Test
public void testOdd() throws CodeException {
    assertTrue(ODD, context("3"));
    assertFalse(ODD, context("4"));
    // Verify behavior inside repeated section
    String template = "{.repeated section items}{.odd?}{@index}{.end}{.end}";
    JsonNode json = json("{\"items\": [{}, {}, {}, {}, {}]}");
    Context ctx = execute(template, json);
    assertEquals(ctx.buffer().toString(), "");
    template = "{.repeated section items}{.odd? @index}{@index}{.end}{.end}";
    ctx = execute(template, json);
    assertEquals(ctx.buffer().toString(), "135");
}
Also used : Context(com.squarespace.template.Context) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.testng.annotations.Test)

Example 19 with Context

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

the class CorePredicatesTest method testEven.

@Test
public void testEven() throws CodeException {
    assertTrue(EVEN, context("4"));
    assertTrue(EVEN, context("-4"));
    assertFalse(EVEN, context("5"));
    assertFalse(EVEN, context("-5"));
    assertFalse(EVEN, context("false"));
    assertFalse(EVEN, context("\"hi\""));
    assertFalse(EVEN, context("\"4\""));
    assertFalse(EVEN, context("{\"foo\": \"bar\"}"));
    // Verify behavior inside repeated section
    String template = "{.repeated section items}{.even?}{@index}{.end}{.end}";
    JsonNode json = json("{\"items\": [{}, {}, {}, {}, {}]}");
    Context ctx = execute(template, json);
    assertEquals(ctx.buffer().toString(), "");
    template = "{.repeated section items}{.even? @index}{@index}{.end}{.end}";
    ctx = execute(template, json);
    assertEquals(ctx.buffer().toString(), "24");
}
Also used : Context(com.squarespace.template.Context) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.testng.annotations.Test)

Example 20 with Context

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

the class ContentFormattersTest method testCapitalize.

@Test
public void testCapitalize() throws CodeException {
    String template = "{user.name|capitalize}";
    String json = "{\"user\": {\"name\": \"Bob Smith\"}}";
    Instruction code = compiler().compile(template).code();
    Context ctx = compiler().newExecutor().code(code).json(json).execute();
    String result = eval(ctx);
    assertEquals(result, "BOB SMITH");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) 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