From fe2664627f6293fbb2041985d5fcab6c0e51f0e9 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 27 Mar 2013 10:29:57 -0700 Subject: [PATCH] A few tweaks to default delims patch (and some tidying): - triple-stache mode reverted to only work with {{{ }}} rather than working with whatever default delims you set; I think this is more in the spirit of the Mustache spec - Compiler.withDelims takes a delims string rather than the internal Delims class which callers don't care about or have access to - nixed Delims constructors, made updateDelims fluent - nixed DelimsTest, it wasn't testing anything that wasn't also already tested by the functional tests in MustacheTest --- .../com/samskivert/mustache/Mustache.java | 64 +++++++++---------- .../com/samskivert/mustache/DelimsTest.java | 38 ----------- .../com/samskivert/mustache/MustacheTest.java | 18 ++---- 3 files changed, 36 insertions(+), 84 deletions(-) delete mode 100644 src/test/java/com/samskivert/mustache/DelimsTest.java diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index bdca141..4365f41 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -60,7 +60,7 @@ public class Mustache /** The collector used by templates compiled with this compiler. */ public final Collector collector; - /** The Delimiters used by default int he templates compiled with this compiler. */ + /** The delimiters used by default in templates compiled with this compiler. */ public final Delims delims; /** Compiles the supplied template into a repeatedly executable intermediate form. */ @@ -114,25 +114,31 @@ public class Mustache /** 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.loader, this.collector, this.delims); + this.missingIsNull, emptyStringIsFalse, 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, loader, this.collector, this.delims); + this.missingIsNull, this.emptyStringIsFalse, 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.loader, collector, this.delims); + this.missingIsNull, this.emptyStringIsFalse, this.loader, collector, + this.delims); } - /** Returns a compiler configured to use the supplied delims as default delimiters. */ - public Compiler withDelims (Delims delims) { + /** Returns a compiler configured to use the supplied delims as default delimiters. + * @param delims a string of the form {@code {AB CD} or {@code {A D} where A and B are + * 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.loader, this.collector, delims); + this.missingIsNull, this.emptyStringIsFalse, this.loader, + this.collector, new Delims().updateDelims(delims)); } /** Returns the value to use in the template for the null-valued property {@code name}. See @@ -205,7 +211,8 @@ public class Mustache */ public static Compiler compiler () { - return new Compiler(true, false, null, true, false, FAILING_LOADER, new DefaultCollector(), new Delims()); + return new Compiler(true, false, null, true, false, FAILING_LOADER, new DefaultCollector(), + new Delims()); } /** @@ -372,9 +379,9 @@ public class Mustache delims.updateDelims(text.substring(1, text.length()-1)); text.setLength(0); } else { - // if we haven't remapped the delimiters, and the tag starts with {{{ then + // if the delimiters are {{ and }}, and the tag starts with {{{ then // require that it end with }}} and disable HTML escaping - if (delims.isDefault() && text.charAt(0) == delims.start1) { + if (delims.isStaches() && text.charAt(0) == delims.start1) { try { // we've only parsed }} at this point, so we have to slurp in // another character from the input stream and check it @@ -407,36 +414,17 @@ public class Mustache protected static class Delims { public char start1 = '{'; - private final char defaultStart1; public char start2 = '{'; - private final char defaultStart2; public char end1 = '}'; - private final char defaultEnd1; public char end2 = '}'; - private final char defaultEnd2; - public Delims () { - this("{{ }}"); + public boolean isStaches () { + return start1 == '{' && start2 == '{' && end1 == '}' && end2 == '}'; } - public Delims (String s) { - updateDelims(s); - defaultStart1 = start1; - defaultStart2 = start2; - defaultEnd1 = end1; - defaultEnd2 = end2; - } - - public boolean isDefault () { - return start1 == defaultStart1 && start2 == defaultStart2 && end1 == defaultEnd1 && end2 == defaultEnd2; - } - - public void updateDelims (String dtext) { - String errmsg = "Invalid delimiter configuration '" + dtext + "'. Must be of the " + - "form {{=1 2=}} or {{=12 34=}} where 1, 2, 3 and 4 are delimiter chars."; - + public Delims updateDelims (String dtext) { String[] delims = dtext.split(" "); - if (delims.length != 2) throw new MustacheException(errmsg); + if (delims.length != 2) throw new MustacheException(errmsg(dtext)); switch (delims[0].length()) { case 1: @@ -448,7 +436,7 @@ public class Mustache start2 = delims[0].charAt(1); break; default: - throw new MustacheException(errmsg); + throw new MustacheException(errmsg(dtext)); } switch (delims[1].length()) { @@ -461,8 +449,14 @@ public class Mustache end2 = delims[1].charAt(1); break; default: - throw new MustacheException(errmsg); + throw new MustacheException(errmsg(dtext)); } + return this; + } + + private static String errmsg (String dtext) { + return "Invalid delimiter configuration '" + dtext + "'. Must be of the " + + "form {{=1 2=}} or {{=12 34=}} where 1, 2, 3 and 4 are delimiter chars."; } } diff --git a/src/test/java/com/samskivert/mustache/DelimsTest.java b/src/test/java/com/samskivert/mustache/DelimsTest.java deleted file mode 100644 index 0214b3c..0000000 --- a/src/test/java/com/samskivert/mustache/DelimsTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.samskivert.mustache; - -import org.junit.Test; - -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; - -public class DelimsTest { - - @Test public void shouldConstructDefaultDelims () { - test(new Mustache.Delims(), '{', '{', '}', '}', true); - } - - @Test public void shouldUpdateDefaultDelims () { - Mustache.Delims delims = new Mustache.Delims(); - delims.updateDelims("<% %>"); - test(delims, '<', '%', '%', '>', false); - } - - @Test public void shouldConstructCustomDelims () { - test(new Mustache.Delims("<% %>"), '<', '%', '%', '>', true); - } - - @Test public void shouldUpdateCustomConstructedDelims () { - Mustache.Delims delims = new Mustache.Delims("<% %>"); - delims.updateDelims("{{ }}"); - test(delims, '{', '{', '}', '}', false); - } - - protected void test (Mustache.Delims delims, char start1, char start2, char end1, char end2, boolean isDefault) - { - assertThat(delims.start1, equalTo(start1)); - assertThat(delims.start2, equalTo(start2)); - assertThat(delims.end1, equalTo(end1)); - assertThat(delims.end2, equalTo(end2)); - assertThat(delims.isDefault(), equalTo(isDefault)); - } -} diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index 68d2b46..931b548 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -4,10 +4,6 @@ package com.samskivert.mustache; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -18,6 +14,9 @@ import java.util.HashMap; import java.util.Map; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Various unit tests. @@ -517,23 +516,20 @@ public class MustacheTest } @Test public void testNonStandardDefaultDelims () { - test(Mustache.compiler().withDelims(new Mustache.Delims("<% %>")), "bar", "<%foo%>", new Object() { + test(Mustache.compiler().withDelims("<% %>"), "bar", "<%foo%>", new Object() { String foo = "bar"; }); } - protected void test (Mustache.Compiler compiler, String expected, String template, Object ctx) - { + protected void test (Mustache.Compiler compiler, String expected, String template, Object ctx) { assertEquals(expected, compiler.compile(template).execute(ctx)); } - protected void test (String expected, String template, Object ctx) - { + protected void test (String expected, String template, Object ctx) { test(Mustache.compiler(), expected, template, ctx); } - protected Object context (Object... data) - { + protected Object context (Object... data) { Map ctx = new HashMap(); for (int ii = 0; ii < data.length; ii += 2) { ctx.put(data[ii].toString(), data[ii+1]);