Improved detection of invalid use of inline tables.

This commit is contained in:
moandji.ezana 2015-02-15 01:44:13 +02:00
parent 40f12ca3b1
commit 2b9af91bf4
2 changed files with 66 additions and 4 deletions

View file

@ -128,7 +128,11 @@ class Results {
Container currentTable = stack.peek();
if (value instanceof Map) {
startTable(key);
if (stack.size() == 1) {
startTables(Identifier.from(key, null));
} else {
startTable(key);
}
@SuppressWarnings("unchecked")
Map<String, Object> valueMap = (Map<String, Object>) value;
for (Map.Entry<String, Object> entry : valueMap.entrySet()) {

View file

@ -115,11 +115,11 @@ public class InlineTableTest {
@Test
public void should_read_nested_inline_tables() throws Exception {
Toml tables = new Toml().parse("tables = { t1 = { t1_1 = 1, t1_2 = 2}, t2 = { t2_1 = \"a\"} }").getTable("tables");
Toml tables = new Toml().parse("tables = { t1 = { t1_1 = 1, t1_2 = 2}, t2 = { t2_1 = { t2_1_1 = \"a\" }} }").getTable("tables");
assertEquals(1L, tables.getLong("t1.t1_1").longValue());
assertEquals(2L, tables.getLong("t1.t1_2").longValue());
assertEquals("a", tables.getString("t2.t2_1"));
assertEquals("a", tables.getString("t2.t2_1.t2_1_1"));
}
@Test
@ -138,6 +138,32 @@ public class InlineTableTest {
assertEquals(1, toml.getLong("tbl.tbl.tbl").intValue());
}
@Test
public void should_mix_with_tables() throws Exception {
Toml toml = new Toml().parse("t = { k = 1 }\n [b]\n k = 2\n t = { k = 3}");
assertEquals(1, toml.getLong("t.k").intValue());
assertEquals(2, toml.getLong("b.k").intValue());
assertEquals(3, toml.getLong("b.t.k").intValue());
}
@Test
public void should_add_properties_to_existing_inline_table() throws Exception {
Toml toml = new Toml().parse("[a]\n b = {k = 1}\n [a.b.c]\n k = 2");
assertEquals(1, toml.getLong("a.b.k").intValue());
assertEquals(2, toml.getLong("a.b.c.k").intValue());
}
@Test
public void should_mix_with_table_arrays() throws Exception {
Toml toml = new Toml().parse("t = { k = 1 }\n [[b]]\n t = { k = 2 }\n [[b]]\n t = { k = 3 }");
assertEquals(1, toml.getLong("t.k").intValue());
assertEquals(2, toml.getLong("b[0].t.k").intValue());
assertEquals(3, toml.getLong("b[1].t.k").intValue());
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_invalid_key() throws Exception {
new Toml().parse("tbl = { a. = 1 }");
@ -162,10 +188,42 @@ public class InlineTableTest {
}
@Test
public void should_fail_when_key_duplicated_by_other_key() throws Exception {
public void should_fail_when_duplicated_by_other_key() throws Exception {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate key: tbl");
new Toml().parse("tbl = { a = 1 }\n tbl = 1");
}
@Test
public void should_fail_when_duplicated_by_other_inline_table() throws Exception {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate table definition: [tbl]");
new Toml().parse("tbl = { a = 1 }\n tbl = {}");
}
@Test
public void should_fail_when_duplicated_by_top_level_table() throws Exception {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate table definition: [tbl]");
new Toml().parse("tbl = {}\n [tbl]");
}
@Test
public void should_fail_when_duplicates_second_level_table() throws Exception {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate key: b");
new Toml().parse("[a.b]\n [a]\n b = {}");
}
@Test
public void should_fail_when_inline_table_duplicates_table() throws Exception {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate key: b");
new Toml().parse("[a.b]\n [a]\n b = {}");
}
}