Search in sources :

Example 6 with CodeMaker

use of com.squarespace.template.CodeMaker 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 7 with CodeMaker

use of com.squarespace.template.CodeMaker 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)

Example 8 with CodeMaker

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

the class CorePredicatesTest method testLessThanOrEqual.

@Test
public void testLessThanOrEqual() throws CodeException {
    CodeMaker mk = maker();
    assertTrue(LESS_THAN_OR_EQUAL, context("1"), mk.args(" 1"));
    assertTrue(LESS_THAN_OR_EQUAL, context("1"), mk.args(" 2"));
    assertTrue(LESS_THAN_OR_EQUAL, context("3.1415"), mk.args(" 3.2"));
    assertTrue(LESS_THAN_OR_EQUAL, context("3.1415"), mk.args(" 3.1415"));
    assertTrue(LESS_THAN_OR_EQUAL, context("\"c\""), mk.args(" \"c\""));
    assertTrue(LESS_THAN_OR_EQUAL, context("[]"), mk.args(" []"));
    assertTrue(LESS_THAN_OR_EQUAL, context("{}"), mk.args(" 1 2"));
    assertTrue(LESS_THAN_OR_EQUAL, context("{}"), mk.args(" 2 2"));
    assertFalse(LESS_THAN_OR_EQUAL, context("-10"), mk.args(" -20"));
    assertFalse(LESS_THAN_OR_EQUAL, context("-10"), mk.args(" -20"));
    assertFalse(LESS_THAN_OR_EQUAL, context("3.1415"), mk.args(" 3.1"));
    assertFalse(LESS_THAN_OR_EQUAL, context("\"z\""), mk.args(" \"j\""));
    assertFalse(LESS_THAN_OR_EQUAL, context("{}"), mk.args(" 3 2"));
}
Also used : CodeMaker(com.squarespace.template.CodeMaker) Test(org.testng.annotations.Test)

Example 9 with CodeMaker

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

the class CorePredicatesTest method testEqual.

@Test
public void testEqual() throws CodeException {
    CodeMaker mk = maker();
    assertTrue(EQUAL, context("3"), mk.args(" 3"));
    assertTrue(EQUAL, context("\"hello\""), mk.args(" \"hello\""));
    assertTrue(EQUAL, context("[1, 2, 3]"), mk.args(":[1, 2, 3]"));
    assertTrue(EQUAL, context("{\"foo\": \"bar\"}"), mk.args(" {\"foo\":\"bar\"}"));
    assertFalse(EQUAL, context("-1"), mk.args(" 3"));
    assertFalse(EQUAL, context("\"hello\""), mk.args(" \"goodbye\""));
    assertFalse(EQUAL, context("[1, 2, 3]"), mk.args(":1"));
    assertFalse(EQUAL, context("{\"foo\":\"bar\"}"), mk.args(":1"));
    // Compare 2 variables
    String template = "{.equal? a b}yes{.or}no{.end}";
    Context ctx = execute(template, "{\"a\": 1, \"b\": 1}");
    assertEquals(ctx.buffer().toString(), "yes");
    ctx = execute(template, "{\"a\": 1, \"b\": 2}");
    assertEquals(ctx.buffer().toString(), "no");
    // Compare variable with JSON
    template = "{.equal? 1 b}yes{.or}no{.end}";
    ctx = execute(template, "{\"b\": 1}");
    assertEquals(ctx.buffer().toString(), "yes");
    ctx = execute(template, "{\"b\": 2}");
    assertEquals(ctx.buffer().toString(), "no");
}
Also used : Context(com.squarespace.template.Context) CodeMaker(com.squarespace.template.CodeMaker) Test(org.testng.annotations.Test)

Example 10 with CodeMaker

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

the class CorePredicatesTest method testNth.

@Test
public void testNth() throws CodeException {
    CodeMaker mk = maker();
    assertTrue(NTH, context("3"), mk.args(" 3"));
    assertFalse(NTH, context("2"), mk.args(" 3"));
    assertFalse(NTH, context("3"), mk.args(" 2"));
    assertFalse(NTH, context("[]"), mk.args(" 2"));
    assertFalse(NTH, context("\"a\""), mk.args(" 2"));
    assertFalse(NTH, context("2"), mk.args(" \"a\""));
    String template = "{.nth? num 3}A{.or}B{.end}";
    assertEquals(execute(template, "{\"num\": 6}").buffer().toString(), "A");
    assertEquals(execute(template, "{\"num\": 5}").buffer().toString(), "B");
    assertEquals(execute(template, "{}").buffer().toString(), "B");
    template = "{.repeated section @}{.nth? @index 3}A{.end}{.end}";
    assertEquals(execute(template, "[0,0,0]").buffer().toString(), "A");
    assertEquals(execute(template, "[0,0,0,0,0,0]").buffer().toString(), "AA");
}
Also used : CodeMaker(com.squarespace.template.CodeMaker) Test(org.testng.annotations.Test)

Aggregations

CodeMaker (com.squarespace.template.CodeMaker)19 Test (org.testng.annotations.Test)19 Arguments (com.squarespace.template.Arguments)10 Context (com.squarespace.template.Context)4 CodeBuilder (com.squarespace.template.CodeBuilder)1 Instruction (com.squarespace.template.Instruction)1