Search in sources :

Example 1 with CodeSyntaxException

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

the class CorePredicatesTest method testJsonPredicateVariable.

@Test
public void testJsonPredicateVariable() throws CodeException {
    Context ctx = context("{\"number\": 1, \"foo\": {\"bar\": 1}}");
    ctx.pushSection(new String[] { "foo", "bar" });
    assertTrue(EQUAL, ctx, maker().args(" number"));
    String template = "{.repeated section nums}{.equal? @index 2}{@}{.end}{.end}";
    JsonNode json = json("{\"nums\": [10, 20, 30]}");
    ctx = execute(template, json);
    assertEquals(ctx.buffer().toString(), "20");
    // Ensure that invalid arguments are caught and proper error raised.
    template = "{.equal? 1 -}#{.end}";
    try {
        execute("{.equal? 1 .}#{.end}", json("{}"));
        Assert.fail("Expected CodeSyntaxException PREDICATE_ARGS_INVALID to be thrown");
    } catch (CodeSyntaxException e) {
        assertEquals(e.getErrorInfo().getType(), SyntaxErrorType.PREDICATE_ARGS_INVALID);
    }
}
Also used : Context(com.squarespace.template.Context) CodeSyntaxException(com.squarespace.template.CodeSyntaxException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.testng.annotations.Test)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 CodeSyntaxException (com.squarespace.template.CodeSyntaxException)1 Context (com.squarespace.template.Context)1 Test (org.testng.annotations.Test)1