use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeValidityTest method testRepeated.
@Test
public void testRepeated() throws CodeException {
String jsonData = "{\"foo\": [0, 0, 0]}";
RootInst root = builder().repeated("foo").text("1").var("@").alternatesWith().text("-").end().eof().build();
assertContext(execute(jsonData, root), "10-10-10");
root = builder().repeated("bar").text("1").end().eof().build();
assertContext(execute("{}", root), "");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeValidityTest method testSection.
@Test
public void testSection() throws CodeException {
RootInst root = builder().section("foo").var("bar").or().text("B").end().eof().build();
assertContext(execute("{\"foo\": 1}", root), "");
assertContext(execute("{}", root), "B");
assertContext(execute("{\"foo\": {\"bar\": 1}}", root), "1");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class ContextTest method testLoggingHook.
@Test
public void testLoggingHook() throws CodeException {
// Add a hook to ensure that all unexpected exceptions are logged.
final List<Exception> exceptions = new ArrayList<>();
LoggingHook hook = new LoggingHook() {
@Override
public void log(Exception e) {
exceptions.add(e);
}
};
CodeMaker mk = maker();
Context ctx = context("{\"a\": \"b\"}");
ctx.setSafeExecution();
ctx.setLoggingHook(hook);
// Execute the 'npe' formatter.
RootInst root = builder().var("a", mk.fmt(NPE)).eof().build();
ctx.execute(root);
assertEquals(exceptions.size(), 1);
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class HardSoftCodeLimiterTest method testHardLimit.
@Test
public void testHardLimit() throws CodeException {
TestHandler handler = new TestHandler(Limit.HARD, 6);
CodeLimiter limiter = HardSoftCodeLimiter.builder().setHardLimit(5).setResolution(1).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);
}
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class HardSoftCodeLimiterTest method testApplyPartialSubcontext.
@Test
public void testApplyPartialSubcontext() throws CodeException {
TestHandler handler = new TestHandler(Limit.SOFT, 6);
CodeLimiter limiter = HardSoftCodeLimiter.builder().setSoftLimit(5).setResolution(1).setHandler(handler).build();
JsonNode node = JsonUtils.decode("[0,1,2]");
JsonNode partials = JsonUtils.decode("{\"foo\":\"{@}\"}");
Context ctx = new Context(node);
ctx.setPartials(partials);
ctx.setCompiler(compiler());
ctx.setCodeLimiter(limiter);
CodeMaker mk = maker();
// Instruction count: root repeated[N] { var apply { root var } }
RootInst root = builder().repeated("@").var("@", mk.fmt(APPLY, mk.args(" foo"))).end().eof().build();
ctx.execute(root);
assertTrue(handler.wasCalled());
assertEquals(ctx.buffer().toString(), "012");
assertEquals(limiter.instructionCount(), 14);
}
Aggregations