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. */
public Compiler escapeHTML (boolean escapeHTML) {
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
@@ -88,7 +89,8 @@ public class Mustache
* context. */
public Compiler standardsMode (boolean standardsMode) {
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
@@ -96,7 +98,8 @@ public class Mustache
* supplied default for missing keys and existing keys that return null values. */
public Compiler defaultValue (String defaultValue) {
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
@@ -112,35 +115,36 @@ public class Mustache
* </ul> */
public Compiler nullValue (String nullValue) {
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. */
public Compiler emptyStringIsFalse (boolean emptyStringIsFalse) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, emptyStringIsFalse, this.zeroIsFalse, this.loader, this.collector,
this.delims);
this.missingIsNull, emptyStringIsFalse, this.zeroIsFalse,
this.loader, this.collector, this.delims);
}
/** Returns a compiler that will treat zero as a false value if parameter is true. */
public Compiler zeroIsFalse (boolean zeroIsFalse) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, zeroIsFalse, this.loader, this.collector,
this.delims);
this.missingIsNull, this.emptyStringIsFalse, zeroIsFalse,
this.loader, this.collector, this.delims);
}
/** Returns a compiler configured to use the supplied template loader to handle partials. */
public Compiler withLoader (TemplateLoader loader) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse, loader, this.collector,
this.delims);
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse,
loader, this.collector, this.delims);
}
/** Returns a compiler configured to use the supplied collector. */
public Compiler withCollector (Collector collector) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse, this.loader, collector,
this.delims);
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse,
this.loader, collector, this.delims);
}
/** 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. */
public Compiler withDelims (String delims) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse, this.loader,
this.collector, new Delims().updateDelims(delims));
this.missingIsNull, this.emptyStringIsFalse, this.zeroIsFalse,
this.loader, this.collector, new Delims().updateDelims(delims));
}
/** 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,
boolean missingIsNull, boolean emptyStringIsFalse, boolean zeroIsFalse, TemplateLoader loader,
Collector collector, Delims delims) {
boolean missingIsNull, boolean emptyStringIsFalse, boolean zeroIsFalse,
TemplateLoader loader, Collector collector, Delims delims) {
this.escapeHTML = escapeHTML;
this.standardsMode = standardsMode;
this.nullValue = nullValue;
@@ -222,8 +226,8 @@ public class Mustache
* Returns a compiler that escapes HTML by default and does not use standards mode.
*/
public static Compiler compiler () {
return new Compiler(true, false, null, true, false, false, FAILING_LOADER, new DefaultCollector(),
new Delims());
return new Compiler(true, false, null, true, false, false, FAILING_LOADER,
new DefaultCollector(), new Delims());
}
/**
@@ -710,7 +714,8 @@ public class Mustache
}
} else if (_compiler.emptyStringIsFalse && "".equals(value)) {
// 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
} else {
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() {
String foo = "";
});
}
@Test public void testSectionWithLongValueZero () {
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() {
@Test public void testSectionWithNonFalseyZero () {
test(Mustache.compiler(), "test", "{{#foo}}test{{/foo}}", new Object() {
Long foo = 0L;
});
}
@Test public void testSectionWithIntegerValueZero () {
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() {
Integer foo = 0;
});
}
@Test public void testSectionWithFloatValueZero () {
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() {
Float foo = 0F;
});
}
@Test public void testSectionWithDoubleValueZero () {
test(Mustache.compiler().zeroIsFalse(true), "", "{{#foo}}test{{/foo}}", new Object() {
Double foo = 0D;
});
@Test public void testSectionWithFalseyZero () {
test(Mustache.compiler().zeroIsFalse(true), "",
"{{#intv}}intv{{/intv}}" +
"{{#longv}}longv{{/longv}}" +
"{{#floatv}}floatv{{/floatv}}" +
"{{#doublev}}doublev{{/doublev}}" +
"{{#longm}}longm{{/longm}}" +
"{{#intm}}intm{{/intm}}" +
"{{#floatm}}floatm{{/floatm}}" +
"{{#doublem}}doublem{{/doublem}}",
new Object() {
Integer intv = 0;
Long longv = 0L;
Float floatv = 0f;
Double doublev = 0d;
int intm () { return 0; }
long longm () { return 0l; }
float floatm () { return 0f; }
double doublem () { return 0d; }
});
}
@Test public void testMissingSection () {