2014-12-15 11:01:48 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
2015-01-25 11:07:54 +00:00
|
|
|
import static org.hamcrest.Matchers.contains;
|
2015-02-10 12:32:44 +00:00
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
2014-12-15 11:01:48 +00:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2015-04-29 18:27:31 +00:00
|
|
|
import static org.junit.Assert.assertNull;
|
2015-01-25 11:07:54 +00:00
|
|
|
import static org.junit.Assert.assertThat;
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
2015-02-10 12:32:44 +00:00
|
|
|
import java.util.Arrays;
|
2014-12-15 11:01:48 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import com.moandjiezana.toml.testutils.Utils;
|
|
|
|
|
|
|
|
public class ArrayTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_array() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("list = [\"a\", \"b\", \"c\"]");
|
2014-12-15 11:01:48 +00:00
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertEquals(asList("a", "b", "c"), toml.<String>getList("list"));
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 09:42:05 +00:00
|
|
|
@Test
|
2015-04-29 18:27:31 +00:00
|
|
|
public void should_return_null_if_no_value_for_key() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("");
|
2015-04-13 09:42:05 +00:00
|
|
|
|
2015-04-29 18:27:31 +00:00
|
|
|
assertNull(toml.getList("a"));
|
2015-04-13 09:42:05 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 11:01:48 +00:00
|
|
|
@Test
|
|
|
|
public void should_allow_multiline_array() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("should_allow_multiline_array"));
|
2014-12-15 11:01:48 +00:00
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertEquals(asList("a", "b", "c"), toml.<String>getList("a"));
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void should_get_nested_arrays() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml clients = new Toml().read("data = [ [\"gamma\", \"delta\"], [1, 2]] # just an update to make sure parsers support it");
|
2014-12-15 11:01:48 +00:00
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.<String>getList("data"));
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
2015-02-10 12:32:44 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_deeply_nested_arrays() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
List<List<?>> data = new Toml().read("data = [[[1], [2]], [3, 4]]").getList("data");
|
2015-02-10 12:32:44 +00:00
|
|
|
|
|
|
|
assertThat(data, hasSize(2));
|
|
|
|
assertEquals(Arrays.asList(1L), data.get(0).get(0));
|
|
|
|
assertEquals(asList(2L), data.get(0).get(1));
|
|
|
|
assertEquals(asList(3L, 4L), data.get(1));
|
|
|
|
}
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void should_get_nested_arrays_with_no_space_between_outer_and_inner_array() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml clients = new Toml().read("data = [[\"gamma\", \"delta\"], [1, 2]] # just an update to make sure parsers support it");
|
2014-12-15 11:01:48 +00:00
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.<String>getList("data"));
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_ignore_comma_at_end_of_array() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("key=[1,2,3,]");
|
2014-12-15 11:01:48 +00:00
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertEquals(asList(1L, 2L, 3L), toml.<Long>getList("key"));
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 11:07:54 +00:00
|
|
|
@Test
|
|
|
|
public void should_support_mixed_string_types() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("key = [\"a\", 'b', \"\"\"c\"\"\", '''d''']");
|
2015-01-25 11:07:54 +00:00
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertThat(toml.<String>getList("key"), contains("a", "b", "c", "d"));
|
2015-01-25 11:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_support_array_terminator_in_strings() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("key = [\"a]\", 'b]', \"\"\"c]\"\"\", '''d]''']");
|
2015-01-25 11:07:54 +00:00
|
|
|
|
2015-02-07 13:33:33 +00:00
|
|
|
assertThat(toml.<String>getList("key"), contains("a]", "b]", "c]", "d]"));
|
2015-01-25 11:07:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 12:32:44 +00:00
|
|
|
@Test
|
|
|
|
public void should_support_array_of_inline_tables() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(getClass().getResourceAsStream("should_support_array_of_inline_tables.toml"));
|
2015-02-10 12:32:44 +00:00
|
|
|
|
|
|
|
assertThat(toml.getList("points"), hasSize(4));
|
|
|
|
assertEquals(1, toml.getLong("points[0].x").longValue());
|
|
|
|
assertEquals(2, toml.getLong("points[0].y").longValue());
|
|
|
|
assertEquals(3, toml.getLong("points[0].z").longValue());
|
|
|
|
assertEquals(7, toml.getLong("points[1].x").longValue());
|
|
|
|
assertEquals(8, toml.getLong("points[1].y").longValue());
|
|
|
|
assertEquals(9, toml.getLong("points[1].z").longValue());
|
|
|
|
assertEquals(2, toml.getLong("points[2].x").longValue());
|
|
|
|
assertEquals(4, toml.getLong("points[2].y").longValue());
|
|
|
|
assertEquals(8, toml.getLong("points[2].z").longValue());
|
2015-02-11 14:02:30 +00:00
|
|
|
assertEquals("3", toml.getString("points[3].x"));
|
|
|
|
assertEquals("6", toml.getString("points[3].y"));
|
|
|
|
assertEquals("12", toml.getString("points[3].z"));
|
2015-02-10 12:32:44 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 11:01:48 +00:00
|
|
|
private File file(String file) {
|
|
|
|
return Utils.file(getClass(), file);
|
|
|
|
}
|
|
|
|
}
|