From 14896a60334f291ff85f71da2ad05ca151308ba2 Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Sat, 14 Feb 2015 01:36:14 +0200 Subject: [PATCH] Code coverage improvements --- .../java/com/moandjiezana/toml/Container.java | 13 +---- .../toml/InlineTableConverter.java | 22 +------- .../java/com/moandjiezana/toml/Results.java | 54 +++++-------------- .../moandjiezana/toml/ErrorMessagesTest.java | 7 +++ .../moandjiezana/toml/InlineTableTest.java | 20 +++++++ .../java/com/moandjiezana/toml/TomlTest.java | 4 -- 6 files changed, 44 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/moandjiezana/toml/Container.java b/src/main/java/com/moandjiezana/toml/Container.java index 21b6c1a..834a891 100644 --- a/src/main/java/com/moandjiezana/toml/Container.java +++ b/src/main/java/com/moandjiezana/toml/Container.java @@ -68,21 +68,12 @@ abstract class Container { @Override void put(String key, Object value) { - if (value instanceof Container.Table) { - values.add((Container.Table) value); - return; - } - - getCurrent().put(key, value); + values.add((Container.Table) value); } @Override Object get(String key) { - if (values.isEmpty()) { - return null; - } - - return getCurrent().get(key); + throw new UnsupportedOperationException(); } List> getValues() { diff --git a/src/main/java/com/moandjiezana/toml/InlineTableConverter.java b/src/main/java/com/moandjiezana/toml/InlineTableConverter.java index 0bee80e..10d4864 100644 --- a/src/main/java/com/moandjiezana/toml/InlineTableConverter.java +++ b/src/main/java/com/moandjiezana/toml/InlineTableConverter.java @@ -21,7 +21,6 @@ class InlineTableConverter implements ValueConverter { int startIndex = sharedIndex.get(); boolean inKey = true; boolean inValue = false; - boolean quoted = false; boolean terminated = false; StringBuilder currentKey = new StringBuilder(); HashMap results = new HashMap(); @@ -30,12 +29,7 @@ class InlineTableConverter implements ValueConverter { for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet()) { char c = s.charAt(i); - if (c == '"' && inKey) { - quoted = !quoted; - currentKey.append(c); - } else if (quoted) { - currentKey.append(c); - } else if (inValue && !Character.isWhitespace(c)) { + if (inValue && !Character.isWhitespace(c)) { Object converted = CONVERTERS.convert(s, sharedIndex, context.with(Identifier.from(currentKey.toString(), context))); if (converted instanceof Results.Errors) { @@ -46,20 +40,6 @@ class InlineTableConverter implements ValueConverter { results.put(currentKey.toString().trim(), converted); currentKey = new StringBuilder(); inValue = false; - } else if (c == '{') { - sharedIndex.incrementAndGet(); - Object converted = convert(s, sharedIndex, context.with(Identifier.from(currentKey.toString(), context))); - - if (converted instanceof Results.Errors) { - errors.add((Results.Errors) converted); - return errors; - } - - results.put(currentKey.toString().trim(), converted); - - inKey = true; - inValue = false; - currentKey = new StringBuilder(); } else if (c == ',') { inKey = true; inValue = false; diff --git a/src/main/java/com/moandjiezana/toml/Results.java b/src/main/java/com/moandjiezana/toml/Results.java index 0a85431..11a8c08 100644 --- a/src/main/java/com/moandjiezana/toml/Results.java +++ b/src/main/java/com/moandjiezana/toml/Results.java @@ -19,29 +19,17 @@ class Results { } void emptyImplicitTable(String table, int line) { - sb.append("Invalid table definition due to empty implicit table name: "); - if (!table.startsWith("[")) { - sb.append('['); - } - sb.append(table); - if (!table.endsWith("]")) { - sb.append(']'); - } - sb.append("\n"); + sb.append("Invalid table definition due to empty implicit table name: ") + .append(table) + .append("\n"); } void invalidTable(String table, int line) { sb.append("Invalid table definition on line ") .append(line) - .append(": "); - if (!table.startsWith("[")) { - sb.append('['); - } - sb.append(table); - if (!table.endsWith("]")) { - sb.append(']'); - } - sb.append("]\n"); + .append(": ") + .append(table) + .append("]\n"); } void duplicateKey(String key, int line) { @@ -50,16 +38,6 @@ class Results { .append('\n'); } - void invalidIdentifier(Identifier identifier, int line) { - if (identifier.isKey()) { - invalidKey(identifier.getName(), line); - } else if (identifier.isTable()) { - invalidTable(identifier.getName(), line); - } else if (identifier.isTableArray()) { - invalidTableArray(identifier.getName(), line); - } - } - void invalidTextAfterIdentifier(Identifier identifier, char text, int line) { sb.append("Invalid text after key ") .append(identifier.getName()) @@ -70,12 +48,9 @@ class Results { } void invalidKey(String key, int line) { - sb.append("Invalid key"); - if (line > -1) { - sb.append(" on line ") - .append(line); - } - sb.append(": ") + sb.append("Invalid key on line ") + .append(line) + .append(": ") .append(key) .append('\n'); } @@ -100,10 +75,10 @@ class Results { void unterminatedKey(String key, int line) { sb.append("Key is not followed by an equals sign on line ") - .append(line) - .append(": ") - .append(key) - .append('\n'); + .append(line) + .append(": ") + .append(key) + .append('\n'); } void unterminated(String key, String value, int line) { @@ -215,8 +190,7 @@ class Results { for (int i = 0; i < tableParts.length; i++) { String tablePart = tableParts[i].name; Container currentContainer = stack.peek(); - if (tablePart.isEmpty()) { - } else if (currentContainer.get(tablePart) instanceof Container) { + if (currentContainer.get(tablePart) instanceof Container) { Container nextTable = (Container) currentContainer.get(tablePart); stack.push(nextTable); if (stack.peek() instanceof Container.TableArray) { diff --git a/src/test/java/com/moandjiezana/toml/ErrorMessagesTest.java b/src/test/java/com/moandjiezana/toml/ErrorMessagesTest.java index 3ba1a88..1775b8b 100644 --- a/src/test/java/com/moandjiezana/toml/ErrorMessagesTest.java +++ b/src/test/java/com/moandjiezana/toml/ErrorMessagesTest.java @@ -79,6 +79,13 @@ public class ErrorMessagesTest { new Toml().parse("k = [\"abc\""); } + @Test + public void should_message_unterminated_inline_table() throws Exception { + e.expectMessage("Unterminated value on line 1: k = { a = \"abc\""); + + new Toml().parse("k = { a = \"abc\""); + } + @Test public void should_message_key_without_equals() throws Exception { e.expectMessage("Key is not followed by an equals sign on line 2: k"); diff --git a/src/test/java/com/moandjiezana/toml/InlineTableTest.java b/src/test/java/com/moandjiezana/toml/InlineTableTest.java index 573ad3a..7982262 100644 --- a/src/test/java/com/moandjiezana/toml/InlineTableTest.java +++ b/src/test/java/com/moandjiezana/toml/InlineTableTest.java @@ -12,11 +12,16 @@ import java.util.Calendar; import java.util.List; import java.util.TimeZone; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class InlineTableTest { private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); + + @Rule + public ExpectedException e = ExpectedException.none(); @Test public void should_read_empty_inline_table() throws Exception { @@ -125,4 +130,19 @@ public class InlineTableTest { assertEquals("de]\"f", strings.getString("multiline")); assertEquals("gh]\"i", strings.getString("multiline_literal")); } + + @Test(expected = IllegalStateException.class) + public void should_fail_on_invalid_key() throws Exception { + new Toml().parse("tbl = { a. = 1 }"); + } + + @Test(expected = IllegalStateException.class) + public void should_fail_when_unterminated() throws Exception { + new Toml().parse("tbl = { a = 1 "); + } + + @Test(expected = IllegalStateException.class) + public void should_fail_on_invalid_value() throws Exception { + new Toml().parse("tbl = { a = abc }"); + } } diff --git a/src/test/java/com/moandjiezana/toml/TomlTest.java b/src/test/java/com/moandjiezana/toml/TomlTest.java index 76b1d48..1818966 100644 --- a/src/test/java/com/moandjiezana/toml/TomlTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlTest.java @@ -169,8 +169,4 @@ public class TomlTest { public void should_fail_when_illegal_characters_after_table() throws Exception { new Toml().parse("[error] if you didn't catch this, your parser is broken"); } - - private File file(String file) { - return new File(getClass().getResource(file + ".toml").getFile()); - } }