Search in sources :

Example 6 with RootInst

use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.

the class CodeExecuteTest method testRepeatOr.

@Test
public void testRepeatOr() throws CodeException {
    RootInst root = builder().repeated("foo").text("A").var("@").or().text("B").end().eof().build();
    assertEquals(repr(root), "{.repeated section foo}A{@}{.or}B{.end}");
    assertContext(execute("{\"foo\": [1, 2, 3]}", root), "A1A2A3");
    assertContext(execute("{\"foo\": []}", root), "B");
    assertContext(execute("{}", root), "B");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 7 with RootInst

use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.

the class CodeValidityTest method testComments.

@Test
public void testComments() throws CodeException {
    RootInst root = builder().comment("foo").mcomment("bar\nbaz").eof().build();
    assertContext(execute("{}", root), "");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 8 with RootInst

use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.

the class CodeValidityTest method testMacro.

@Test
public void testMacro() throws CodeException {
    CodeMaker mk = maker();
    RootInst root = builder().macro("foo").text("A").var("@").end().var("@", mk.fmt(APPLY, mk.args(" foo"))).eof().build();
    assertContext(execute("1", root), "A1");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 9 with RootInst

use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.

the class CodeValidityTest method testRepeatedIndex.

@Test
public void testRepeatedIndex() throws CodeException {
    String jsonData = "{\"foo\": [\"A\", \"B\", \"C\"]}";
    CodeBuilder cb = builder();
    cb.repeated("foo").var("@").var("@index").alternatesWith().text(".").end().eof();
    RootInst root = cb.build();
    // @index is 1-based
    assertContext(execute(jsonData, root), "A1.B2.C3");
    jsonData = "{\"a\": [\"x\", \"y\"]}";
    cb = builder();
    cb.repeated("a").var("@").section("@").var("@").var("@index").end().alternatesWith().text(".").end().eof();
    assertContext(execute(jsonData, cb.build()), "xx1.yy2");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 10 with RootInst

use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.

the class CodeValidityTest method testVariableFormatters.

@Test
public void testVariableFormatters() throws CodeException {
    RootInst root = builder().var("a", SAFE).eof().build();
    assertContext(execute("{\"a\": \"x <> y\"}", root), "x  y");
    // Chain formatters together.
    CodeMaker mk = maker();
    List<FormatterCall> formatters = mk.formatters(mk.fmt(SAFE), mk.fmt(TRUNCATE, mk.args(" 5")));
    root = builder().var("a", formatters).eof().build();
    assertContext(execute("{\"a\": \"x <> y <> z\"}", root), "x  y ...");
}
Also used : 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