mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-29 11:42:15 +00:00
Added getArray(String, List)
This commit is contained in:
parent
c0eafb9f17
commit
d958baebb5
2 changed files with 38 additions and 0 deletions
|
@ -166,6 +166,19 @@ public class Toml {
|
|||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key a TOML key
|
||||
* @param defaultValue a list of default values
|
||||
* @param <T> type of list items
|
||||
* @return an empty {@link List} is the key is not found
|
||||
*/
|
||||
public <T> List<T> getList(String key, List<T> defaultValue) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<T> val = (List<T>) get(key);
|
||||
|
||||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key) {
|
||||
return (Boolean) get(key);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
|
@ -86,6 +88,28 @@ public class DefaultValueTest {
|
|||
assertEquals(_2012_11_10, toml.getDate("d", _2012_11_10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_array() throws Exception {
|
||||
Toml toml = new Toml().parse("a = [1, 2, 3]\n b = []");
|
||||
|
||||
assertEquals(asList(1L, 2L, 3L), toml.getList("a", asList(3L, 2L, 1L)));
|
||||
assertEquals(Collections.emptyList(), toml.getList("b", asList(3L, 2L, 1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_empty_array() throws Exception {
|
||||
Toml toml = new Toml().parse("a = []");
|
||||
|
||||
assertEquals(Collections.emptyList(), toml.getList("a", asList(3L, 2L, 1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_array_default_value() throws Exception {
|
||||
Toml toml = new Toml();
|
||||
|
||||
assertEquals(asList(3L, 2L, 1L), toml.getList("a", asList(3L, 2L, 1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_prefer_default_from_constructor() throws Exception {
|
||||
Toml defaults = new Toml().parse("n = 1\n d = 1.1\n b = true\n date = 2011-11-10T13:12:00Z\n s = 'a'\n a = [1, 2, 3]");
|
||||
|
@ -96,5 +120,6 @@ public class DefaultValueTest {
|
|||
assertTrue(toml.getBoolean("b", false));
|
||||
assertEquals(_2011_11_10, toml.getDate("date", _2012_11_10));
|
||||
assertEquals("a", toml.getString("s", "b"));
|
||||
assertEquals(asList(1L, 2L, 3L), toml.getList("a", asList(3L, 2L, 1L)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue