use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeValidityTest method testPredicate.
@Test
public void testPredicate() throws CodeException {
CodeBuilder cb = builder().section("@").predicate(PLURAL).text("A");
cb.or(CorePredicates.SINGULAR).text("B");
// end or
cb.or().text("C").end();
// section
cb.end();
RootInst root = cb.eof().build();
assertEquals(repr(root), "{.section @}{.plural?}A{.or singular?}B{.or}C{.end}{.end}");
assertContext(execute("174", root), "A");
assertContext(execute("174.35", root), "A");
assertContext(execute("1", root), "B");
assertContext(execute("1.0", root), "B");
// zero is false-y, so entire section is skipped
assertContext(execute("0", root), "");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeValidityTest method testIfPredicate.
@Test
public void testIfPredicate() throws CodeException {
CodeMaker mk = maker();
RootInst root = builder().ifpred(PLURAL).text("A").or(SINGULAR).text("B").or().text("C").end().eof().build();
assertContext(execute("5", root), "A");
assertContext(execute("1", root), "B");
assertContext(execute("0", root), "C");
root = builder().ifpred(EQUAL, mk.args(" 2")).text("A").or().text("B").end().eof().build();
assertContext(execute("2", root), "A");
assertContext(execute("1", root), "B");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeValidityTest method testIfExpression.
@Test
public void testIfExpression() throws CodeException {
CodeMaker mk = maker();
// AND TESTS
CodeBuilder cb = builder().ifexpn(mk.strlist("a", "b"), mk.oplist(Operator.LOGICAL_AND));
RootInst root = cb.text("A").or().text("B").end().eof().build();
assertContext(execute("{\"a\": 1, \"b\": 3.14159}", root), "A");
assertContext(execute("{\"a\": true, \"b\": \"Bill\"}", root), "A");
assertContext(execute("{\"a\": 1}", root), "B");
assertContext(execute("{\"a\": true, \"b\": false}", root), "B");
assertContext(execute("{}", root), "B");
// Index into nested arrays.
cb = builder().ifexpn(mk.strlist("a.0.b", "a.1.b"), mk.oplist(Operator.LOGICAL_AND));
root = cb.text("A").or().text("B").end().eof().build();
assertContext(execute("{\"a\": [{\"b\": 1}, {\"b\": true}]}", root), "A");
assertContext(execute("{\"a\": [{\"b\": 3.14}, {\"b\": \"hi\"}]}", root), "A");
assertContext(execute("{\"a\": [{\"b\": null}, {\"b\": true}]}", root), "B");
assertContext(execute("{\"a\": [{\"b\": \"\"}, {\"b\": true}]}", root), "B");
root = builder().ifexpn(mk.strlist("a"), mk.oplist()).text("A").end().eof().build();
assertContext(execute("{\"a\": 1}", root), "A");
assertContext(execute("{}", root), "");
// OR TESTS
cb = builder().ifexpn(mk.strlist("a", "b", "c"), mk.oplist(Operator.LOGICAL_OR, Operator.LOGICAL_OR));
root = cb.text("A").or().text("B").end().eof().build();
assertContext(execute("{\"a\": true}", root), "A");
assertContext(execute("{\"b\": \"Bill\"}", root), "A");
assertContext(execute("{\"c\": 3.14159}", root), "A");
assertContext(execute("{\"a\": false, \"b\": 0, \"c\": \"Fred\"}", root), "A");
assertContext(execute("{\"a\": {}, \"b\": {\"c\": 1}}", root), "A");
assertContext(execute("{}", root), "B");
assertContext(execute("{\"a\": \"\"}", root), "B");
assertContext(execute("{\"a\": null}", root), "B");
assertContext(execute("{\"c\": false}", root), "B");
assertContext(execute("{\"c\": false, \"b\": 0}", root), "B");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeValidityTest method testAlternatesWithOr.
@Test
public void testAlternatesWithOr() throws CodeException {
RootInst root = builder().repeated("a").text("A").alternatesWith().text("-").or().text("B").end().eof().build();
assertContext(execute("{\"a\": [1,2,3]}", root), "A-A-A");
assertContext(execute("{}", root), "B");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeValidityTest method testOrWithSection.
@Test
public void testOrWithSection() throws CodeException {
// This construction makes almost no sense but is possible in the language,
// so adding it for coverage.
RootInst root = builder().section("@").or().predicate(PLURAL).or().text("A").var("@").end().end().eof().build();
assertEquals(repr(root), "{.section @}{.or}{.plural?}{.or}A{@}{.end}{.end}");
assertContext(execute("0", root), "A0");
}
Aggregations