Search in sources :

Example 31 with RootInst

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

Example 32 with RootInst

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

Example 33 with RootInst

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

Example 34 with RootInst

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

Example 35 with RootInst

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