Toml#getTables() returns null for missing keys and indices

This commit is contained in:
moandji.ezana 2015-04-29 22:10:30 +02:00
parent 24a5134c2c
commit ae2911c82c
3 changed files with 26 additions and 9 deletions

View file

@ -300,6 +300,10 @@ public class Toml {
current = ((Map<String, Object>) current).get(k.name); current = ((Map<String, Object>) current).get(k.name);
if (k.index > -1 && current != null) { if (k.index > -1 && current != null) {
if (k.index >= ((List<?>) current).size()) {
return null;
}
current = ((List<?>) current).get(k.index); current = ((List<?>) current).get(k.index);
} }

View file

@ -74,13 +74,6 @@ public class TableArrayTest {
assertEquals(3, toml.getTable("a").getTable("b").getTables("c").get(0).getLong("id").intValue()); assertEquals(3, toml.getTable("a").getTable("b").getTables("c").get(0).getLong("id").intValue());
} }
@Test
public void should_return_null_for_missing_table_array() throws Exception {
List<Toml> tomls = new Toml().parse("[a]").getTables("b");
assertNull(tomls);
}
@Test @Test
public void should_navigate_array_with_compound_key() throws Exception { public void should_navigate_array_with_compound_key() throws Exception {
Toml toml = new Toml().parse(file("fruit_table_array")); Toml toml = new Toml().parse(file("fruit_table_array"));
@ -94,6 +87,28 @@ public class TableArrayTest {
assertEquals("granny smith", appleVariety.getString("name")); assertEquals("granny smith", appleVariety.getString("name"));
assertEquals("plantain", bananaVariety); assertEquals("plantain", bananaVariety);
} }
@Test
public void should_return_null_for_missing_table_array() throws Exception {
Toml toml = new Toml().parse("[a]");
assertNull(toml.getTables("b"));
}
@Test
public void should_return_null_for_missing_table_array_with_index() throws Exception {
Toml toml = new Toml();
assertNull(toml.getTable("a[0]"));
assertNull(toml.getString("a[0].c"));
}
@Test
public void should_return_null_for_index_out_of_bounds() throws Exception {
Toml toml = new Toml().parse("[[a]]\n c = 1");
assertNull(toml.getTable("a[1]"));
}
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void should_fail_on_empty_table_array_name() { public void should_fail_on_empty_table_array_name() {

View file

@ -69,10 +69,8 @@ public class TableTest {
Toml toml = new Toml(); Toml toml = new Toml();
assertNull(toml.getString("a.b")); assertNull(toml.getString("a.b"));
assertNull(toml.getString("a.b[0].c"));
assertNull(toml.getList("a.b")); assertNull(toml.getList("a.b"));
assertNull(toml.getTable("a.b")); assertNull(toml.getTable("a.b"));
assertNull(toml.getTable("a.b[0]"));
} }
@Test @Test