Search in sources :

Example 56 with Context

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

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

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

Example 59 with Context

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

the class CoreFormattersTest method testDateNoTimeZone.

@Test
public void testDateNoTimeZone() throws CodeException {
    Context ctx = compiler().newExecutor().template("{@|date %Y}").json("167200000").execute();
    assertEquals(ctx.buffer().toString(), "1970");
}
Also used : Context(com.squarespace.template.Context) Test(org.testng.annotations.Test)

Example 60 with Context

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

the class InternationalFormattersTest method format.

private String format(String locale, Formatter impl, Arguments args, String json) throws CodeException {
    Context ctx = new Context(JsonUtils.decode(json), Locale.forLanguageTag(locale));
    impl.validateArgs(args);
    Variables variables = new Variables("@", ctx.node());
    impl.apply(ctx, args, variables);
    return variables.first().node().asText();
}
Also used : Context(com.squarespace.template.Context) Variables(com.squarespace.template.Variables)

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