Search in sources :

Example 1 with Arguments

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

Example 2 with Arguments

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

Example 3 with Arguments

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

Example 4 with Arguments

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

Example 5 with Arguments

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

Aggregations

Arguments (com.squarespace.template.Arguments)14 CodeMaker (com.squarespace.template.CodeMaker)10 Test (org.testng.annotations.Test)10 Context (com.squarespace.template.Context)5 Variables (com.squarespace.template.Variables)3 ArgumentsException (com.squarespace.template.ArgumentsException)1 CodeBuilder (com.squarespace.template.CodeBuilder)1 Instruction (com.squarespace.template.Instruction)1 StringView (com.squarespace.template.StringView)1 Locale (java.util.Locale)1