Search in sources :

Example 11 with Context

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

the class CoreFormattersTest method testApplyPartial.

@Test
public void testApplyPartial() throws CodeException {
    String partials = "{\"block.item\": \"this {@} value\"}";
    String input = "{\"foo\": 123}";
    CodeMaker mk = maker();
    CodeBuilder cb = builder().text("hi ");
    Arguments args = mk.args(" block.item");
    APPLY.validateArgs(args);
    cb.var("foo", mk.fmt(APPLY, args));
    Instruction root = cb.text("!").eof().build();
    Context ctx = new Context(JsonUtils.decode(input));
    ctx.setCompiler(compiler());
    ctx.setPartials(JsonUtils.decode(partials));
    ctx.execute(root);
    assertContext(ctx, "hi this 123 value!");
}
Also used : Context(com.squarespace.template.Context) Arguments(com.squarespace.template.Arguments) CodeMaker(com.squarespace.template.CodeMaker) Instruction(com.squarespace.template.Instruction) CodeBuilder(com.squarespace.template.CodeBuilder) Test(org.testng.annotations.Test)

Example 12 with Context

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

the class CoreFormattersTest method testApplyPartialMissing.

@Test
public void testApplyPartialMissing() throws CodeException {
    String template = "{@|apply foo}";
    String partials = "{\"block\": \"hello\"}";
    String input = "{}";
    Instruction inst = compiler().compile(template).code();
    Context ctx = new Context(JsonUtils.decode(input));
    ctx.setCompiler(compiler());
    ctx.setPartials(JsonUtils.decode(partials));
    try {
        ctx.execute(inst);
        fail("Expected exception");
    } catch (CodeExecuteException e) {
        assertEquals(e.getErrorInfo().getType(), APPLY_PARTIAL_MISSING);
    }
}
Also used : Context(com.squarespace.template.Context) CodeExecuteException(com.squarespace.template.CodeExecuteException) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 13 with Context

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

the class CoreFormattersTest method testApplyPartialRecursion.

@Test
public void testApplyPartialRecursion() throws CodeException {
    String template = "{@|apply one}";
    String partials = "{\"one\": \"1{@|apply two}\", \"two\": \"2{@|apply one}\"}";
    String input = "{}";
    Instruction inst = compiler().compile(template).code();
    Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).maxPartialDepth(6).safeExecution(true).execute();
    assertContext(ctx, "121212");
    assertEquals(ctx.getErrors().size(), 1);
    assertEquals(ctx.getErrors().get(0).getType(), APPLY_PARTIAL_RECURSION_DEPTH);
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 14 with Context

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

the class CoreFormattersTest method testApplyPartialRecursionDepth.

@Test
public void testApplyPartialRecursionDepth() throws CodeException {
    String template = "{@|apply one}";
    String partials = "{\"one\": \"1{@|apply two}\", \"two\": \"2{@|apply three}\", " + "\"three\": \"3{@|apply four}\", \"four\": \"4\"}";
    String input = "{}";
    Instruction inst = compiler().compile(template).code();
    Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).safeExecution(true).maxPartialDepth(3).execute();
    assertContext(ctx, "123");
    assertEquals(ctx.getErrors().size(), 1);
    assertEquals(ctx.getErrors().get(0).getType(), APPLY_PARTIAL_RECURSION_DEPTH);
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 15 with Context

use of com.squarespace.template.Context 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

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