2013-02-26 07:57:26 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNull;
|
2014-08-05 20:18:04 +00:00
|
|
|
import static org.junit.Assert.assertThat;
|
2013-02-26 07:57:26 +00:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.Calendar;
|
2014-04-08 10:52:56 +00:00
|
|
|
import java.util.Map;
|
2013-02-26 07:57:26 +00:00
|
|
|
import java.util.TimeZone;
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
import org.fest.reflect.core.Reflection;
|
2014-08-05 20:18:04 +00:00
|
|
|
import org.hamcrest.Matchers;
|
2013-02-26 07:57:26 +00:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
public class TomlTest {
|
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_get_table() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
2014-04-06 20:25:21 +00:00
|
|
|
Toml group = toml.getTable("group");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals("value", group.getString("key"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_value_for_multi_key() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals("value", toml.getString("group.key"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_get_value_for_multi_key_with_no_parent_table() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals("value", toml.getString("group.sub.key"));
|
|
|
|
}
|
|
|
|
|
2013-02-26 19:20:00 +00:00
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_get_table_for_multi_key() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\"");
|
|
|
|
|
2014-04-06 20:25:21 +00:00
|
|
|
assertEquals("value", toml.getTable("group.sub").getString("key"));
|
2013-02-26 19:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_get_table_for_multi_key_with_no_parent_table() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
|
|
|
|
|
2014-04-06 20:25:21 +00:00
|
|
|
assertEquals("value", toml.getTable("group.sub").getString("key"));
|
2013-02-26 19:20:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 08:00:37 +00:00
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_get_value_from_table_with_sub_table() throws Exception {
|
2013-03-22 08:00:37 +00:00
|
|
|
Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2");
|
|
|
|
|
|
|
|
assertEquals(2, toml.getLong("a.d").intValue());
|
2014-04-06 20:25:21 +00:00
|
|
|
assertEquals(1, toml.getTable("a.b").getLong("c").intValue());
|
2013-03-22 08:00:37 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test
|
|
|
|
public void should_return_null_if_no_value_for_key() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertNull(toml.getString("a"));
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
@Test
|
|
|
|
public void should_return_empty_list_if_no_value_for_key() throws Exception {
|
|
|
|
Toml toml = new Toml().parse("");
|
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertTrue(toml.<String>getList("a").isEmpty());
|
2014-04-08 10:52:56 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test
|
|
|
|
public void should_return_null_when_no_value_for_multi_key() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertNull(toml.getString("group.key"));
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
@Test
|
|
|
|
public void should_return_empty_toml_when_no_value_for_table() throws Exception {
|
|
|
|
Toml toml = new Toml().parse("[a]").getTable("b");
|
|
|
|
|
|
|
|
assertTrue(Reflection.field("values").ofType(Map.class).in(toml).get().isEmpty());
|
|
|
|
assertNull(toml.getString("x"));
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test
|
|
|
|
public void should_load_from_file() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse(new File(getClass().getResource("should_load_from_file.toml").getFile()));
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals("value", toml.getString("key"));
|
|
|
|
}
|
|
|
|
|
2013-02-28 07:33:16 +00:00
|
|
|
@Test
|
|
|
|
public void should_support_blank_lines() throws Exception {
|
|
|
|
Toml toml = new Toml().parse(new File(getClass().getResource("should_support_blank_line.toml").getFile()));
|
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("group.key").intValue());
|
|
|
|
}
|
|
|
|
|
2014-08-05 20:18:04 +00:00
|
|
|
@Test
|
|
|
|
public void should_allow_comment_after_values() throws Exception {
|
|
|
|
Toml toml = new Toml().parse(new File(getClass().getResource("should_allow_comment_after_values.toml").getFile()));
|
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("a").intValue());
|
|
|
|
assertEquals(1.1, toml.getDouble("b").doubleValue(), 0);
|
|
|
|
assertEquals("abc", toml.getString("c"));
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.set(2014, Calendar.AUGUST, 4, 13, 47, 0);
|
|
|
|
cal.set(Calendar.MILLISECOND, 0);
|
|
|
|
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
assertEquals(cal.getTime(), toml.getDate("d"));
|
2015-02-07 13:33:33 +00:00
|
|
|
assertThat(toml.<String>getList("e"), Matchers.contains("a", "b"));
|
2014-08-05 20:18:04 +00:00
|
|
|
assertTrue(toml.getBoolean("f"));
|
2014-08-22 21:59:02 +00:00
|
|
|
assertEquals("abc", toml.getString("g"));
|
|
|
|
assertEquals("abc", toml.getString("h"));
|
|
|
|
assertEquals("abc\nabc", toml.getString("i"));
|
|
|
|
assertEquals("abc\nabc", toml.getString("j"));
|
2014-08-05 20:18:04 +00:00
|
|
|
}
|
2014-08-12 11:15:12 +00:00
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_empty_key_name() throws Exception {
|
|
|
|
new Toml().parse(" = 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_key_name_with_hash() throws Exception {
|
|
|
|
new Toml().parse("a# = 1");
|
|
|
|
}
|
2014-12-15 21:47:06 +00:00
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_key_name_starting_with_square_bracket() throws Exception {
|
|
|
|
new Toml().parse("[a = 1");
|
|
|
|
}
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_fail_when_key_is_overwritten_by_table() {
|
2013-03-22 08:00:37 +00:00
|
|
|
new Toml().parse("[a]\nb=1\n[a.b]\nc=2");
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_when_key_is_overwritten_by_another_key() {
|
2013-02-26 19:20:00 +00:00
|
|
|
new Toml().parse("[fruit]\ntype=\"apple\"\ntype=\"orange\"");
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|
2013-03-22 08:00:37 +00:00
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_fail_when_table_defined_twice() throws Exception {
|
2013-03-22 08:00:37 +00:00
|
|
|
new Toml().parse("[a]\nb=1\n[a]\nc=2");
|
|
|
|
}
|
|
|
|
|
2014-04-06 19:12:11 +00:00
|
|
|
@Test(expected = IllegalStateException.class)
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_fail_when_illegal_characters_after_table() throws Exception {
|
2013-03-22 08:00:37 +00:00
|
|
|
new Toml().parse("[error] if you didn't catch this, your parser is broken");
|
|
|
|
}
|
2014-08-22 21:59:02 +00:00
|
|
|
|
2014-08-05 20:18:04 +00:00
|
|
|
private File file(String file) {
|
|
|
|
return new File(getClass().getResource(file + ".toml").getFile());
|
|
|
|
}
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|