mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-29 11:42:15 +00:00
Throw exception when duplicate keys are defined inside an inline table
This commit is contained in:
parent
14896a6033
commit
40f12ca3b1
4 changed files with 41 additions and 2 deletions
|
@ -37,7 +37,14 @@ class InlineTableConverter implements ValueConverter {
|
|||
return errors;
|
||||
}
|
||||
|
||||
results.put(currentKey.toString().trim(), converted);
|
||||
String currentKeyTrimmed = currentKey.toString().trim();
|
||||
Object previous = results.put(currentKeyTrimmed, converted);
|
||||
|
||||
if (previous != null) {
|
||||
errors.duplicateKey(currentKeyTrimmed, context.line.get());
|
||||
return errors;
|
||||
}
|
||||
|
||||
currentKey = new StringBuilder();
|
||||
inValue = false;
|
||||
} else if (c == ',') {
|
||||
|
|
|
@ -33,7 +33,12 @@ class Results {
|
|||
}
|
||||
|
||||
void duplicateKey(String key, int line) {
|
||||
sb.append("Duplicate key: ")
|
||||
sb.append("Duplicate key");
|
||||
if (line > -1) {
|
||||
sb.append(" on line ")
|
||||
.append(line);
|
||||
}
|
||||
sb.append(": ")
|
||||
.append(key)
|
||||
.append('\n');
|
||||
}
|
||||
|
|
|
@ -22,6 +22,10 @@ class TomlParser {
|
|||
for (int i = index.get(); i < tomlString.length(); i = index.incrementAndGet()) {
|
||||
char c = tomlString.charAt(i);
|
||||
|
||||
if (results.errors.hasErrors()) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '#' && !inComment) {
|
||||
inComment = true;
|
||||
} else if (!Character.isWhitespace(c) && !inComment && identifier == null) {
|
||||
|
|
|
@ -131,6 +131,13 @@ public class InlineTableTest {
|
|||
assertEquals("gh]\"i", strings.getString("multiline_literal"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_read_inline_table_in_regular_table() throws Exception {
|
||||
Toml toml = new Toml().parse("[tbl]\n tbl = { tbl = 1 }");
|
||||
|
||||
assertEquals(1, toml.getLong("tbl.tbl.tbl").intValue());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void should_fail_on_invalid_key() throws Exception {
|
||||
new Toml().parse("tbl = { a. = 1 }");
|
||||
|
@ -145,4 +152,20 @@ public class InlineTableTest {
|
|||
public void should_fail_on_invalid_value() throws Exception {
|
||||
new Toml().parse("tbl = { a = abc }");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_fail_when_key_duplicated_inside_inline_table() throws Exception {
|
||||
e.expect(IllegalStateException.class);
|
||||
e.expectMessage("Duplicate key on line 1: a");
|
||||
|
||||
new Toml().parse("tbl = { a = 1, a = 2 }");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_fail_when_key_duplicated_by_other_key() throws Exception {
|
||||
e.expect(IllegalStateException.class);
|
||||
e.expectMessage("Duplicate key: tbl");
|
||||
|
||||
new Toml().parse("tbl = { a = 1 }\n tbl = 1");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue