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