use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class DecimalFormatterTest method format.
private static String format(CLDR.Locale locale, Arguments args, String json) throws CodeException {
Context ctx = new Context(JsonUtils.decode(json));
ctx.cldrLocale(locale);
DECIMAL.validateArgs(args);
Variables variables = new Variables("@", ctx.node());
DECIMAL.apply(ctx, args, variables);
return variables.first().node().asText();
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class MoneyFormatterTest method format.
private static String format(CLDR.Locale locale, Arguments args, String json) throws CodeException {
Context ctx = new Context(JsonUtils.decode(json));
ctx.cldrLocale(locale);
MONEY.validateArgs(args);
Variables variables = new Variables("@", ctx.node());
MONEY.apply(ctx, args, variables);
return variables.first().node().asText();
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class UnitLocalesTest method test.
private void test(CLDR.Locale locale, String templatePath, String expectedPath) throws Exception {
Pair<CompiledTemplate, JsonNode> compiled = load(templatePath, "units/_numbers.json");
Context ctx = new Context(compiled._2);
ctx.cldrLocale(locale);
ctx.execute(compiled._1.code());
String actual = reformat(ctx.buffer().toString());
String expected = reformat(GeneralUtils.loadResource(getClass(), expectedPath));
if (!actual.trim().equals(expected.trim())) {
String diff = TestCaseParser.diff(expected, actual);
throw new AssertionError("File '" + expectedPath + "' output does not match:\n" + diff);
}
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class CoreFormattersTest method testApplyPartialErrorSafe.
@Test
public void testApplyPartialErrorSafe() throws CodeException {
String template = "{@|apply block}";
String partials = "{\"block\": \"{@|no-formatter}\"}";
Context ctx = compiler().newExecutor().template(template).partialsMap(((ObjectNode) JsonUtils.decode(partials))).json("123").safeExecution(true).execute();
assertEquals(ctx.getErrors().size(), 1);
assertEquals(ctx.getErrors().get(0).getType(), COMPILE_PARTIAL_SYNTAX);
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class CoreFormattersTest method testTruncate.
@Test
public void testTruncate() throws CodeException {
CodeMaker mk = maker();
assertFormatter(TRUNCATE, mk.args(""), "\"foo\"", "foo");
assertFormatter(TRUNCATE, mk.args(" 5 .."), "\"foo bar baz\"", "foo ..");
assertFormatter(TRUNCATE, mk.args(" 100 .."), "\"foo bar baz\"", "foo bar baz");
Context ctx = compiler().newExecutor().template("{@|truncate xyz}").safeExecution(true).execute();
assertEquals(ctx.getErrors().size(), 1);
assertEquals(ctx.getErrors().get(0).getType(), SyntaxErrorType.FORMATTER_ARGS_INVALID);
runner.exec("f-truncate-%N.html");
}
Aggregations