Added Toml#isEmpty()

Added tests for navigation to non-existant keys
This commit is contained in:
moandji.ezana 2015-02-13 09:09:05 +02:00
parent ca7fcc766b
commit ba98daeb85
3 changed files with 28 additions and 11 deletions

View file

@ -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

View file

@ -198,6 +198,10 @@ public class Toml {
return tables;
}
public boolean isEmpty() {
return values.isEmpty();
}
/**
* <p>
* Populates an instance of targetClass with the values of this Toml instance.

View file

@ -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;
@ -60,6 +60,17 @@ public class TomlTest {
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 {
Toml toml = new Toml().parse("");
@ -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");