Some tidying, added a negative test, method tests.

The latter was because the new feature was not actually conditional on its
compiler parameter. It was on all the time.
This commit is contained in:
Michael Bayne
2013-05-13 13:22:45 -07:00
parent 4300b1213e
commit 6e3051c3ba
2 changed files with 53 additions and 38 deletions
@@ -80,7 +80,8 @@ public class Mustache
/** Returns a compiler that either does or does not escape HTML by default. */ /** Returns a compiler that either does or does not escape HTML by default. */
public Compiler escapeHTML (boolean escapeHTML) { public Compiler escapeHTML (boolean escapeHTML) {
return new Compiler(escapeHTML, this.standardsMode, this.nullValue, this.missingIsNull, return new Compiler(escapeHTML, this.standardsMode, this.nullValue, this.missingIsNull,
this.emptyStringIsFalse, this.zeroIsFalse, this.loader, this.collector, this.delims); this.emptyStringIsFalse, this.zeroIsFalse, this.loader,
this.collector, this.delims);
} }
/** Returns a compiler that either does or does not use standards mode. Standards mode /** Returns a compiler that either does or does not use standards mode. Standards mode
@@ -88,7 +89,8 @@ public class Mustache
* context. */ * context. */
public Compiler standardsMode (boolean standardsMode) { public Compiler standardsMode (boolean standardsMode) {
return new Compiler(this.escapeHTML, standardsMode, this.nullValue, this.missingIsNull, return new Compiler(this.escapeHTML, standardsMode, this.nullValue, this.missingIsNull,
this.emptyStringIsFalse, this.zeroIsFalse, this.loader, this.collector, this.delims); this.emptyStringIsFalse, this.zeroIsFalse, this.loader,
this.collector, this.delims);
} }
/** Returns a compiler that will use the given value for any variable that is missing, or /** Returns a compiler that will use the given value for any variable that is missing, or
@@ -96,7 +98,8 @@ public class Mustache
* supplied default for missing keys and existing keys that return null values. */ * supplied default for missing keys and existing keys that return null values. */
public Compiler defaultValue (String defaultValue) { public Compiler defaultValue (String defaultValue) {
return new Compiler(this.escapeHTML, this.standardsMode, defaultValue, true, return new Compiler(this.escapeHTML, this.standardsMode, defaultValue, true,
this.emptyStringIsFalse, this.zeroIsFalse, this.loader, this.collector, this.delims); this.emptyStringIsFalse, this.zeroIsFalse, this.loader,
this.collector, this.delims);
} }
/** Returns a compiler that will use the given value for any variable that resolves to /** Returns a compiler that will use the given value for any variable that resolves to
@@ -112,35 +115,36 @@ public class Mustache
* </ul> */ * </ul> */
public Compiler nullValue (String nullValue) { public Compiler nullValue (String nullValue) {
return new Compiler(this.escapeHTML, this.standardsMode, nullValue, false, return new Compiler(this.escapeHTML, this.standardsMode, nullValue, false,
this.emptyStringIsFalse, this.zeroIsFalse, this.loader, this.collector, this.delims); this.emptyStringIsFalse, this.zeroIsFalse, this.loader,
this.collector, this.delims);
} }
/** Returns a compiler that will treat empty string as a false value if parameter is true. */ /** Returns a compiler that will treat empty string as a false value if parameter is true. */
public Compiler emptyStringIsFalse (boolean emptyStringIsFalse) { public Compiler emptyStringIsFalse (boolean emptyStringIsFalse) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue, return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, emptyStringIsFalse, this.zeroIsFalse, this.loader, this.collector, this.missingIsNull, emptyStringIsFalse, this.zeroIsFalse,
this.delims); this.loader, this.collector, this.delims);
} }
/** Returns a compiler that will treat zero as a false value if parameter is true. */ /** Returns a compiler that will treat zero as a false value if parameter is true. */
public Compiler zeroIsFalse (boolean zeroIsFalse) { public Compiler zeroIsFalse (boolean zeroIsFalse) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue, return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, zeroIsFalse, this.loader, this.collector, this.missingIsNull, this.emptyStringIsFalse, zeroIsFalse,
this.delims); this.loader, this.collector, this.delims);
} }
/** Returns a compiler configured to use the supplied template loader to handle partials. */ /** Returns a compiler configured to use the supplied template loader to handle partials. */
public Compiler withLoader (TemplateLoader loader) { public Compiler withLoader (TemplateLoader loader) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue, return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse, loader, this.collector, this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse,
this.delims); loader, this.collector, this.delims);
} }
/** Returns a compiler configured to use the supplied collector. */ /** Returns a compiler configured to use the supplied collector. */
public Compiler withCollector (Collector collector) { public Compiler withCollector (Collector collector) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue, return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse, this.loader, collector, this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse,
this.delims); this.loader, collector, this.delims);
} }
/** Returns a compiler configured to use the supplied delims as default delimiters. /** Returns a compiler configured to use the supplied delims as default delimiters.
@@ -148,8 +152,8 @@ public class Mustache
* opening delims and C and D are closing delims. */ * opening delims and C and D are closing delims. */
public Compiler withDelims (String delims) { public Compiler withDelims (String delims) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue, return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse, this.loader, this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse,
this.collector, new Delims().updateDelims(delims)); this.loader, this.collector, new Delims().updateDelims(delims));
} }
/** Returns the value to use in the template for the null-valued property {@code name}. See /** Returns the value to use in the template for the null-valued property {@code name}. See
@@ -159,8 +163,8 @@ public class Mustache
} }
protected Compiler (boolean escapeHTML, boolean standardsMode, String nullValue, protected Compiler (boolean escapeHTML, boolean standardsMode, String nullValue,
boolean missingIsNull, boolean emptyStringIsFalse, boolean zeroIsFalse, TemplateLoader loader, boolean missingIsNull, boolean emptyStringIsFalse, boolean zeroIsFalse,
Collector collector, Delims delims) { TemplateLoader loader, Collector collector, Delims delims) {
this.escapeHTML = escapeHTML; this.escapeHTML = escapeHTML;
this.standardsMode = standardsMode; this.standardsMode = standardsMode;
this.nullValue = nullValue; this.nullValue = nullValue;
@@ -222,8 +226,8 @@ public class Mustache
* Returns a compiler that escapes HTML by default and does not use standards mode. * Returns a compiler that escapes HTML by default and does not use standards mode.
*/ */
public static Compiler compiler () { public static Compiler compiler () {
return new Compiler(true, false, null, true, false, false, FAILING_LOADER, new DefaultCollector(), return new Compiler(true, false, null, true, false, false, FAILING_LOADER,
new Delims()); new DefaultCollector(), new Delims());
} }
/** /**
@@ -710,7 +714,8 @@ public class Mustache
} }
} else if (_compiler.emptyStringIsFalse && "".equals(value)) { } else if (_compiler.emptyStringIsFalse && "".equals(value)) {
// omit the section // omit the section
} else if (value != null && value instanceof Number && ((Number) value).longValue() == 0) { } else if (_compiler.zeroIsFalse && (value instanceof Number) &&
((Number)value).longValue() == 0) {
// omit the section // omit the section
} else { } else {
executeSegs(tmpl, ctx.nest(value, 0, false, false), out); executeSegs(tmpl, ctx.nest(value, 0, false, false), out);
@@ -139,34 +139,44 @@ public class MustacheTest
}); });
} }
@Test public void testSectionWithEmptyString () { @Test public void testSectionWithNonFalseyEmptyString () {
test(Mustache.compiler(), "test", "{{#foo}}test{{/foo}}", new Object() {
String foo = "";
});
}
@Test public void testSectionWithFalseyEmptyString () {
test(Mustache.compiler().emptyStringIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() { test(Mustache.compiler().emptyStringIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() {
String foo = ""; String foo = "";
}); });
} }
@Test public void testSectionWithLongValueZero () { @Test public void testSectionWithNonFalseyZero () {
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() { test(Mustache.compiler(), "test", "{{#foo}}test{{/foo}}", new Object() {
Long foo = 0L; Long foo = 0L;
}); });
} }
@Test public void testSectionWithIntegerValueZero () { @Test public void testSectionWithFalseyZero () {
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() { test(Mustache.compiler().zeroIsFalse(true), "",
Integer foo = 0; "{{#intv}}intv{{/intv}}" +
}); "{{#longv}}longv{{/longv}}" +
} "{{#floatv}}floatv{{/floatv}}" +
"{{#doublev}}doublev{{/doublev}}" +
@Test public void testSectionWithFloatValueZero () { "{{#longm}}longm{{/longm}}" +
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() { "{{#intm}}intm{{/intm}}" +
Float foo = 0F; "{{#floatm}}floatm{{/floatm}}" +
}); "{{#doublem}}doublem{{/doublem}}",
} new Object() {
Integer intv = 0;
@Test public void testSectionWithDoubleValueZero () { Long longv = 0L;
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() { Float floatv = 0f;
Double foo = 0D; Double doublev = 0d;
}); int intm () { return 0; }
long longm () { return 0l; }
float floatm () { return 0f; }
double doublem () { return 0d; }
});
} }
@Test public void testMissingSection () { @Test public void testMissingSection () {