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");
}
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), "");
}
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");
}
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");
}
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 ...");
}
Aggregations