Search in sources :

Example 16 with RootInst

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");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 17 with RootInst

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);
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 18 with RootInst

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");
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 19 with RootInst

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);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 20 with RootInst

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);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Aggregations

RootInst (com.squarespace.template.Instructions.RootInst)42 Test (org.testng.annotations.Test)41 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 DecimalNode (com.fasterxml.jackson.databind.node.DecimalNode)1 AlternatesWithInst (com.squarespace.template.Instructions.AlternatesWithInst)1 IfInst (com.squarespace.template.Instructions.IfInst)1 IfPredicateInst (com.squarespace.template.Instructions.IfPredicateInst)1 PredicateInst (com.squarespace.template.Instructions.PredicateInst)1 RepeatedInst (com.squarespace.template.Instructions.RepeatedInst)1 SectionInst (com.squarespace.template.Instructions.SectionInst)1 TextInst (com.squarespace.template.Instructions.TextInst)1 VariableInst (com.squarespace.template.Instructions.VariableInst)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1