use of com.squarespace.template.Arguments in project template-compiler by Squarespace.
the class CoreFormattersTest method testPluralize.
@Test
public void testPluralize() throws CodeException {
CodeMaker mk = maker();
Arguments args = mk.args("");
assertFormatter(PLURALIZE, "0", "s");
assertFormatter(PLURALIZE, "1", "");
assertFormatter(PLURALIZE, "2", "s");
assertFormatter(PLURALIZE, "3.1415", "s");
args = mk.args(" A");
assertFormatter(PLURALIZE, args, "0", "A");
assertFormatter(PLURALIZE, args, "1", "");
assertFormatter(PLURALIZE, args, "2", "A");
assertFormatter(PLURALIZE, args, "100", "A");
args = mk.args("/A/B");
assertFormatter(PLURALIZE, args, "0", "B");
assertFormatter(PLURALIZE, args, "1", "A");
assertFormatter(PLURALIZE, args, "2", "B");
assertFormatter(PLURALIZE, args, "100", "B");
// Too many args
args = mk.args(":1:2:3:4");
assertInvalidArgs(PLURALIZE, args);
}
use of com.squarespace.template.Arguments in project template-compiler by Squarespace.
the class CoreFormattersTest method testProp.
@Test
public void testProp() throws CodeException {
CodeMaker mk = maker();
Arguments args = mk.args(" foo");
assertFormatter(PROP, args, "{ \"foo\": 123 }", "123");
args = mk.args(" bar");
assertFormatter(PROP, args, "{ \"bar\": \"123\" }", "123");
args = mk.args(" foo");
assertFormatter(PROP, args, "{ \"bar\": 123 }", "");
args = mk.args(" bar quux");
assertFormatter(PROP, args, "{ \"bar\": 123 }", "");
args = mk.args(" foo bar");
assertFormatter(PROP, args, "{}", "");
runner.exec("f-prop-%N.html");
}
use of com.squarespace.template.Arguments in project template-compiler by Squarespace.
the class CoreFormattersTest method testCycle.
@Test
public void testCycle() throws CodeException {
CodeMaker mk = maker();
Arguments args = mk.args(" A B C");
String result = "";
for (int i = -6; i < 6; i++) {
result += format(CYCLE, args, Integer.toString(i));
}
assertEquals("CABCABCABCAB", result);
}
use of com.squarespace.template.Arguments in project template-compiler by Squarespace.
the class CoreFormattersTest method testLookup.
@Test
public void testLookup() throws CodeException {
CodeMaker mk = maker();
Arguments args = mk.args(" var");
assertFormatter(LOOKUP, args, "{\"var\": \"foo\", \"foo\": 123}", "123");
assertFormatter(LOOKUP, args, "{\"var\": \"bar\", \"foo\": 123}", "");
assertFormatter(LOOKUP, args, "{\"var\": \"bar\", \"foo\": 123, \"bar\": 456}", "456");
assertFormatter(LOOKUP, args, "{\"var\": \"foo.bar\", \"foo\": {\"bar\": 123}}", "123");
assertFormatter(LOOKUP, args, "{\"var\": \"data.1\", \"data\": [123, 456]}", "456");
assertFormatter(LOOKUP, args, "{\"var\": \"foo.bar.0\", \"foo\": {\"bar\": [123]}}", "123");
Context ctx = compiler().newExecutor().json("{\"foo\": {\"bar\": 123}, \"baz\": \"bar\"}").template("{foo|lookup baz}").safeExecution(true).execute();
assertContext(ctx, "123");
}
use of com.squarespace.template.Arguments in project template-compiler by Squarespace.
the class CoreFormattersTest method testApplyPartial.
@Test
public void testApplyPartial() throws CodeException {
String partials = "{\"block.item\": \"this {@} value\"}";
String input = "{\"foo\": 123}";
CodeMaker mk = maker();
CodeBuilder cb = builder().text("hi ");
Arguments args = mk.args(" block.item");
APPLY.validateArgs(args);
cb.var("foo", mk.fmt(APPLY, args));
Instruction root = cb.text("!").eof().build();
Context ctx = new Context(JsonUtils.decode(input));
ctx.setCompiler(compiler());
ctx.setPartials(JsonUtils.decode(partials));
ctx.execute(root);
assertContext(ctx, "hi this 123 value!");
}
Aggregations