Search in sources :

Example 41 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class ContentPredicatesTest method testSameDay.

@Test
public void testSameDay() throws CodeException {
    long date1 = KnownDates.NOV_15_2013_123030_UTC - (3600L * 1000);
    String json = getDateTestJson(date1, "America/New_York");
    ObjectNode node = (ObjectNode) JsonUtils.decode(json);
    ObjectNode dates = JsonUtils.createObjectNode();
    dates.put("startDate", date1);
    dates.put("endDate", KnownDates.NOV_15_2013_123030_UTC);
    node.set("dates", dates);
    String[] key = new String[] { "dates" };
    Context ctx = new Context(node);
    ctx.pushSection(key);
    assertTrue(ContentPredicates.SAME_DAY, ctx);
    // 2 days prior, shouldn't match
    dates.put("endDate", KnownDates.NOV_15_2013_123030_UTC - (86400L * 1000 * 2));
    ctx = new Context(node);
    ctx.pushSection(key);
    assertFalse(ContentPredicates.SAME_DAY, ctx);
}
Also used : Context(com.squarespace.template.Context) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Test(org.testng.annotations.Test)

Example 42 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class LegacyMoneyFormatFactoryTest method cldr.

private String cldr(String tag, String currency, String number, boolean narrow) throws CodeException {
    Arguments args = mk.args(narrow ? " symbol:narrow" : " ");
    CLDR_MONEY.validateArgs(args);
    Locale locale = Locale.forLanguageTag(tag);
    Context ctx = new Context(moneyJson(number, currency), new StringBuilder(), locale);
    Variables variables = new Variables("@", ctx.node());
    CLDR_MONEY.apply(ctx, args, variables);
    return variables.first().node().asText();
}
Also used : Locale(java.util.Locale) Context(com.squarespace.template.Context) Variables(com.squarespace.template.Variables) Arguments(com.squarespace.template.Arguments)

Example 43 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class MoneyFormatterLegacyTest method format.

private static String format(String locale, Arguments args, String json) throws CodeException {
    Context ctx = new Context(JsonUtils.decode(json));
    ctx.javaLocale(Locale.forLanguageTag(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 44 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class MoneyFormatterTest method format.

private static String format(String locale, Arguments args, String json) throws CodeException {
    Context ctx = new Context(JsonUtils.decode(json));
    ctx.javaLocale(Locale.forLanguageTag(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 45 with Context

use of com.squarespace.template.Context in project template-compiler by Squarespace.

the class MoneyFormatterTest method executeLocale.

private String executeLocale(String template, String json, String locale) throws CodeException {
    Compiler compiler = compiler();
    Context ctx = compiler.newExecutor().json(json).template(template).locale(Locale.forLanguageTag(locale)).execute();
    return ctx.buffer().toString();
}
Also used : Context(com.squarespace.template.Context) Compiler(com.squarespace.template.Compiler)

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