Code coverage improvements

This commit is contained in:
moandji.ezana 2015-02-14 01:36:14 +02:00
parent ba98daeb85
commit 14896a6033
6 changed files with 44 additions and 76 deletions

View file

@ -68,21 +68,12 @@ abstract class Container {
@Override @Override
void put(String key, Object value) { void put(String key, Object value) {
if (value instanceof Container.Table) { values.add((Container.Table) value);
values.add((Container.Table) value);
return;
}
getCurrent().put(key, value);
} }
@Override @Override
Object get(String key) { Object get(String key) {
if (values.isEmpty()) { throw new UnsupportedOperationException();
return null;
}
return getCurrent().get(key);
} }
List<Map<String, Object>> getValues() { List<Map<String, Object>> getValues() {

View file

@ -21,7 +21,6 @@ class InlineTableConverter implements ValueConverter {
int startIndex = sharedIndex.get(); int startIndex = sharedIndex.get();
boolean inKey = true; boolean inKey = true;
boolean inValue = false; boolean inValue = false;
boolean quoted = false;
boolean terminated = false; boolean terminated = false;
StringBuilder currentKey = new StringBuilder(); StringBuilder currentKey = new StringBuilder();
HashMap<String, Object> results = new HashMap<String, Object>(); HashMap<String, Object> results = new HashMap<String, Object>();
@ -30,12 +29,7 @@ class InlineTableConverter implements ValueConverter {
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet()) { for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet()) {
char c = s.charAt(i); char c = s.charAt(i);
if (c == '"' && inKey) { if (inValue && !Character.isWhitespace(c)) {
quoted = !quoted;
currentKey.append(c);
} else if (quoted) {
currentKey.append(c);
} else if (inValue && !Character.isWhitespace(c)) {
Object converted = CONVERTERS.convert(s, sharedIndex, context.with(Identifier.from(currentKey.toString(), context))); Object converted = CONVERTERS.convert(s, sharedIndex, context.with(Identifier.from(currentKey.toString(), context)));
if (converted instanceof Results.Errors) { if (converted instanceof Results.Errors) {
@ -46,20 +40,6 @@ class InlineTableConverter implements ValueConverter {
results.put(currentKey.toString().trim(), converted); results.put(currentKey.toString().trim(), converted);
currentKey = new StringBuilder(); currentKey = new StringBuilder();
inValue = false; 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 == ',') { } else if (c == ',') {
inKey = true; inKey = true;
inValue = false; inValue = false;

View file

@ -19,29 +19,17 @@ class Results {
} }
void emptyImplicitTable(String table, int line) { void emptyImplicitTable(String table, int line) {
sb.append("Invalid table definition due to empty implicit table name: "); sb.append("Invalid table definition due to empty implicit table name: ")
if (!table.startsWith("[")) { .append(table)
sb.append('['); .append("\n");
}
sb.append(table);
if (!table.endsWith("]")) {
sb.append(']');
}
sb.append("\n");
} }
void invalidTable(String table, int line) { void invalidTable(String table, int line) {
sb.append("Invalid table definition on line ") sb.append("Invalid table definition on line ")
.append(line) .append(line)
.append(": "); .append(": ")
if (!table.startsWith("[")) { .append(table)
sb.append('['); .append("]\n");
}
sb.append(table);
if (!table.endsWith("]")) {
sb.append(']');
}
sb.append("]\n");
} }
void duplicateKey(String key, int line) { void duplicateKey(String key, int line) {
@ -50,16 +38,6 @@ class Results {
.append('\n'); .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) { void invalidTextAfterIdentifier(Identifier identifier, char text, int line) {
sb.append("Invalid text after key ") sb.append("Invalid text after key ")
.append(identifier.getName()) .append(identifier.getName())
@ -70,12 +48,9 @@ class Results {
} }
void invalidKey(String key, int line) { void invalidKey(String key, int line) {
sb.append("Invalid key"); sb.append("Invalid key on line ")
if (line > -1) { .append(line)
sb.append(" on line ") .append(": ")
.append(line);
}
sb.append(": ")
.append(key) .append(key)
.append('\n'); .append('\n');
} }
@ -100,10 +75,10 @@ class Results {
void unterminatedKey(String key, int line) { void unterminatedKey(String key, int line) {
sb.append("Key is not followed by an equals sign on line ") sb.append("Key is not followed by an equals sign on line ")
.append(line) .append(line)
.append(": ") .append(": ")
.append(key) .append(key)
.append('\n'); .append('\n');
} }
void unterminated(String key, String value, int line) { void unterminated(String key, String value, int line) {
@ -215,8 +190,7 @@ class Results {
for (int i = 0; i < tableParts.length; i++) { for (int i = 0; i < tableParts.length; i++) {
String tablePart = tableParts[i].name; String tablePart = tableParts[i].name;
Container currentContainer = stack.peek(); Container currentContainer = stack.peek();
if (tablePart.isEmpty()) { if (currentContainer.get(tablePart) instanceof Container) {
} else if (currentContainer.get(tablePart) instanceof Container) {
Container nextTable = (Container) currentContainer.get(tablePart); Container nextTable = (Container) currentContainer.get(tablePart);
stack.push(nextTable); stack.push(nextTable);
if (stack.peek() instanceof Container.TableArray) { if (stack.peek() instanceof Container.TableArray) {

View file

@ -79,6 +79,13 @@ public class ErrorMessagesTest {
new Toml().parse("k = [\"abc\""); 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 @Test
public void should_message_key_without_equals() throws Exception { public void should_message_key_without_equals() throws Exception {
e.expectMessage("Key is not followed by an equals sign on line 2: k"); e.expectMessage("Key is not followed by an equals sign on line 2: k");

View file

@ -12,11 +12,16 @@ import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
public class InlineTableTest { public class InlineTableTest {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
@Rule
public ExpectedException e = ExpectedException.none();
@Test @Test
public void should_read_empty_inline_table() throws Exception { public void should_read_empty_inline_table() throws Exception {
@ -125,4 +130,19 @@ public class InlineTableTest {
assertEquals("de]\"f", strings.getString("multiline")); assertEquals("de]\"f", strings.getString("multiline"));
assertEquals("gh]\"i", strings.getString("multiline_literal")); 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 }");
}
} }

View file

@ -169,8 +169,4 @@ public class TomlTest {
public void should_fail_when_illegal_characters_after_table() throws Exception { 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"); 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());
}
} }