Improved error message when key overwrites table

This commit is contained in:
moandji.ezana 2015-04-13 13:23:58 +02:00
parent 0795127266
commit 8cae6c2e32
3 changed files with 25 additions and 10 deletions

View file

@ -23,12 +23,20 @@ class Results {
}
public void tableDuplicatesKey(String table, AtomicInteger line) {
sb.append("Key already exists for table definition on line ")
sb.append("Key already exists for table defined on line ")
.append(line.get())
.append(": [")
.append(table)
.append("]\n");
}
public void keyDuplicatesTable(String key, AtomicInteger line) {
sb.append("Table already exists for key defined on line ")
.append(line.get())
.append(": ")
.append(key)
.append('\n');
}
void emptyImplicitTable(String table, int line) {
sb.append("Invalid table definition due to empty implicit table name: ")
@ -158,7 +166,11 @@ class Results {
} else if (currentTable.accepts(key)) {
currentTable.put(key, value);
} else {
errors.duplicateKey(key, line != null ? line.get() : -1);
if (currentTable.get(key) instanceof Container) {
errors.keyDuplicatesTable(key, line);
} else {
errors.duplicateKey(key, line != null ? line.get() : -1);
}
}
}
@ -224,11 +236,7 @@ class Results {
} else if (currentContainer.accepts(tablePart)) {
startTable(tablePart, line);
} else {
if (currentContainer.get(tablePart) instanceof Container) {
errors.duplicateTable(tableName, line.get());
} else {
errors.tableDuplicatesKey(tablePart, line);
}
errors.tableDuplicatesKey(tablePart, line);
break;
}
}

View file

@ -122,9 +122,16 @@ public class ErrorMessagesTest {
}
@Test
public void should_fail_when_key_itn_root_is_overwritten_by_table() throws Exception {
e.expectMessage("Key already exists for table definition on line 2: [a]");
public void should_message_key_in_root_is_overwritten_by_table() throws Exception {
e.expectMessage("Key already exists for table defined on line 2: [a]");
new Toml().parse("a=1\n [a]");
}
@Test
public void should_message_table_is_overwritten_by_key() throws Exception {
e.expectMessage("Table already exists for key defined on line 3: b");
new Toml().parse("[a.b]\n [a]\n b=1");
}
}

View file

@ -190,7 +190,7 @@ public class InlineTableTest {
@Test
public void should_fail_when_duplicated_by_other_key() throws Exception {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate key on line 2: tbl");
e.expectMessage("Table already exists for key defined on line 2: tbl");
new Toml().parse("tbl = { a = 1 }\n tbl = 1");
}