use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class ContextTest method testLookups.
@Test
public void testLookups() throws CodeException {
CodeMaker mk = maker();
Context ctx = context("{\"a\": {\"b\": [1,2,3]}}");
ctx.push(mk.strarray("a"));
assertEquals(ctx.node(), json("{\"b\": [1,2,3]}"));
String json = "{\"a\": {\"c\": 1}, \"b\": 2}";
RootInst root = builder().section("a").var("b").var("c").end().eof().build();
assertContext(execute(json, root), "21");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class ContextTest method testSafeExecutionMode.
@Test
public void testSafeExecutionMode() throws CodeException {
CodeMaker mk = maker();
Context ctx = context("{\"a\": {}}");
ctx.setPartials(JsonUtils.decode("{\"foo\": \"this {.section x} value\"}"));
ctx.setSafeExecution();
ctx.setCompiler(compiler());
// Apply a partial that contains a syntax error. In safe mode, this should just
// append errors to the context, not raise exceptions.
RootInst root = builder().var("a", mk.fmt(APPLY, mk.args(" foo"))).eof().build();
ctx.execute(root);
assertEquals(ctx.getErrors().size(), 1);
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class HardSoftCodeLimiterTest method testNoLimit.
@Test
public void testNoLimit() throws CodeException {
JsonNode node = JsonUtils.decode("[0,1,2,3,4,5,6,7,8,9]");
Context ctx = new Context(node);
RootInst root = builder().repeated("@").var("@").end().eof().build();
ctx.execute(root);
assertEquals(ctx.buffer().toString(), "0123456789");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class HardSoftCodeLimiterTest method testFormatter.
@Test
public void testFormatter() throws CodeException {
TestHandler handler = new TestHandler(Limit.SOFT, 6);
CodeLimiter limiter = HardSoftCodeLimiter.builder().setSoftLimit(5).setResolution(1).setHandler(handler).build();
JsonNode node = JsonUtils.decode("[\"abc\",\"def\",\"ghi\"]");
Context ctx = new Context(node);
ctx.setCodeLimiter(limiter);
CodeMaker mk = maker();
// Instruction count: root repeated[N] { var truncate }
RootInst root = builder().repeated("@").var("@", mk.fmt(TRUNCATE, mk.args(" 2"))).end().eof().build();
ctx.execute(root);
assertTrue(handler.wasCalled());
assertEquals(ctx.buffer().toString(), "ab...de...gh...");
assertEquals(limiter.instructionCount(), 8);
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class HardSoftCodeLimiterTest method testResolution.
@Test
public void testResolution() throws CodeException {
TestHandler handler = new TestHandler(Limit.HARD, 6);
CodeLimiter limiter = HardSoftCodeLimiter.builder().setHardLimit(4).setResolution(3).setHandler(handler).build();
JsonNode node = JsonUtils.decode("[0,1,2,3,4,5,6,7,8,9]");
Context ctx = new Context(node);
ctx.setCodeLimiter(limiter);
RootInst root = builder().repeated("@").var("@").end().eof().build();
try {
ctx.execute(root);
fail("Expected CODE_LIMIT_REACHED exception");
} catch (CodeExecuteException e) {
assertTrue(handler.wasCalled());
assertEquals(handler.limitType(), Limit.HARD);
assertEquals(e.getErrorInfo().getType(), CODE_LIMIT_REACHED);
assertEquals(limiter.instructionCount(), 6);
}
}
Aggregations