Added test to show that constructor defaults take precedence over method

defaults
This commit is contained in:
moandji.ezana 2015-03-30 22:09:12 +02:00
parent 7f11f97020
commit d31792e5a8

View file

@ -1,10 +1,11 @@
package com.moandjiezana.toml; package com.moandjiezana.toml;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Date;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.junit.Test; import org.junit.Test;
@ -82,4 +83,22 @@ public class DefaultValueTest {
Date defaultValue = calendar.getTime(); Date defaultValue = calendar.getTime();
assertEquals(defaultValue, toml.getDate("d", defaultValue)); assertEquals(defaultValue, toml.getDate("d", defaultValue));
} }
@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]");
Toml toml = new Toml(defaults).parse("");
assertEquals(1, toml.getLong("n", 2L).intValue());
assertEquals(1.1, toml.getDouble("d", 2.2), 0);
assertTrue(toml.getBoolean("b", false));
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(Calendar.YEAR, 2012);
Date defaultValue = calendar.getTime();
assertEquals(expected, toml.getDate("date", defaultValue));
assertEquals("a", toml.getString("s", "b"));
}
} }