Added strictSections configuration.

Section behavior used to be somewhat hackily determined from nullValue and
defaultValue, but it was messy. Now it has its own configuration and things are
more explicit.

Previously missing/null sections were treated as false, but if you set
nullValue to something, then missing sections became an error, but null
sections remained false. If you set defaultValue to something, missing sections
went back to being omitted. Kooky.

Now missing sections are omitted, unless you set strictSections to true, in
which case they become an error. nullValue and defaultValue have no impact on
section behavior.

There is no way to make null sections an error, but I don't see any demand for
that, so I'm going to leave that as a TODO.

Closes #60.
This commit is contained in:
Michael Bayne
2014-12-20 11:03:03 -08:00
parent 09fee0f037
commit 8618f89f7e
4 changed files with 107 additions and 78 deletions
@@ -161,20 +161,47 @@ public class MustacheTest
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 testMissingNonStrictSection () {
test("", "{{#foo}}{{bar}}{{/foo}}", new Object() {
// no foo; section omitted due to non-strict-sections
});
test(Mustache.compiler().nullValue(""), "", "{{#foo}}{{bar}}{{/foo}}", new Object() {
// no foo; no exception because nullValue does change section strictness
});
test(Mustache.compiler().defaultValue(""), "", "{{#foo}}{{bar}}{{/foo}}", new Object() {
// no foo; no exception because defaultValue does change section strictness
});
}
@Test(expected=MustacheException.class)
public void testMissingStrictSection () {
test(Mustache.compiler().strictSections(true), "", "{{#foo}}{{bar}}{{/foo}}", new Object() {
// no foo; should throw exception due to strict-sections
});
}
@Test(expected=MustacheException.class)
public void testMissingStrictSectionNullValue () {
// missing strict-sections always throw regardless of nullValue()
test(Mustache.compiler().strictSections(true).nullValue(""), "", "{{#foo}}{{bar}}{{/foo}}",
new Object());
}
@Test(expected=MustacheException.class)
public void testMissingStrictSectionDefaultValue () {
// missing strict-sections always throw regardless of defaultValue()
test(Mustache.compiler().strictSections(true).defaultValue(""), "", "{{#foo}}{{bar}}{{/foo}}",
new Object());
}
@Test public void testSectionWithNonFalseyEmptyString () {
test(Mustache.compiler(), "test", "{{#foo}}test{{/foo}}", new Object() {
String foo = "";
@@ -223,25 +250,6 @@ public class MustacheTest
});
}
@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());
}
@@ -558,15 +566,15 @@ public class MustacheTest
}
@Test public void testStandardsModeWithNullValuesInLoop () {
String tmpl = "first line\n{{#nonexistent}}foo\n{{/nonexistent}}\nsecond line";
String result = Mustache.compiler().standardsMode(true).compile(tmpl).execute(new Object());
check("first line\nsecond line", result);
test("first line\nsecond line",
"first line\n{{#nullvalue}}foo\n{{/nullvalue}}\nsecond line",
context("nullvalue", null));
}
@Test public void testStandardsModeWithNullValuesInInverseLoop () {
String tmpl = "first line\n{{^nonexistent}}foo{{/nonexistent}} \nsecond line";
String result = Mustache.compiler().standardsMode(true).compile(tmpl).execute(new Object());
check("first line\nfoo \nsecond line", result);
test("first line\nfoo \nsecond line",
"first line\n{{^nullvalue}}foo{{/nullvalue}} \nsecond line",
context("nullvalue", null));
}
@Test public void testStandardsModeWithDotValue () {