mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-07-03 03:03:28 +00:00
getTable() and getTables() return null for missing keys
This commit is contained in:
parent
a210896477
commit
24a5134c2c
|
@ -203,23 +203,25 @@ public class Toml {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key A table name, not including square brackets.
|
* @param key A table name, not including square brackets.
|
||||||
* @return A new Toml instance. Empty if no value is found for key.
|
* @return A new Toml instance or <code>null</code> if no value is found for key.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Toml getTable(String key) {
|
public Toml getTable(String key) {
|
||||||
return new Toml(null, (Map<String, Object>) get(key));
|
Map<String, Object> map = (Map<String, Object>) get(key);
|
||||||
|
|
||||||
|
return map != null ? new Toml(null, map) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key Name of array of tables, not including square brackets.
|
* @param key Name of array of tables, not including square brackets.
|
||||||
* @return An empty {@link List} if no value is found for key.
|
* @return A {@link List} of Toml instances or <code>null</code> if no value is found for key.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Toml> getTables(String key) {
|
public List<Toml> getTables(String key) {
|
||||||
List<Map<String, Object>> tableArray = (List<Map<String, Object>>) get(key);
|
List<Map<String, Object>> tableArray = (List<Map<String, Object>>) get(key);
|
||||||
|
|
||||||
if (tableArray == null) {
|
if (tableArray == null) {
|
||||||
return Collections.emptyList();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<Toml> tables = new ArrayList<Toml>();
|
ArrayList<Toml> tables = new ArrayList<Toml>();
|
||||||
|
|
|
@ -5,7 +5,6 @@ import static org.hamcrest.Matchers.hasSize;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -76,10 +75,10 @@ public class TableArrayTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_return_empty_list_when_no_value_for_table_array() throws Exception {
|
public void should_return_null_for_missing_table_array() throws Exception {
|
||||||
List<Toml> tomls = new Toml().parse("[a]").getTables("b");
|
List<Toml> tomls = new Toml().parse("[a]").getTables("b");
|
||||||
|
|
||||||
assertTrue(tomls.isEmpty());
|
assertNull(tomls);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -54,14 +54,25 @@ public class TableTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_handle_navigation_to_missing_value() throws Exception {
|
public void should_get_empty_table() throws Exception {
|
||||||
|
Toml toml = new Toml().parse("[a]");
|
||||||
|
assertTrue(toml.getTable("a").isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_return_null_for_missing_table() throws Exception {
|
||||||
|
assertNull(new Toml().getTable("a"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_return_null_when_navigating_to_missing_value() throws Exception {
|
||||||
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.getString("a.b[0].c"));
|
||||||
assertNull(toml.getList("a.b"));
|
assertNull(toml.getList("a.b"));
|
||||||
assertTrue(toml.getTable("a.b").isEmpty());
|
assertNull(toml.getTable("a.b"));
|
||||||
assertTrue(toml.getTable("a.b[0]").isEmpty());
|
assertNull(toml.getTable("a.b[0]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -11,6 +11,7 @@ import java.util.Calendar;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
@ -64,10 +65,7 @@ public class TomlTest {
|
||||||
@Test
|
@Test
|
||||||
public void should_be_empty_if_no_values() throws Exception {
|
public void should_be_empty_if_no_values() throws Exception {
|
||||||
assertTrue(new Toml().isEmpty());
|
assertTrue(new Toml().isEmpty());
|
||||||
Toml toml = new Toml().parse("[a]");
|
assertFalse(new Toml().parse("a = 1").isEmpty());
|
||||||
assertTrue(toml.getTable("a").isEmpty());
|
|
||||||
assertTrue(toml.getTable("b").isEmpty());
|
|
||||||
assertFalse(toml.isEmpty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
|
|
Loading…
Reference in a new issue