Merge branch 'writer' of git@github.com:mwanji/toml4j.git into writer

Conflicts:
	src/main/java/com/moandjiezana/toml/TomlWriter.java
	src/test/java/com/moandjiezana/toml/TomlWriterTest.java
This commit is contained in:
moandji.ezana 2015-07-14 01:44:46 +02:00
commit 16915c8edb
4 changed files with 60 additions and 37 deletions

View file

@ -1,12 +1,12 @@
package com.moandjiezana.toml; package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueWriters.WRITERS;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import static com.moandjiezana.toml.ValueWriters.WRITERS;
abstract class ArrayValueWriter implements ValueWriter { abstract class ArrayValueWriter implements ValueWriter {
static protected boolean isArrayish(Object value) { static protected boolean isArrayish(Object value) {
return value instanceof Collection || value.getClass().isArray(); return value instanceof Collection || value.getClass().isArray();
@ -24,7 +24,7 @@ abstract class ArrayValueWriter implements ValueWriter {
return valueWriter.isPrimitiveType() || isArrayish(first); return valueWriter.isPrimitiveType() || isArrayish(first);
} }
return false; return true;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View file

@ -146,15 +146,16 @@ public class TomlWriter {
* @param from the object to be written * @param from the object to be written
* @param target the Writer to which TOML will be written. The Writer is not closed. * @param target the Writer to which TOML will be written. The Writer is not closed.
* @throws IOException if target.write() fails * @throws IOException if target.write() fails
* @throws IllegalStateException if
*/ */
public void write(Object from, Writer target) throws IOException { public void write(Object from, Writer target) throws IOException {
ValueWriter valueWriter = WRITERS.findWriterFor(from); ValueWriter valueWriter = WRITERS.findWriterFor(from);
if (valueWriter != MAP_VALUE_WRITER && valueWriter != OBJECT_VALUE_WRITER) { ValueWriter writer = WRITERS.findWriterFor(from);
throw new IllegalArgumentException("An object of type " + from.getClass().getSimpleName() + " cannot produce valid TOML."); if (writer == MAP_VALUE_WRITER || writer == OBJECT_VALUE_WRITER) {
} WRITERS.findWriterFor(from).write(from, context);
} else {
WriterContext context = new WriterContext(indentationPolicy, datePolicy, target); throw new IllegalStateException("Top-level value must not be a primitive or array.");
valueWriter.write(from, context); }
} }
} }

View file

@ -1,6 +1,10 @@
package com.moandjiezana.toml; package com.moandjiezana.toml;
import static org.junit.Assert.assertEquals; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.junit.*;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -15,15 +19,15 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import org.junit.Test; import static org.junit.Assert.*;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class BurntSushiValidEncoderTest { public class BurntSushiValidEncoderTest {
@Test
public void array_empty() throws Exception {
runEncoder("array-empty");
}
@Test @Test
public void arrays_hetergeneous() throws Exception { public void arrays_hetergeneous() throws Exception {
runEncoder("arrays-hetergeneous"); runEncoder("arrays-hetergeneous");

View file

@ -220,7 +220,7 @@ public class TomlWriterTest {
List<Integer> aList = new LinkedList<Integer>(); List<Integer> aList = new LinkedList<Integer>();
Float[] anArray = new Float[0]; Float[] anArray = new Float[0];
} }
assertEquals("", new TomlWriter().write(new TestClass())); assertEquals("aList = []\nanArray = []\n", new TomlWriter().write(new TestClass()));
} }
@Test @Test
@ -284,14 +284,17 @@ public class TomlWriterTest {
@Test @Test
public void should_write_strings_to_toml_utf8() throws UnsupportedEncodingException { public void should_write_strings_to_toml_utf8() throws UnsupportedEncodingException {
Map<String, String> input = new HashMap<String, String>(); class Utf8Test {
input.put("a", " é foo € \b \t \n \f \r \" \\ "); String input;
input.put("b", " \uD801\uDC28 \uD840\uDC0B "); // Check unicode code points greater than 0XFFFF }
String expected = "a = \" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\\\ \"\n" Utf8Test utf8Test = new Utf8Test();
+ "b = \" \\U00010428 \\U0002000B \"\n"; utf8Test.input = " é foo € \b \t \n \f \r \" \\ ";
assertEquals("input = \" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\\\ \"\n", new TomlWriter().write(utf8Test));
assertEquals(expected, new TomlWriter().write(input)); // Check unicode code points greater than 0XFFFF
utf8Test.input = " \uD801\uDC28 \uD840\uDC0B ";
assertEquals("input = \" \\U00010428 \\U0002000B \"\n", new TomlWriter().write(utf8Test));
} }
@Test @Test
@ -472,19 +475,34 @@ public class TomlWriterTest {
assertEquals("a = 1\n", readFile(output)); assertEquals("a = 1\n", readFile(output));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalStateException.class)
public void should_not_write_date() throws Exception { public void should_refuse_to_write_string_fragment() {
new TomlWriter().write("fragment");
}
@Test(expected = IllegalStateException.class)
public void should_refuse_to_write_boolean_fragment() {
new TomlWriter().write(true);
}
@Test(expected = IllegalStateException.class)
public void should_refuse_to_write_number_fragment() {
new TomlWriter().write(42);
}
@Test(expected = IllegalStateException.class)
public void should_refuse_to_write_date_fragment() {
new TomlWriter().write(new Date()); new TomlWriter().write(new Date());
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalStateException.class)
public void should_not_write_boolean() throws Exception { public void should_refuse_to_write_array_fragment() {
new TomlWriter().write(Boolean.TRUE); new TomlWriter().write(new int[2]);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalStateException.class)
public void should_not_write_number() throws Exception { public void should_refuse_to_write_table_array_fragment() {
new TomlWriter().write(Long.valueOf(1)); new TomlWriter().write(new SimpleTestClass[2]);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)