diff --git a/src/test/java/com/moandjiezana/toml/ArrayTest.java b/src/test/java/com/moandjiezana/toml/ArrayTest.java index 9e1f06a..f38751f 100644 --- a/src/test/java/com/moandjiezana/toml/ArrayTest.java +++ b/src/test/java/com/moandjiezana/toml/ArrayTest.java @@ -24,6 +24,13 @@ public class ArrayTest { assertEquals(asList("a", "b", "c"), toml.getList("list")); } + @Test + public void should_return_empty_list_if_no_value_for_key() throws Exception { + Toml toml = new Toml().parse(""); + + assertTrue(toml.getList("a").isEmpty()); + } + @Test public void should_allow_multiline_array() throws Exception { Toml toml = new Toml().parse(file("should_allow_multiline_array")); @@ -57,13 +64,6 @@ public class ArrayTest { assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.getList("data")); } - @Test - public void should_return_empty_list_when_no_value_for_table_array() throws Exception { - List tomls = new Toml().parse("[a]").getTables("b"); - - assertTrue(tomls.isEmpty()); - } - @Test public void should_ignore_comma_at_end_of_array() throws Exception { Toml toml = new Toml().parse("key=[1,2,3,]"); diff --git a/src/test/java/com/moandjiezana/toml/TableArrayTest.java b/src/test/java/com/moandjiezana/toml/TableArrayTest.java index d1a78d4..847befd 100644 --- a/src/test/java/com/moandjiezana/toml/TableArrayTest.java +++ b/src/test/java/com/moandjiezana/toml/TableArrayTest.java @@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import java.io.File; import java.util.List; @@ -74,6 +75,13 @@ public class TableArrayTest { assertEquals(3, toml.getTable("a").getTable("b").getTables("c").get(0).getLong("id").intValue()); } + @Test + public void should_return_empty_list_when_no_value_for_table_array() throws Exception { + List tomls = new Toml().parse("[a]").getTables("b"); + + assertTrue(tomls.isEmpty()); + } + @Test public void should_navigate_array_with_compound_key() throws Exception { Toml toml = new Toml().parse(file("fruit_table_array")); diff --git a/src/test/java/com/moandjiezana/toml/TableTest.java b/src/test/java/com/moandjiezana/toml/TableTest.java new file mode 100644 index 0000000..091d239 --- /dev/null +++ b/src/test/java/com/moandjiezana/toml/TableTest.java @@ -0,0 +1,85 @@ +package com.moandjiezana.toml; + +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class TableTest { + + @Test + public void should_get_table() throws Exception { + Toml toml = new Toml().parse("[group]\nkey = \"value\""); + + Toml group = toml.getTable("group"); + + assertEquals("value", group.getString("key")); + } + + @Test + public void should_get_value_for_multi_key() throws Exception { + Toml toml = new Toml().parse("[group]\nkey = \"value\""); + + assertEquals("value", toml.getString("group.key")); + } + + @Test + public void should_get_value_for_multi_key_with_no_parent_table() throws Exception { + Toml toml = new Toml().parse("[group.sub]\nkey = \"value\""); + + assertEquals("value", toml.getString("group.sub.key")); + } + + @Test + public void should_get_table_for_multi_key() throws Exception { + Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\""); + + assertEquals("value", toml.getTable("group.sub").getString("key")); + } + + @Test + public void should_get_table_for_multi_key_with_no_parent_table() throws Exception { + Toml toml = new Toml().parse("[group.sub]\nkey = \"value\""); + + assertEquals("value", toml.getTable("group.sub").getString("key")); + } + + @Test + public void should_get_value_from_table_with_sub_table() throws Exception { + Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2"); + + assertEquals(2, toml.getLong("a.d").intValue()); + assertEquals(1, toml.getTable("a.b").getLong("c").intValue()); + } + + @Test + public void should_handle_navigation_to_missing_value() throws Exception { + Toml toml = new Toml(); + + assertNull(toml.getString("a.b")); + assertNull(toml.getString("a.b[0].c")); + assertThat(toml.getList("a.b"), hasSize(0)); + assertTrue(toml.getTable("a.b").isEmpty()); + assertTrue(toml.getTable("a.b[0]").isEmpty()); + } + + @Test + public void should_return_null_when_no_value_for_multi_key() throws Exception { + Toml toml = new Toml().parse(""); + + assertNull(toml.getString("group.key")); + } + + @Test(expected = IllegalStateException.class) + public void should_fail_when_table_defined_twice() throws Exception { + new Toml().parse("[a]\nb=1\n[a]\nc=2"); + } + + @Test(expected = IllegalStateException.class) + 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"); + } +} diff --git a/src/test/java/com/moandjiezana/toml/TomlTest.java b/src/test/java/com/moandjiezana/toml/TomlTest.java index 1818966..14a586e 100644 --- a/src/test/java/com/moandjiezana/toml/TomlTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlTest.java @@ -1,6 +1,5 @@ package com.moandjiezana.toml; -import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; @@ -12,64 +11,14 @@ import java.util.Calendar; import java.util.TimeZone; import org.hamcrest.Matchers; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class TomlTest { - @Test - public void should_get_table() throws Exception { - Toml toml = new Toml().parse("[group]\nkey = \"value\""); - - Toml group = toml.getTable("group"); - - assertEquals("value", group.getString("key")); - } - - @Test - public void should_get_value_for_multi_key() throws Exception { - Toml toml = new Toml().parse("[group]\nkey = \"value\""); - - assertEquals("value", toml.getString("group.key")); - } - - @Test - public void should_get_value_for_multi_key_with_no_parent_table() throws Exception { - Toml toml = new Toml().parse("[group.sub]\nkey = \"value\""); - - assertEquals("value", toml.getString("group.sub.key")); - } - - @Test - public void should_get_table_for_multi_key() throws Exception { - Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\""); - - assertEquals("value", toml.getTable("group.sub").getString("key")); - } - - @Test - public void should_get_table_for_multi_key_with_no_parent_table() throws Exception { - Toml toml = new Toml().parse("[group.sub]\nkey = \"value\""); - - assertEquals("value", toml.getTable("group.sub").getString("key")); - } - - @Test - public void should_get_value_from_table_with_sub_table() throws Exception { - Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2"); - - assertEquals(2, toml.getLong("a.d").intValue()); - assertEquals(1, toml.getTable("a.b").getLong("c").intValue()); - } - @Test - public void should_handle_navigation_to_missing_value() throws Exception { - Toml toml = new Toml(); - - assertNull(toml.getString("a.b")); - assertNull(toml.getString("a.b[0].c")); - assertThat(toml.getList("a.b"), hasSize(0)); - assertTrue(toml.getTable("a.b").isEmpty()); - assertTrue(toml.getTable("a.b[0]").isEmpty()); - } + @Rule + public final ExpectedException expectedException = ExpectedException.none(); @Test public void should_return_null_if_no_value_for_key() throws Exception { @@ -78,20 +27,6 @@ public class TomlTest { assertNull(toml.getString("a")); } - @Test - public void should_return_empty_list_if_no_value_for_key() throws Exception { - Toml toml = new Toml().parse(""); - - assertTrue(toml.getList("a").isEmpty()); - } - - @Test - public void should_return_null_when_no_value_for_multi_key() throws Exception { - Toml toml = new Toml().parse(""); - - assertNull(toml.getString("group.key")); - } - @Test public void should_load_from_file() throws Exception { Toml toml = new Toml().parse(new File(getClass().getResource("should_load_from_file.toml").getFile())); @@ -154,19 +89,15 @@ public class TomlTest { public void should_fail_when_key_is_overwritten_by_table() { new Toml().parse("[a]\nb=1\n[a.b]\nc=2"); } + + @Test + public void should_fail_when_key_in_root_is_overwritten_by_table() throws Exception { + expectedException.expect(IllegalStateException.class); + new Toml().parse("a=1\n [a]"); + } @Test(expected = IllegalStateException.class) public void should_fail_when_key_is_overwritten_by_another_key() { new Toml().parse("[fruit]\ntype=\"apple\"\ntype=\"orange\""); } - - @Test(expected = IllegalStateException.class) - public void should_fail_when_table_defined_twice() throws Exception { - new Toml().parse("[a]\nb=1\n[a]\nc=2"); - } - - @Test(expected = IllegalStateException.class) - 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"); - } }