Search in sources :

Example 21 with RootInst

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

the class InstructionReprTest method testRootRepr.

@Test
public void testRootRepr() {
    CodeMaker mk = maker();
    RootInst r1 = mk.root();
    assertEquals(r1.repr(), "");
    r1.getConsequent().add(mk.text("A"));
    assertEquals(r1.repr(), "A");
    r1.getConsequent().add(mk.text("B"));
    r1.getConsequent().add(mk.text("C"));
    assertEquals(r1.repr(), "ABC");
    assertEquals(ReprEmitter.get(r1, false), "");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 22 with RootInst

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

the class CodeExecuteTest method testVariableScope.

@Test
public void testVariableScope() throws CodeException {
    RootInst root = builder().repeated("names").bindvar("@curr", "name").alternatesWith().var("@curr").end().eof().build();
    String json = "{\"names\": [{\"name\": \"bob\"}, {\"name\": \"larry\"}]}";
    assertContext(execute(json, root), "bob");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 23 with RootInst

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

the class CodeExecuteTest method testVariable.

@Test
public void testVariable() throws CodeException {
    RootInst root = builder().var("foo.bar").eof().build();
    assertContext(execute("{\"foo\": {\"bar\": 123}}", root), "123");
    root = builder().var("@").eof().build();
    assertContext(execute("3.14159", root), "3.14159");
    assertContext(execute("123.000", root), "123");
    assertContext(execute("null", root), "");
    root = builder().var("foo.2.bar").eof().build();
    assertContext(execute("{\"foo\": [0, 0, {\"bar\": \"hi\"}]}", root), "hi");
    root = builder().var("foo").eof().build();
    assertContext(execute("{\"foo\": [\"a\", \"b\", 123]}", root), "a,b,123");
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 24 with RootInst

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

the class CodeExecuteTest method testText.

@Test
public void testText() throws CodeException {
    String expected = "defjkl";
    RootInst root = builder().text(ALPHAS, 3, 6).text(ALPHAS, 9, 12).eof().build();
    assertContext(execute("{}", root), expected);
}
Also used : RootInst(com.squarespace.template.Instructions.RootInst) Test(org.testng.annotations.Test)

Example 25 with RootInst

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

the class ReferenceScanner method extract.

/**
 * Extracts reference metrics from a single instruction.
 */
public void extract(Instruction inst) {
    String name = null;
    if (inst == null) {
        return;
    }
    if (!(inst instanceof RootInst)) {
        refs.increment(inst);
    }
    switch(inst.getType()) {
        case TEXT:
            refs.textBytes += ((TextInst) inst).getView().length();
            break;
        case ALTERNATES_WITH:
            AlternatesWithInst alternatesWith = (AlternatesWithInst) inst;
            extractBlock(alternatesWith.getConsequent());
            extract(alternatesWith.getAlternative());
            break;
        case IF:
            {
                BlockInstruction blockInst = (BlockInstruction) inst;
                refs.addIfInstruction(blockInst);
                if (inst instanceof IfInst) {
                    IfInst ifInst = (IfInst) inst;
                    for (Object[] var : ifInst.getVariables()) {
                        name = ReprEmitter.get(var);
                        refs.addVariable(name);
                    }
                } else {
                    IfPredicateInst ifInst = (IfPredicateInst) inst;
                    refs.increment(ifInst.getPredicate());
                }
                extractBlock(blockInst.getConsequent());
                extract(blockInst.getAlternative());
                break;
            }
        case OR_PREDICATE:
        case PREDICATE:
            PredicateInst predicateInst = (PredicateInst) inst;
            Predicate predicate = predicateInst.getPredicate();
            if (predicate != null) {
                refs.increment(predicate);
                predicate.addReferences(predicateInst.getArguments(), refs);
            }
            extractBlock(predicateInst.getConsequent());
            extract(predicateInst.getAlternative());
            break;
        case REPEATED:
            RepeatedInst repeated = (RepeatedInst) inst;
            name = ReprEmitter.get(repeated.getVariable());
            refs.pushSection(name);
            extractBlock(repeated.getConsequent());
            extract(repeated.getAlternative());
            extract(repeated.getAlternatesWith());
            refs.popSection();
            break;
        case ROOT:
            extractBlock(((RootInst) inst).getConsequent());
            break;
        case SECTION:
            SectionInst section = (SectionInst) inst;
            name = ReprEmitter.get(section.getVariable());
            refs.pushSection(name);
            extractBlock(section.getConsequent());
            extract(section.getAlternative());
            refs.popSection();
            break;
        case VARIABLE:
            VariableInst varInst = (VariableInst) inst;
            name = ReprEmitter.get(varInst.getVariables());
            refs.addVariable(name);
            for (FormatterCall call : varInst.getFormatters()) {
                refs.increment(call.getFormatter());
            }
            break;
        default:
            break;
    }
}
Also used : PredicateInst(com.squarespace.template.Instructions.PredicateInst) IfPredicateInst(com.squarespace.template.Instructions.IfPredicateInst) SectionInst(com.squarespace.template.Instructions.SectionInst) RepeatedInst(com.squarespace.template.Instructions.RepeatedInst) RootInst(com.squarespace.template.Instructions.RootInst) IfPredicateInst(com.squarespace.template.Instructions.IfPredicateInst) VariableInst(com.squarespace.template.Instructions.VariableInst) TextInst(com.squarespace.template.Instructions.TextInst) IfInst(com.squarespace.template.Instructions.IfInst) AlternatesWithInst(com.squarespace.template.Instructions.AlternatesWithInst)

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