Moved tests to more appropriate classes

This commit is contained in:
moandji.ezana 2015-04-13 11:42:05 +02:00
parent 1e19f0847f
commit 1a6ddbd5ac
4 changed files with 110 additions and 86 deletions

View file

@ -24,6 +24,13 @@ public class ArrayTest {
assertEquals(asList("a", "b", "c"), toml.<String>getList("list"));
}
@Test
public void should_return_empty_list_if_no_value_for_key() throws Exception {
Toml toml = new Toml().parse("");
assertTrue(toml.<String>getList("a").isEmpty());
}
@Test
public void should_allow_multiline_array() throws Exception {
Toml toml = new Toml().parse(file("should_allow_multiline_array"));
@ -57,13 +64,6 @@ public class ArrayTest {
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.<String>getList("data"));
}
@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());
}
@Test
public void should_ignore_comma_at_end_of_array() throws Exception {
Toml toml = new Toml().parse("key=[1,2,3,]");

View file

@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
@ -74,6 +75,13 @@ public class TableArrayTest {
assertEquals(3, toml.getTable("a").getTable("b").getTables("c").get(0).getLong("id").intValue());
}
@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());
}
@Test
public void should_navigate_array_with_compound_key() throws Exception {
Toml toml = new Toml().parse(file("fruit_table_array"));

View file

@ -0,0 +1,85 @@
package com.moandjiezana.toml;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TableTest {
@Test
public void should_get_table() throws Exception {
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
Toml group = toml.getTable("group");
assertEquals("value", group.getString("key"));
}
@Test
public void should_get_value_for_multi_key() throws Exception {
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
assertEquals("value", toml.getString("group.key"));
}
@Test
public void should_get_value_for_multi_key_with_no_parent_table() throws Exception {
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getString("group.sub.key"));
}
@Test
public void should_get_table_for_multi_key() throws Exception {
Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
public void should_get_table_for_multi_key_with_no_parent_table() throws Exception {
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
public void should_get_value_from_table_with_sub_table() throws Exception {
Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2");
assertEquals(2, toml.getLong("a.d").intValue());
assertEquals(1, toml.getTable("a.b").getLong("c").intValue());
}
@Test
public void should_handle_navigation_to_missing_value() throws Exception {
Toml toml = new Toml();
assertNull(toml.getString("a.b"));
assertNull(toml.getString("a.b[0].c"));
assertThat(toml.getList("a.b"), hasSize(0));
assertTrue(toml.getTable("a.b").isEmpty());
assertTrue(toml.getTable("a.b[0]").isEmpty());
}
@Test
public void should_return_null_when_no_value_for_multi_key() throws Exception {
Toml toml = new Toml().parse("");
assertNull(toml.getString("group.key"));
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_table_defined_twice() throws Exception {
new Toml().parse("[a]\nb=1\n[a]\nc=2");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_illegal_characters_after_table() throws Exception {
new Toml().parse("[error] if you didn't catch this, your parser is broken");
}
}

View file

@ -1,6 +1,5 @@
package com.moandjiezana.toml;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@ -12,64 +11,14 @@ import java.util.Calendar;
import java.util.TimeZone;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class TomlTest {
@Test
public void should_get_table() throws Exception {
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
Toml group = toml.getTable("group");
assertEquals("value", group.getString("key"));
}
@Test
public void should_get_value_for_multi_key() throws Exception {
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
assertEquals("value", toml.getString("group.key"));
}
@Test
public void should_get_value_for_multi_key_with_no_parent_table() throws Exception {
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getString("group.sub.key"));
}
@Test
public void should_get_table_for_multi_key() throws Exception {
Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
public void should_get_table_for_multi_key_with_no_parent_table() throws Exception {
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
public void should_get_value_from_table_with_sub_table() throws Exception {
Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2");
assertEquals(2, toml.getLong("a.d").intValue());
assertEquals(1, toml.getTable("a.b").getLong("c").intValue());
}
@Test
public void should_handle_navigation_to_missing_value() throws Exception {
Toml toml = new Toml();
assertNull(toml.getString("a.b"));
assertNull(toml.getString("a.b[0].c"));
assertThat(toml.getList("a.b"), hasSize(0));
assertTrue(toml.getTable("a.b").isEmpty());
assertTrue(toml.getTable("a.b[0]").isEmpty());
}
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void should_return_null_if_no_value_for_key() throws Exception {
@ -78,20 +27,6 @@ public class TomlTest {
assertNull(toml.getString("a"));
}
@Test
public void should_return_empty_list_if_no_value_for_key() throws Exception {
Toml toml = new Toml().parse("");
assertTrue(toml.<String>getList("a").isEmpty());
}
@Test
public void should_return_null_when_no_value_for_multi_key() throws Exception {
Toml toml = new Toml().parse("");
assertNull(toml.getString("group.key"));
}
@Test
public void should_load_from_file() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("should_load_from_file.toml").getFile()));
@ -155,18 +90,14 @@ public class TomlTest {
new Toml().parse("[a]\nb=1\n[a.b]\nc=2");
}
@Test
public void should_fail_when_key_in_root_is_overwritten_by_table() throws Exception {
expectedException.expect(IllegalStateException.class);
new Toml().parse("a=1\n [a]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_key_is_overwritten_by_another_key() {
new Toml().parse("[fruit]\ntype=\"apple\"\ntype=\"orange\"");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_table_defined_twice() throws Exception {
new Toml().parse("[a]\nb=1\n[a]\nc=2");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_illegal_characters_after_table() throws Exception {
new Toml().parse("[error] if you didn't catch this, your parser is broken");
}
}