Search in sources :

Example 11 with RootInst

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

the class CodeValidityTest method testSectionScope.

@Test
public void testSectionScope() throws CodeException {
    RootInst root = builder().section("a").var("a").or().var("b").end().eof().build();
    assertContext(execute("{\"a\": 1, \"b\": \"B\"}", root), "1");
    assertContext(execute("{\"a\": 0, \"b\": \"B\"}", root), "B");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 12 with RootInst

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

the class CodeValidityTest method testRepeatedOr.

@Test
public void testRepeatedOr() throws CodeException {
    String jsonData = "{\"a\": [0, 0, 0]}";
    RootInst root = builder().repeated("a").var("@").alternatesWith().text("-").or().text("X").end().eof().build();
    assertContext(execute(jsonData, root), "0-0-0");
    jsonData = "{\"b\": [0, 0, 0]}";
    assertContext(execute(jsonData, root), "X");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 13 with RootInst

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

the class CodeValidityTest method testAlternatesWith.

/**
 * Sort of confusing to add sections to ALTERNATES_WITH block, since we're always pointing to the
 * next element in the array when that block is executed. Typical uses of ALTERNATES_WITH just output
 * static text, but nesting things inside those blocks is possible.
 */
@Test
public void testAlternatesWith() throws CodeException {
    RootInst root = builder().repeated("a").alternatesWith().section("b").var("@").end().end().eof().build();
    String json = "{\"a\": [{\"b\": 1}, {\"b\": 2}, {\"b\": 3}, {\"b\": 4}]}";
    assertContext(execute(json, root), "123");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 14 with RootInst

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

the class CodeValidityTest method testVariables.

@Test
public void testVariables() throws CodeException {
    RootInst root = builder().var("foo").var("bar").eof().build();
    assertContext(execute("{\"foo\": 1, \"bar\": 2}", root), "12");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 15 with RootInst

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

the class CodeValidityTest method testText.

@Test
public void testText() throws CodeException {
    RootInst root = builder().text("foo").text("bar").eof().build();
    assertContext(execute("{}", root), "foobar");
    root = builder().eof().build();
    assertContext(execute("{}", root), "");
}
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