Added Compiler.nullValue() for situations where one wants to provide a value

for use when variables resolve to null, but still wishes for exceptions to be
thrown when values are not resolvable (i.e. are typos). Pre-existing behavior
is preserved if one uses defaultValue() or does not change the default
configuration.

Nota bene: there are complexities, so be sure to read the documentation.
This commit is contained in:
Michael Bayne
2011-05-28 10:42:45 -07:00
parent ef971015bd
commit 9fae13e008
4 changed files with 214 additions and 35 deletions
@@ -113,6 +113,43 @@ public class MustacheTest
"d", new Object[] { context("e", "3"), context("e", "4") })));
}
@Test public void testNullSection () {
test("", "{{#foo}}{{bar}}{{/foo}}", new Object() {
Object foo = null;
});
}
@Test public void testNullSectionWithDefaultValue () {
test(Mustache.compiler().defaultValue(""), "", "{{#foo}}{{bar}}{{/foo}}", new Object() {
Object foo = null;
});
}
@Test public void testNullSectionWithNullValue () {
test(Mustache.compiler().nullValue(""), "", "{{#foo}}{{bar}}{{/foo}}", new Object() {
Object foo = null;
});
}
@Test public void testMissingSection () {
test("", "{{#foo}}{{bar}}{{/foo}}", new Object() {
// no foo
});
}
@Test public void testMissingSectionWithDefaultValue () {
test(Mustache.compiler().defaultValue(""), "", "{{#foo}}{{bar}}{{/foo}}", new Object() {
// no foo
});
}
@Test(expected=MustacheException.class)
public void testMissingSectionWithNullValue () {
test(Mustache.compiler().nullValue(""), "", "{{#foo}}{{bar}}{{/foo}}", new Object() {
// no foo
});
}
@Test public void testComment () {
test("foobar", "foo{{! nothing to see here}}bar", new Object());
}
@@ -215,6 +252,28 @@ public class MustacheTest
});
}
@Test(expected=NullPointerException.class)
public void testNullComponentInCompoundVariable () {
// make sure that even if we have a null value configured, we freak out
test(Mustache.compiler().nullValue("foo"), "unused", "{{foo.bar.baz}}", new Object() {
Object foo () {
return new Object() {
Object bar = null;
};
}
});
}
@Test(expected=NullPointerException.class)
public void testMissingComponentInCompoundVariable () {
// make sure that even if we have a default value configured, we freak out
test(Mustache.compiler().defaultValue("foo"), "unused", "{{foo.bar.baz}}", new Object() {
Object foo () {
return new Object(); // no bar
}
});
}
@Test public void testNewlineSkipping () {
String tmpl = "list:\n" +
"{{#items}}\n" +
@@ -322,6 +381,23 @@ public class MustacheTest
});
}
@Test(expected=MustacheException.class)
public void testMissingValueWithNullDefault () {
test(Mustache.compiler().nullValue(""),
"bar", "{{missing}}{{notmissing}}", new Object() {
String notmissing = "bar";
// no field or method for 'missing'
});
}
@Test public void testNullValueGetsNullDefault () {
test(Mustache.compiler().nullValue("foo"),
"foobar", "{{nullvar}}{{nonnullvar}}", new Object() {
String nonnullvar = "bar";
String nullvar = null;
});
}
protected void test (Mustache.Compiler compiler, String expected, String template, Object ctx)
{
assertEquals(expected, compiler.compile(template).execute(ctx));