Search in sources :

Example 1 with Context

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();
}
Also used : Context(com.squarespace.template.Context) Variables(com.squarespace.template.Variables)

Example 2 with Context

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();
}
Also used : Context(com.squarespace.template.Context) Variables(com.squarespace.template.Variables)

Example 3 with Context

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);
    }
}
Also used : Context(com.squarespace.template.Context) JsonNode(com.fasterxml.jackson.databind.JsonNode) CompiledTemplate(com.squarespace.template.CompiledTemplate)

Example 4 with Context

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);
}
Also used : Context(com.squarespace.template.Context) Test(org.testng.annotations.Test)

Example 5 with Context

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");
}
Also used : Context(com.squarespace.template.Context) CodeMaker(com.squarespace.template.CodeMaker) Test(org.testng.annotations.Test)

Aggregations

Context (com.squarespace.template.Context)73 Test (org.testng.annotations.Test)55 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)21 Instruction (com.squarespace.template.Instruction)14 Variables (com.squarespace.template.Variables)11 TextNode (com.fasterxml.jackson.databind.node.TextNode)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 Arguments (com.squarespace.template.Arguments)5 CodeMaker (com.squarespace.template.CodeMaker)4 Compiler (com.squarespace.template.Compiler)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 CompiledTemplate (com.squarespace.template.CompiledTemplate)2 CodeBuilder (com.squarespace.template.CodeBuilder)1 CodeExecuteException (com.squarespace.template.CodeExecuteException)1 CodeMachine (com.squarespace.template.CodeMachine)1 CodeSyntaxException (com.squarespace.template.CodeSyntaxException)1 ErrorInfo (com.squarespace.template.ErrorInfo)1 Locale (java.util.Locale)1