2013-02-26 07:57:26 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
2014-08-12 10:53:18 +00:00
|
|
|
import static org.junit.Assert.assertNotNull;
|
2013-02-26 07:57:26 +00:00
|
|
|
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.List;
|
|
|
|
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
|
|
|
|
public void should_get_string() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("a = \"a\"");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals("a", toml.getString("a"));
|
|
|
|
}
|
|
|
|
|
2014-07-21 11:00:35 +00:00
|
|
|
@Test
|
|
|
|
public void should_get_empty_string() {
|
|
|
|
Toml toml = new Toml().parse("a = \"\"");
|
|
|
|
assertEquals("", toml.getString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_empty_string_with_trailing_new_line() {
|
|
|
|
Toml toml = new Toml().parse("a = \"\"\n");
|
|
|
|
assertEquals("", toml.getString("a"));
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test
|
|
|
|
public void should_get_number() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("b = 1001");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals(1001, toml.getLong("b").intValue());
|
|
|
|
}
|
|
|
|
|
2013-03-22 12:36:48 +00:00
|
|
|
@Test
|
|
|
|
public void should_get_negative_number() throws Exception {
|
|
|
|
Toml toml = new Toml().parse("b = -1001");
|
|
|
|
|
|
|
|
assertEquals(-1001, toml.getLong("b").intValue());
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test
|
2014-08-05 20:18:04 +00:00
|
|
|
public void should_get_array() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("list = [\"a\", \"b\", \"c\"]");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals(asList("a", "b", "c"), toml.getList("list", String.class));
|
|
|
|
}
|
|
|
|
|
2014-08-05 20:18:04 +00:00
|
|
|
@Test
|
|
|
|
public void should_allow_multiline_array() throws Exception {
|
|
|
|
Toml toml = new Toml().parse(file("should_allow_multiline_array"));
|
|
|
|
|
|
|
|
assertEquals(asList("a", "b", "c"), toml.getList("a", String.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2014-08-12 10:53:18 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-08-05 20:18:04 +00:00
|
|
|
public void should_get_nested_arrays() throws Exception {
|
2014-08-12 10:53:18 +00:00
|
|
|
Toml clients = new Toml().parse("data = [ [\"gamma\", \"delta\"], [1, 2]] # just an update to make sure parsers support it");
|
2014-08-05 20:18:04 +00:00
|
|
|
|
|
|
|
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.getList("data", String.class));
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test
|
|
|
|
public void should_get_boolean() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("bool_false = false\nbool_true = true");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertFalse(toml.getBoolean("bool_false"));
|
|
|
|
assertTrue(toml.getBoolean("bool_true"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_date() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("a_date = 2011-11-10T13:12:00Z");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.set(2011, Calendar.NOVEMBER, 10, 13, 12, 00);
|
|
|
|
calendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
|
|
|
|
assertEquals(calendar.getTime(), toml.getDate("a_date"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_double() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("double = 5.25");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals(5.25D, toml.getDouble("double").doubleValue(), 0.0);
|
|
|
|
}
|
|
|
|
|
2013-03-22 12:36:48 +00:00
|
|
|
@Test
|
|
|
|
public void should_get_negative_double() throws Exception {
|
|
|
|
Toml toml = new Toml().parse("double = -5.25");
|
|
|
|
|
|
|
|
assertEquals(-5.25D, toml.getDouble("double").doubleValue(), 0.0);
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@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("");
|
|
|
|
|
|
|
|
assertTrue(toml.getList("a", String.class).isEmpty());
|
|
|
|
}
|
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_return_empty_list_when_no_value_for_table_array() throws Exception {
|
|
|
|
List<Toml> tomls = new Toml().parse("[a]").getTables("b");
|
|
|
|
|
|
|
|
assertTrue(tomls.isEmpty());
|
|
|
|
}
|
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_support_numbers_in_key_names() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("a1 = 1");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("a1").intValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_support_numbers_in_table_names() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("[group1]\na = 1");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("group1.a").intValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_support_underscores_in_key_names() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("a_a = 1");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("a_a").intValue());
|
|
|
|
}
|
|
|
|
|
2013-02-28 07:33:16 +00:00
|
|
|
@Test
|
|
|
|
public void should_support_question_marks_in_key_names() throws Exception {
|
|
|
|
Toml toml = new Toml().parse("key?=true");
|
|
|
|
|
|
|
|
assertTrue(toml.getBoolean("key?"));
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_support_underscores_in_table_names() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
Toml toml = new Toml().parse("[group_a]\na = 1");
|
2013-02-26 07:57:26 +00:00
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("group_a.a").intValue());
|
|
|
|
}
|
|
|
|
|
2013-02-28 07:33:16 +00:00
|
|
|
@Test
|
2014-04-06 20:55:56 +00:00
|
|
|
public void should_support_sharp_sign_in_table_names() throws Exception {
|
2013-02-28 07:33:16 +00:00
|
|
|
Toml toml = new Toml().parse("[group#]\nkey=1");
|
|
|
|
|
|
|
|
assertEquals(1, toml.getLong("group#.key").intValue());
|
|
|
|
}
|
2014-08-12 10:53:18 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_support_spaces_in_table_names() throws Exception {
|
|
|
|
Toml toml = new Toml().parse("[valid key]");
|
|
|
|
|
|
|
|
assertNotNull(toml.getTable("valid 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"));
|
|
|
|
assertThat(toml.getList("e", String.class), Matchers.contains("a", "b"));
|
|
|
|
assertTrue(toml.getBoolean("f"));
|
|
|
|
}
|
|
|
|
|
2013-02-28 07:33:16 +00:00
|
|
|
@Test
|
2013-02-28 08:59:17 +00:00
|
|
|
public void should_support_special_characters_in_strings() {
|
|
|
|
Toml toml = new Toml().parse(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
|
2013-02-28 07:33:16 +00:00
|
|
|
|
2013-03-22 08:00:37 +00:00
|
|
|
assertEquals("\" \t \n \r \\ / \b \f", toml.getString("key"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_support_unicode_characters_in_strings() throws Exception {
|
2014-08-05 20:18:04 +00:00
|
|
|
Toml toml = new Toml().parse(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
|
2013-03-22 08:00:37 +00:00
|
|
|
|
2014-08-05 20:18:04 +00:00
|
|
|
assertEquals("more or less ±", toml.getString("unicode_key"));
|
2013-02-28 08:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_reserved_special_character_in_strings() throws Exception {
|
|
|
|
new Toml().parse("key=\"\\m\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_ignore_comma_at_end_of_array() throws Exception {
|
|
|
|
Toml toml = new Toml().parse("key=[1,2,3,]");
|
|
|
|
|
|
|
|
assertEquals(asList(1L, 2L, 3L), toml.getList("key", Long.class));
|
2013-02-28 07:33:16 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_when_dot_in_key_name() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
new Toml().parse("a.a = 1");
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
2014-08-03 12:24:31 +00:00
|
|
|
public void should_fail_on_non_existant_date() throws Exception {
|
2013-02-26 19:20:00 +00:00
|
|
|
new Toml().parse("d = 2012-13-01T15:00:00Z");
|
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-08-03 12:24:31 +00:00
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_invalid_number() throws Exception {
|
|
|
|
new Toml().parse("a = 200-");
|
|
|
|
}
|
|
|
|
|
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-04-10 13:07:45 +00:00
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_when_illegal_characters_after_key() throws Exception {
|
|
|
|
new Toml().parse("number = 3.14 pi");
|
|
|
|
}
|
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
|
|
|
}
|