use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeExecuteTest method testCtxVar.
@Test
public void testCtxVar() throws CodeException {
RootInst root = builder().ctxvar("@foo", "src=image.src", "foc=style.focus", "unk=var.not.exist").var("@foo.src").text(" (").var("@foo.foc.x").text(",").var("@foo.foc.y").text(") ").var("@foo.unk").eof().build();
assertEquals(repr(root), "{.ctx @foo src=image.src foc=style.focus unk=var.not.exist}" + "{@foo.src} ({@foo.foc.x},{@foo.foc.y}) {@foo.unk}");
Context ctx = execute("{\"image\":{\"src\":\"./face.jpg\"}," + "\"style\":{\"focus\":{\"x\":0.3,\"y\":0.7}}}", root);
assertContext(ctx, "./face.jpg (0.3,0.7) ");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeExecuteTest method testSection.
@Test
public void testSection() throws CodeException {
RootInst root = builder().section("foo.bar").var("baz").end().eof().build();
String json = "{\"foo\": {\"bar\": {\"baz\": 123}}}";
assertEquals(repr(root), "{.section foo.bar}{baz}{.end}");
assertContext(execute(json, root), "123");
root = builder().section("foo.1").var("@").end().eof().build();
json = "{\"foo\": [\"a\", \"b\"]}";
assertEquals(repr(root), "{.section foo.1}{@}{.end}");
assertContext(execute(json, root), "b");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeExecuteTest method testRepeatIndex.
@Test
public void testRepeatIndex() throws CodeException {
RootInst root = builder().repeated("foo").var("@index").text("-").var("@index0").text(" ").end().eof().build();
assertEquals(repr(root), "{.repeated section foo}{@index}-{@index0} {.end}");
assertContext(execute("{\"foo\": [8, 8, 8]}", root), "1-0 2-1 3-2 ");
assertContext(execute("{\"foo\": []}", root), "");
assertContext(execute("{}", root), "");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeExecuteTest method testSectionMissing.
@Test
public void testSectionMissing() throws CodeException {
CodeBuilder builder = builder();
builder.section("foo").text("A").or().text("B").end();
RootInst root = builder.eof().build();
assertContext(execute("{\"foo\": 123}", root), "A");
assertContext(execute("{}", root), "B");
assertContext(execute("{\"foo\": null}", root), "B");
assertContext(execute("{\"foo\": []}", root), "B");
}
use of com.squarespace.template.Instructions.RootInst in project template-compiler by Squarespace.
the class CodeExecuteTest method testBindVar.
@Test
public void testBindVar() throws CodeException {
RootInst root = builder().bindvar("@name", "foo").var("@name").eof().build();
assertEquals(repr(root), "{.var @name foo}{@name}");
assertContext(execute("{\"foo\": 123}", root), "123");
// Bind variable to element inside array
root = builder().bindvar("@name", "foo.1").var("@name").eof().build();
assertEquals(repr(root), "{.var @name foo.1}{@name}");
assertContext(execute("{\"foo\": [1,2,3]}", root), "2");
// Resolve variable in outer scope
root = builder().bindvar("@val", "foo").section("bar").var("@val").end().eof().build();
assertEquals(repr(root), "{.var @val foo}{.section bar}{@val}{.end}");
assertContext(execute("{\"foo\": 1, \"bar\": 2}", root), "1");
// Full example with nesting
String json = "{\"managers\":[" + "{\"name\": \"Bill\", \"employees\": [{\"name\": \"Peter\"}, {\"name\": \"Michael\"}]}," + "{\"name\": \"Bob\", \"employees\": [{\"name\": \"Samir\"}]}" + "]}";
CodeBuilder cb = builder();
cb.repeated("managers").bindvar("@boss", "name").bindvar("@boss-idx", "@index");
cb.repeated("employees").var("@boss-idx").text(".").var("@index").text(" ");
cb.var("name").text(" is managed by ").var("@boss").text("\n").end();
root = cb.alternatesWith().text("---\n").end().eof().build();
assertContext(execute(json, root), "1.1 Peter is managed by Bill\n" + "1.2 Michael is managed by Bill\n" + "---\n" + "2.1 Samir is managed by Bob\n");
// Example with dotted access on @var
json = "{\"person\": {\"name\": \"Larry\", \"age\": \"21\"}}";
root = builder().bindvar("@person", "person").var("@person.name").text(" is ").var("@person.age").eof().build();
assertContext(execute(json, root), "Larry is 21");
}
Aggregations