use of com.squarespace.template.Context 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");
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class CorePredicatesTest method testDebug.
@Test
public void testDebug() throws CodeException {
String[] key = new String[] { "a", "b" };
Context ctx = context("{\"debug\": true, \"a\": {\"b\": 1}}");
ctx.pushSection(key);
assertTrue(CorePredicates.DEBUG, ctx);
for (String json : new String[] { "{\"a\": {\"b\": 1}}", "{\"debug\": 0, \"a\": {\"b\": 1}}" }) {
ctx = context(json);
ctx.pushSection(key);
assertFalse(CorePredicates.DEBUG, ctx);
}
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class CorePredicatesTest method testOdd.
@Test
public void testOdd() throws CodeException {
assertTrue(ODD, context("3"));
assertFalse(ODD, context("4"));
// Verify behavior inside repeated section
String template = "{.repeated section items}{.odd?}{@index}{.end}{.end}";
JsonNode json = json("{\"items\": [{}, {}, {}, {}, {}]}");
Context ctx = execute(template, json);
assertEquals(ctx.buffer().toString(), "");
template = "{.repeated section items}{.odd? @index}{@index}{.end}{.end}";
ctx = execute(template, json);
assertEquals(ctx.buffer().toString(), "135");
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class CorePredicatesTest method testEven.
@Test
public void testEven() throws CodeException {
assertTrue(EVEN, context("4"));
assertTrue(EVEN, context("-4"));
assertFalse(EVEN, context("5"));
assertFalse(EVEN, context("-5"));
assertFalse(EVEN, context("false"));
assertFalse(EVEN, context("\"hi\""));
assertFalse(EVEN, context("\"4\""));
assertFalse(EVEN, context("{\"foo\": \"bar\"}"));
// Verify behavior inside repeated section
String template = "{.repeated section items}{.even?}{@index}{.end}{.end}";
JsonNode json = json("{\"items\": [{}, {}, {}, {}, {}]}");
Context ctx = execute(template, json);
assertEquals(ctx.buffer().toString(), "");
template = "{.repeated section items}{.even? @index}{@index}{.end}{.end}";
ctx = execute(template, json);
assertEquals(ctx.buffer().toString(), "24");
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class ContentFormattersTest method testCapitalize.
@Test
public void testCapitalize() throws CodeException {
String template = "{user.name|capitalize}";
String json = "{\"user\": {\"name\": \"Bob Smith\"}}";
Instruction code = compiler().compile(template).code();
Context ctx = compiler().newExecutor().code(code).json(json).execute();
String result = eval(ctx);
assertEquals(result, "BOB SMITH");
}
Aggregations