Search in sources :

Example 36 with RootInst

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

Example 37 with RootInst

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

Example 38 with RootInst

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

Example 39 with RootInst

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

Example 40 with RootInst

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);
}
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