mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Code coverage improvements
This commit is contained in:
parent
ba98daeb85
commit
14896a6033
6 changed files with 44 additions and 76 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
@Override
|
||||
Object get(String key) {
|
||||
if (values.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getCurrent().get(key);
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
List<Map<String, Object>> getValues() {
|
||||
|
|
|
@ -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<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()) {
|
||||
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;
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -12,12 +12,17 @@ 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 {
|
||||
Toml toml = new Toml().parse("key = {}");
|
||||
|
@ -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 }");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue