use of com.squarespace.template.Instruction 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");
}
use of com.squarespace.template.Instruction 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");
}
use of com.squarespace.template.Instruction 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);
}
use of com.squarespace.template.Instruction 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!");
}
use of com.squarespace.template.Instruction 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);
}
}
Aggregations