From 8998b5f11e9f2a2710080a80f817eec9d2b60767 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 12 Mar 2014 11:31:16 -0700 Subject: [PATCH] May as well report the bogus final char if we can. --- .../com/samskivert/mustache/Mustache.java | 19 ++++++++++--------- .../com/samskivert/mustache/MustacheTest.java | 6 ++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index e9f527d..022ddb5 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -451,17 +451,18 @@ public class Mustache // if the delimiters are {{ and }}, and the tag starts with {{{ then // require that it end with }}} and disable escaping 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 - int end3 = (char)source.read(); - if (end3 != '}') { - throw new MustacheParseException( - "Invalid triple-mustache tag: {{" + text + "}}", line); - } - } catch (IOException e) { + // we've only parsed }} at this point, so we have to slurp in another + // character from the input stream and check it + int end3; + try { end3 = source.read(); } + catch (IOException e) { throw new MustacheException(e); } + if (end3 != '}') { + String got = (end3 == -1) ? "" : String.valueOf((char)end3); + throw new MustacheParseException( + "Invalid triple-mustache tag: {{" + text + "}}" + got, line); + } // convert it into (equivalent) {{&text}} which addTagSegment handles text.replace(0, 1, "&"); } diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index e60adcc..9f1d347 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -590,6 +590,12 @@ public class MustacheTest } catch (MustacheParseException e) { assertEquals("Invalid triple-mustache tag: {{{foo}} @ line 1", e.getMessage()); } + try { + Mustache.compiler().compile("{{{foo}}]"); + fail("Expected MustacheParseException"); + } catch (MustacheParseException e) { + assertEquals("Invalid triple-mustache tag: {{{foo}}] @ line 1", e.getMessage()); + } } @Test public void testNullValueGetsNullDefault () {