Search in sources :

Example 11 with Instruction

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

the class CoreFormattersTest method testApplyPartialErrorSyntax.

@Test
public void testApplyPartialErrorSyntax() throws CodeException {
    String template = "{@|apply block}";
    String input = "{}";
    String partials = "{\"block\": \"{.section foo}{@}\"}";
    Instruction inst = compiler().compile(template).code();
    Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).safeExecution(true).execute();
    assertContext(ctx, "");
    assertEquals(ctx.getErrors().size(), 1);
    assertEquals(ctx.getErrors().get(0).getType(), COMPILE_PARTIAL_SYNTAX);
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 12 with Instruction

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

the class CoreFormattersTest method testApplyPartialPrivate.

@Test
public void testApplyPartialPrivate() throws CodeException {
    // The "private" argument hides the entire parent context from the partial
    String template = "{child|apply block private}{child|apply block}";
    String partials = "{\"block\": \"hello {name}\"}";
    String input = "{\"name\": \"bob\", \"child\": {}}";
    Instruction inst = compiler().compile(template).code();
    Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).safeExecution(true).execute();
    assertContext(ctx, "hello hello bob");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 13 with Instruction

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

the class CoreFormattersTest method testApplyPartialNested.

@Test
public void testApplyPartialNested() 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).execute();
    assertContext(ctx, "1234");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 14 with Instruction

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

the class CoreFormattersTest method formatDate.

private String formatDate(String format, long timestamp, String tzId, Locale locale) throws CodeException {
    String template = "{time|date " + format + "}";
    String json = getDateTestJson(timestamp, tzId);
    Instruction code = compiler().compile(template).code();
    Context ctx = compiler().newExecutor().code(code).json(json).locale(locale).execute();
    return eval(ctx);
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction)

Aggregations

Context (com.squarespace.template.Context)14 Instruction (com.squarespace.template.Instruction)14 Test (org.testng.annotations.Test)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Arguments (com.squarespace.template.Arguments)1 CodeBuilder (com.squarespace.template.CodeBuilder)1 CodeExecuteException (com.squarespace.template.CodeExecuteException)1 CodeMaker (com.squarespace.template.CodeMaker)1 CompiledTemplate (com.squarespace.template.CompiledTemplate)1 ErrorInfo (com.squarespace.template.ErrorInfo)1