diff --git a/CHANGELOG.md b/CHANGELOG.md index a60346f..7af632d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,13 @@ ### Changed -* __REAKING:__ Toml#getList(String) replaced Toml#getList(String, Class) +* __BREAKING:__ Toml#getList(String) replaced Toml#getList(String, Class) * Dropped dependency on Parboiled and its significant transitive dependencies ### Added * Support for [TOML 0.4.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md) +* Toml#isEmpty() * Line numbers included in error messages ### Fixed diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index 1bf9f4e..882654f 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -198,6 +198,10 @@ public class Toml { return tables; } + public boolean isEmpty() { + return values.isEmpty(); + } + /** *

* Populates an instance of targetClass with the values of this Toml instance. diff --git a/src/test/java/com/moandjiezana/toml/TomlTest.java b/src/test/java/com/moandjiezana/toml/TomlTest.java index c00ac11..76b1d48 100644 --- a/src/test/java/com/moandjiezana/toml/TomlTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlTest.java @@ -1,16 +1,16 @@ 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; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.io.File; import java.util.Calendar; -import java.util.Map; import java.util.TimeZone; -import org.fest.reflect.core.Reflection; import org.hamcrest.Matchers; import org.junit.Test; @@ -59,6 +59,17 @@ public class TomlTest { 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_if_no_value_for_key() throws Exception { @@ -81,14 +92,6 @@ public class TomlTest { assertNull(toml.getString("group.key")); } - @Test - public void should_return_empty_toml_when_no_value_for_table() throws Exception { - Toml toml = new Toml().parse("[a]").getTable("b"); - - assertTrue(Reflection.field("values").ofType(Map.class).in(toml).get().isEmpty()); - assertNull(toml.getString("x")); - } - @Test public void should_load_from_file() throws Exception { Toml toml = new Toml().parse(new File(getClass().getResource("should_load_from_file.toml").getFile())); @@ -123,6 +126,15 @@ public class TomlTest { assertEquals("abc\nabc", toml.getString("j")); } + @Test + public void should_be_empty_if_no_values() throws Exception { + assertTrue(new Toml().isEmpty()); + Toml toml = new Toml().parse("[a]"); + assertTrue(toml.getTable("a").isEmpty()); + assertTrue(toml.getTable("b").isEmpty()); + assertFalse(toml.isEmpty()); + } + @Test(expected = IllegalStateException.class) public void should_fail_on_empty_key_name() throws Exception { new Toml().parse(" = 1");