Search in sources :

Example 6 with Context

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

the class CoreFormattersTest method testLookup.

@Test
public void testLookup() throws CodeException {
    CodeMaker mk = maker();
    Arguments args = mk.args(" var");
    assertFormatter(LOOKUP, args, "{\"var\": \"foo\", \"foo\": 123}", "123");
    assertFormatter(LOOKUP, args, "{\"var\": \"bar\", \"foo\": 123}", "");
    assertFormatter(LOOKUP, args, "{\"var\": \"bar\", \"foo\": 123, \"bar\": 456}", "456");
    assertFormatter(LOOKUP, args, "{\"var\": \"foo.bar\", \"foo\": {\"bar\": 123}}", "123");
    assertFormatter(LOOKUP, args, "{\"var\": \"data.1\", \"data\": [123, 456]}", "456");
    assertFormatter(LOOKUP, args, "{\"var\": \"foo.bar.0\", \"foo\": {\"bar\": [123]}}", "123");
    Context ctx = compiler().newExecutor().json("{\"foo\": {\"bar\": 123}, \"baz\": \"bar\"}").template("{foo|lookup baz}").safeExecution(true).execute();
    assertContext(ctx, "123");
}
Also used : Context(com.squarespace.template.Context) Arguments(com.squarespace.template.Arguments) CodeMaker(com.squarespace.template.CodeMaker) Test(org.testng.annotations.Test)

Example 7 with Context

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

the class CoreFormattersTest method testApplyPartialMissingSafe.

@Test
public void testApplyPartialMissingSafe() throws CodeException {
    Context ctx = compiler().newExecutor().template("{@|apply foo}").json("{}").partialsMap((ObjectNode) JsonUtils.decode("{}")).safeExecution(true).execute();
    assertEquals(ctx.getErrors().size(), 1);
    assertEquals(ctx.getErrors().get(0).getType(), APPLY_PARTIAL_MISSING);
}
Also used : Context(com.squarespace.template.Context) Test(org.testng.annotations.Test)

Example 8 with Context

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

the class CoreFormattersTest method testDatePartial.

/**
 * Ensure that partials can see global scope.
 */
@Test
public void testDatePartial() throws CodeException {
    String tzNewYork = "America/New_York";
    String format = "%Y-%m-%d %H:%M:%S %Z";
    String json = getDateTestJson(MAY_13_2013_010000_UTC, tzNewYork);
    String template = "{@|apply block}";
    String partials = "{\"block\": \"{time|date " + format + "}\"}";
    Context ctx = new Context(JsonUtils.decode(json));
    ctx.setCompiler(compiler());
    ctx.setPartials(JsonUtils.decode(partials));
    Instruction inst = compiler().compile(template).code();
    ctx.execute(inst);
    assertContext(ctx, "2013-05-12 21:00:00 EDT");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 9 with Context

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

the class CoreFormattersTest method testIter.

@Test
public void testIter() throws CodeException {
    String template = "{.repeated section foo}{@|iter}{.end}";
    String input = "{\"foo\": [\"a\", \"b\", \"c\"]}";
    Instruction inst = compiler().compile(template).code();
    Context ctx = new Context(JsonUtils.decode(input));
    ctx.setCompiler(compiler());
    ctx.execute(inst);
    assertContext(ctx, "123");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 10 with Context

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

the class CoreFormattersTest method testApplyPartialNestedRecursion.

@Test
public void testApplyPartialNestedRecursion() throws CodeException {
    String template = "{@|apply one}";
    String partials = "{\"one\": \"1{@|apply two}\", \"two\": \"2{@|apply three}\", " + "\"three\": \"3{@|apply four}\", \"four\": \"4{@|apply one}\"}";
    String input = "{}";
    Instruction inst = compiler().compile(template).code();
    Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).safeExecution(true).execute();
    assertContext(ctx, "1234123412341234");
    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)

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