mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Refuse to write TOML fragments.
This commit is contained in:
parent
8a8319b5f9
commit
568a77b1bf
2 changed files with 52 additions and 8 deletions
|
@ -1,7 +1,5 @@
|
||||||
package com.moandjiezana.toml;
|
package com.moandjiezana.toml;
|
||||||
|
|
||||||
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -13,6 +11,10 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import static com.moandjiezana.toml.MapValueWriter.*;
|
||||||
|
import static com.moandjiezana.toml.ObjectValueWriter.*;
|
||||||
|
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Converts Objects to TOML</p>
|
* <p>Converts Objects to TOML</p>
|
||||||
*
|
*
|
||||||
|
@ -144,9 +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 {
|
||||||
WriterContext context = new WriterContext(indentationPolicy, datePolicy, target);
|
WriterContext context = new WriterContext(indentationPolicy, datePolicy, target);
|
||||||
WRITERS.findWriterFor(from).write(from, context);
|
|
||||||
|
ValueWriter writer = WRITERS.findWriterFor(from);
|
||||||
|
if (writer == MAP_VALUE_WRITER || writer == OBJECT_VALUE_WRITER) {
|
||||||
|
WRITERS.findWriterFor(from).write(from, context);
|
||||||
|
} else {
|
||||||
|
throw new IllegalStateException("Top-level value must not be a primitive or array.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -281,12 +281,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 {
|
||||||
String input = " é foo € \b \t \n \f \r \" \\ ";
|
class Utf8Test {
|
||||||
assertEquals("\" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\\\ \"", new TomlWriter().write(input));
|
String input;
|
||||||
|
}
|
||||||
|
|
||||||
|
Utf8Test utf8Test = new Utf8Test();
|
||||||
|
utf8Test.input = " é foo € \b \t \n \f \r \" \\ ";
|
||||||
|
assertEquals("input = \" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\\\ \"\n", new TomlWriter().write(utf8Test));
|
||||||
|
|
||||||
// Check unicode code points greater than 0XFFFF
|
// Check unicode code points greater than 0XFFFF
|
||||||
input = " \uD801\uDC28 \uD840\uDC0B ";
|
utf8Test.input = " \uD801\uDC28 \uD840\uDC0B ";
|
||||||
assertEquals("\" \\U00010428 \\U0002000B \"", new TomlWriter().write(input));
|
assertEquals("input = \" \\U00010428 \\U0002000B \"\n", new TomlWriter().write(utf8Test));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -438,11 +443,41 @@ public class TomlWriterTest {
|
||||||
|
|
||||||
assertEquals(expected, writer.write(o));
|
assertEquals(expected, writer.write(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SimpleTestClass {
|
private static class SimpleTestClass {
|
||||||
int a = 1;
|
int a = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void should_refuse_to_write_array_fragment() {
|
||||||
|
new TomlWriter().write(new int[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void should_refuse_to_write_table_array_fragment() {
|
||||||
|
new TomlWriter().write(new SimpleTestClass[2]);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_write_to_writer() throws IOException {
|
public void should_write_to_writer() throws IOException {
|
||||||
StringWriter output = new StringWriter();
|
StringWriter output = new StringWriter();
|
||||||
|
|
Loading…
Reference in a new issue