Merge branch 'default_values' of git://github.com/udiabon/toml4j into default_values

This commit is contained in:
moandji.ezana 2015-03-30 21:51:33 +02:00
commit 7f11f97020
2 changed files with 110 additions and 0 deletions

View file

@ -136,10 +136,20 @@ public class Toml {
return (String) get(key);
}
public String getString(String key, String defaultValue) {
String val = getString(key);
return val == null ? defaultValue : val;
}
public Long getLong(String key) {
return (Long) get(key);
}
public Long getLong(String key, Long defaultValue) {
Long val = getLong(key);
return val == null ? defaultValue : val;
}
/**
* @param key a TOML key
* @param <T> type of list items
@ -160,14 +170,29 @@ public class Toml {
return (Boolean) get(key);
}
public Boolean getBoolean(String key, Boolean defaultValue) {
Boolean val = getBoolean(key);
return val == null ? defaultValue : val;
}
public Date getDate(String key) {
return (Date) get(key);
}
public Date getDate(String key, Date defaultValue) {
Date val = getDate(key);
return val == null ? defaultValue : val;
}
public Double getDouble(String key) {
return (Double) get(key);
}
public Double getDouble(String key, Double defaultValue) {
Double val = getDouble(key);
return val == null ? defaultValue : val;
}
/**
* @param key A table name, not including square brackets.
* @return A new Toml instance. Empty if no value is found for key.

View file

@ -0,0 +1,85 @@
package com.moandjiezana.toml;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class DefaultValueTest {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
@Test
public void should_get_string() throws Exception {
Toml toml = new Toml().parse("s = \"string value\"");
assertEquals("string value", toml.getString("s", "default string value"));
}
@Test
public void should_get_string_default_value() throws Exception {
Toml toml = new Toml().parse("");
assertEquals("default string value", toml.getString("s", "default string value"));
}
@Test
public void should_get_long() throws Exception {
Toml toml = new Toml().parse("n = 1001");
assertEquals(Long.valueOf(1001), toml.getLong("n", 1002L));
}
@Test
public void should_get_long_default_value() throws Exception {
Toml toml = new Toml().parse("");
assertEquals(Long.valueOf(1002), toml.getLong("n", 1002L));
}
@Test
public void should_get_double() throws Exception {
Toml toml = new Toml().parse("n = 0.5");
assertEquals(Double.valueOf(0.5), toml.getDouble("n", Double.valueOf(0.6)));
}
@Test
public void should_get_double_default_value() throws Exception {
Toml toml = new Toml().parse("");
assertEquals(Double.valueOf(0.6), toml.getDouble("n", Double.valueOf(0.6)));
}
@Test
public void should_get_boolean() throws Exception {
Toml toml = new Toml().parse("b = true");
assertEquals(Boolean.TRUE, toml.getBoolean("b", Boolean.FALSE));
}
@Test
public void should_get_boolean_default_value() throws Exception {
Toml toml = new Toml().parse("");
assertEquals(Boolean.FALSE, toml.getBoolean("b", Boolean.FALSE));
}
@Test
public void should_get_date() throws Exception {
Toml toml = new Toml().parse("d = 2011-11-10T13:12:00Z");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(2011, Calendar.NOVEMBER, 10, 13, 12, 00);
calendar.set(Calendar.MILLISECOND, 0);
Date expected = calendar.getTime();
calendar.set(2012, Calendar.NOVEMBER, 10, 13, 12, 00);
Date defaultValue = calendar.getTime();
assertEquals(expected, toml.getDate("d", defaultValue));
}
@Test
public void should_get_date_default_value() throws Exception {
Toml toml = new Toml().parse("");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(2012, Calendar.NOVEMBER, 10, 13, 12, 00);
calendar.set(Calendar.MILLISECOND, 0);
Date defaultValue = calendar.getTime();
assertEquals(defaultValue, toml.getDate("d", defaultValue));
}
}