From 2b9af91bf43a7aaade21c9cd1f855ae7c8d399ea Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Sun, 15 Feb 2015 01:44:13 +0200 Subject: [PATCH] Improved detection of invalid use of inline tables. --- .../java/com/moandjiezana/toml/Results.java | 6 +- .../moandjiezana/toml/InlineTableTest.java | 64 ++++++++++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/moandjiezana/toml/Results.java b/src/main/java/com/moandjiezana/toml/Results.java index a966278..02da5c7 100644 --- a/src/main/java/com/moandjiezana/toml/Results.java +++ b/src/main/java/com/moandjiezana/toml/Results.java @@ -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 valueMap = (Map) value; for (Map.Entry entry : valueMap.entrySet()) { diff --git a/src/test/java/com/moandjiezana/toml/InlineTableTest.java b/src/test/java/com/moandjiezana/toml/InlineTableTest.java index 37ed12f..53bf549 100644 --- a/src/test/java/com/moandjiezana/toml/InlineTableTest.java +++ b/src/test/java/com/moandjiezana/toml/InlineTableTest.java @@ -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 = {}"); + } }