2015-04-28 09:57:19 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
|
|
|
import static java.util.Arrays.asList;
|
|
|
|
import static org.hamcrest.Matchers.contains;
|
|
|
|
import static org.hamcrest.Matchers.containsInAnyOrder;
|
2015-06-23 22:49:41 +00:00
|
|
|
import static org.hamcrest.Matchers.empty;
|
2015-04-28 09:57:19 +00:00
|
|
|
import static org.hamcrest.Matchers.hasEntry;
|
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
|
|
|
import static org.hamcrest.Matchers.instanceOf;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2015-04-28 21:31:59 +00:00
|
|
|
import org.junit.Rule;
|
2015-04-28 09:57:19 +00:00
|
|
|
import org.junit.Test;
|
2015-04-28 21:31:59 +00:00
|
|
|
import org.junit.rules.ExpectedException;
|
2015-04-28 09:57:19 +00:00
|
|
|
|
|
|
|
import com.moandjiezana.toml.testutils.Utils;
|
|
|
|
|
2015-06-12 13:32:15 +00:00
|
|
|
public class IterationTest {
|
2015-04-28 21:31:59 +00:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public final ExpectedException expectedException = ExpectedException.none();
|
2015-04-28 09:57:19 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_iterate_over_primitive() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("long"));
|
2015-04-28 21:31:59 +00:00
|
|
|
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
2015-04-28 09:57:19 +00:00
|
|
|
|
|
|
|
assertEquals("long", entry.getKey());
|
|
|
|
assertEquals(2L, entry.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void should_iterate_over_list() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("list"));
|
2015-04-28 21:31:59 +00:00
|
|
|
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
2015-04-28 09:57:19 +00:00
|
|
|
|
|
|
|
assertEquals("list", entry.getKey());
|
|
|
|
assertThat((List<String>) entry.getValue(), contains("a", "b", "c"));
|
|
|
|
}
|
|
|
|
|
2015-06-23 22:49:41 +00:00
|
|
|
@Test
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void should_iterate_over_empty_list() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("list = []");
|
2015-06-23 22:49:41 +00:00
|
|
|
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
|
|
|
|
|
|
|
assertEquals("list", entry.getKey());
|
|
|
|
assertThat((List<String>) entry.getValue(), empty());
|
|
|
|
}
|
|
|
|
|
2015-04-28 09:57:19 +00:00
|
|
|
@Test
|
|
|
|
public void should_iterate_over_table() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("table"));
|
2015-04-28 21:31:59 +00:00
|
|
|
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
2015-04-28 09:57:19 +00:00
|
|
|
|
|
|
|
assertEquals("table", entry.getKey());
|
|
|
|
assertEquals("a", ((Toml) entry.getValue()).getString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void should_iterate_over_table_array() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("table_array"));
|
2015-04-28 09:57:19 +00:00
|
|
|
|
2015-04-28 21:31:59 +00:00
|
|
|
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
2015-04-28 09:57:19 +00:00
|
|
|
List<Toml> tableArray = (List<Toml>) entry.getValue();
|
|
|
|
|
|
|
|
assertEquals("table_array", entry.getKey());
|
|
|
|
assertThat(tableArray, contains(instanceOf(Toml.class), instanceOf(Toml.class)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public void should_iterate_over_multiple_entries() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("multiple"));
|
2015-04-28 09:57:19 +00:00
|
|
|
|
|
|
|
Map<String, Object> entries = new HashMap<String, Object>();
|
2015-04-28 21:31:59 +00:00
|
|
|
for (Map.Entry<String, Object> entry : toml.entrySet()) {
|
2015-04-28 09:57:19 +00:00
|
|
|
entries.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThat(entries.keySet(), containsInAnyOrder("a", "b", "c", "e"));
|
|
|
|
assertThat(entries, hasEntry("a", (Object) "a"));
|
|
|
|
assertThat(entries, hasEntry("b", (Object) asList(1L, 2L, 3L)));
|
|
|
|
assertTrue(((Toml) entries.get("c")).getBoolean("d"));
|
|
|
|
assertThat(((List<Toml>) entries.get("e")), hasSize(1));
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:31:59 +00:00
|
|
|
@Test
|
|
|
|
public void should_not_support_setValue_method() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Map.Entry<String, Object> entry = new Toml().read("a = 1").entrySet().iterator().next();
|
2015-04-28 21:31:59 +00:00
|
|
|
|
|
|
|
expectedException.expect(UnsupportedOperationException.class);
|
|
|
|
entry.setValue(2L);
|
|
|
|
}
|
|
|
|
|
2015-06-23 22:49:41 +00:00
|
|
|
@Test
|
|
|
|
public void should_not_iterate_over_empty_toml() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("");
|
2015-06-23 22:49:41 +00:00
|
|
|
|
|
|
|
assertThat(toml.entrySet(), empty());
|
|
|
|
}
|
|
|
|
|
2015-04-28 09:57:19 +00:00
|
|
|
private File file(String name) {
|
|
|
|
return Utils.file(getClass(), "/IteratorTest/" + name);
|
|
|
|
}
|
|
|
|
}
|