Some tidying up, name changes and clarifications. I went with "defaultValue"

rather than "missingVariableValue" because the value is used any time a
variable resolves to null, not necessarily just when it is missing. If you
have a context like:

new Object() {
   String foo = null;
}

and reference {{foo}} in your template, foo is not missing, per se, but the
default value will still be used for it.
This commit is contained in:
Michael Bayne
2011-04-15 12:35:33 -07:00
parent 5d59eada29
commit 15ff48eabb
3 changed files with 29 additions and 30 deletions
@@ -153,12 +153,12 @@ public class MustacheTest
execute(context("a", "<b>")));
}
@Test(expected = MustacheParseException.class)
@Test(expected=MustacheParseException.class)
public void testDanglingTag () {
Mustache.compiler().escapeHTML(true).compile("{{a").execute(context("a", "<b>"));
}
@Test(expected = MustacheParseException.class)
@Test(expected=MustacheParseException.class)
public void testInvalidUnescapeHTML () {
Mustache.compiler().escapeHTML(true).compile("{{{a}}").execute(context("a", "<b>"));
}
@@ -276,7 +276,7 @@ public class MustacheTest
assertEquals(":bar:", result);
}
@Test(expected = MustacheException.class)
@Test(expected=MustacheException.class)
public void testStandardsModeWithNoParentContextSearching () {
String tmpl = "{{#parent}}foo{{parentProperty}}bar{{/parent}}";
String result = Mustache.compiler().standardsMode(true).compile(tmpl).
@@ -284,25 +284,19 @@ public class MustacheTest
"parentProperty", "bar"));
}
@Test(expected = MustacheException.class)
@Test(expected=MustacheException.class)
public void testMissingValue () {
String tmpl = "{{ missing }} {{ notmissing }}";
Mustache.compiler().compile(tmpl).
execute(Collections.singletonMap("notmissing", "bar"));
test("n/a", "{{missing}} {{notmissing}}", context("notmissing", "bar"));
}
@Test public void testMissingValueWithDefault () {
String tmpl = "{{ missing }}{{ notmissing }}";
String result = Mustache.compiler().missingVariableValue("").compile(tmpl).
execute(Collections.singletonMap("notmissing", "bar"));
assertEquals("bar", result);
test(Mustache.compiler().defaultValue(""),
"bar", "{{missing}}{{notmissing}}", context("notmissing", "bar"));
}
@Test public void testMissingValueWithDefaultNonEmptyString () {
String tmpl = "{{ missing }}{{ notmissing }}";
String result = Mustache.compiler().missingVariableValue("foo").compile(tmpl).
execute(Collections.singletonMap("notmissing", "bar"));
assertEquals("foobar", result);
test(Mustache.compiler().defaultValue("foo"),
"foobar", "{{missing}}{{notmissing}}", context("notmissing", "bar"));
}
protected void test (Mustache.Compiler compiler, String expected, String template, Object ctx)