diff --git a/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java b/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java index 8db5dea..a8d038b 100644 --- a/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java +++ b/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java @@ -25,14 +25,11 @@ import com.esotericsoftware.yamlbeans.parser.Event; import com.esotericsoftware.yamlbeans.parser.MappingStartEvent; import com.esotericsoftware.yamlbeans.parser.NodeEvent; -import com.esotericsoftware.yamlbeans.parser.Parser; import com.esotericsoftware.yamlbeans.parser.ScalarEvent; import com.esotericsoftware.yamlbeans.parser.SequenceStartEvent; import java.io.BufferedWriter; -import java.io.FileReader; import java.io.IOException; -import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; @@ -645,14 +642,4 @@ private interface EmitterState { DEFAULT_TAG_PREFIXES_1_1.put("!", "!"); DEFAULT_TAG_PREFIXES_1_1.put("tag:yaml.org,2002:", "!!"); } - - public static void main (String[] args) throws IOException { - Parser parser = new Parser(new FileReader("test/test.yml")); - Emitter emitter = new Emitter(new OutputStreamWriter(System.out)); - while (true) { - Event event = parser.getNextEvent(); - if (event == null) break; - emitter.emit(event); - } - } } diff --git a/test/com/esotericsoftware/yamlbeans/emitter/EmitterTest.java b/test/com/esotericsoftware/yamlbeans/emitter/EmitterTest.java new file mode 100644 index 0000000..aa773b8 --- /dev/null +++ b/test/com/esotericsoftware/yamlbeans/emitter/EmitterTest.java @@ -0,0 +1,160 @@ +package com.esotericsoftware.yamlbeans.emitter; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import com.esotericsoftware.yamlbeans.YamlConfig; +import com.esotericsoftware.yamlbeans.YamlException; +import com.esotericsoftware.yamlbeans.YamlWriter; +import com.esotericsoftware.yamlbeans.parser.DocumentStartEvent; +import com.esotericsoftware.yamlbeans.parser.Event; +import com.esotericsoftware.yamlbeans.parser.Parser; +import com.esotericsoftware.yamlbeans.parser.ScalarEvent; + +public class EmitterTest { + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + private StringWriter stringWriter = null; + private Emitter emitter = null; + + @Before + public void setup() { + stringWriter = new StringWriter(); + emitter = new Emitter(stringWriter); + } + + @Test + public void testEmitterConstructor() throws IOException { + try { + new Emitter(null); + } catch (IllegalArgumentException e) { + assertEquals("stream cannot be null.", e.getMessage()); + } + + StringWriter writer = new StringWriter(); + new Emitter(writer); + + BufferedWriter bufferedWriter = new BufferedWriter(writer); + EmitterConfig emitterConfig = new EmitterConfig(); + new Emitter(bufferedWriter, emitterConfig); + } + + @Test(expected = EmitterException.class) + public void testStateStreamStart() throws IOException { + emitter.emit(Event.STREAM_START); + emitter.emit(new DocumentStartEvent(false, null, null)); + emitter.emit(Event.DOCUMENT_END_TRUE); + } + + @Test(expected = EmitterException.class) + public void testStateNOTHING() throws EmitterException, IOException { + emitter.emit(Event.STREAM_START); + emitter.emit(Event.STREAM_END); + assertEquals(3, emitter.state); + emitter.emit(Event.DOCUMENT_END_TRUE); + } + + @Test(expected = EmitterException.class) + public void testDocumentEnd() throws EmitterException, IOException { + emitter.emit(Event.STREAM_START); + emitter.emit(new DocumentStartEvent(false, null, null)); + emitter.emit(new ScalarEvent(null, null, new boolean[] { true, true }, "test", '\0')); + emitter.emit(Event.STREAM_END); + } + + @Test + public void testEmptySequence() throws YamlException { + YamlConfig yamlConfig = new YamlConfig(); + yamlConfig.writeConfig.setFlowStyle(true); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + List list = new ArrayList(); + yamlWriter.write(list); + yamlWriter.close(); + assertEquals("[]" + LINE_SEPARATOR, stringWriter.toString()); + } + + @Test + public void testStateFlowMapingKey() throws YamlException { + Map map = new LinkedHashMap(); + map.put("key1", "value1"); + map.put("key2", "value2"); + YamlConfig yamlConfig = new YamlConfig(); + yamlConfig.writeConfig.setFlowStyle(true); + yamlConfig.writeConfig.setWriteRootTags(false); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write(map); + yamlWriter.close(); + assertEquals("{key1: value1, key2: value2}" + LINE_SEPARATOR, stringWriter.toString()); + + } + + @Test + public void testStateFlowMapingKeyCanonicalIsTrue() throws EmitterException, IOException { + Map map = new LinkedHashMap(); + map.put("key1", "value1"); + map.put("key2", "value2"); + YamlConfig yamlConfig = new YamlConfig(); + yamlConfig.writeConfig.setFlowStyle(true); + yamlConfig.writeConfig.setWriteRootTags(false); + yamlConfig.writeConfig.setCanonical(true); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write(map); + yamlWriter.close(); + assertEquals( + "--- " + LINE_SEPARATOR + "{" + LINE_SEPARATOR + " ? !java.lang.String \"key1\"" + LINE_SEPARATOR + + " : !java.lang.String \"value1\"," + LINE_SEPARATOR + " ? !java.lang.String \"key2\"" + + LINE_SEPARATOR + " : !java.lang.String \"value2\"" + LINE_SEPARATOR + "}" + LINE_SEPARATOR, + stringWriter.toString()); + } + + @Test(expected = EmitterException.class) + public void testDocumentStart() throws EmitterException, IOException { + emitter.emit(Event.STREAM_START); + emitter.emit(new ScalarEvent(null, null, new boolean[] { true, true }, "test", '\0')); + } + + @Test + public void testBlockMappingValueMultiline() throws EmitterException, IOException { + Map map = new HashMap(); + map.put("555\n666", "value"); + YamlWriter yamlWriter = new YamlWriter(stringWriter); + yamlWriter.write(map); + yamlWriter.close(); + + assertEquals("? |-" + LINE_SEPARATOR + " 555" + LINE_SEPARATOR + " 666" + LINE_SEPARATOR + ": value" + + LINE_SEPARATOR, stringWriter.toString()); + } + + @Test(expected = EmitterException.class) + public void testExpectNode() throws EmitterException, IOException { + emitter.emit(Event.STREAM_START); + emitter.emit(new DocumentStartEvent(false, null, null)); + emitter.emit(Event.MAPPING_END); + } + + @Test + public void testParserToEmitter() throws EmitterException, IOException { + Parser parser = new Parser(new FileReader("test/test.yml")); + Emitter emitter = new Emitter(new OutputStreamWriter(System.out)); + while (true) { + Event event = parser.getNextEvent(); + if (event == null) + break; + emitter.emit(event); + } + emitter.close(); + } +} diff --git a/test/com/esotericsoftware/yamlbeans/emitter/EmitterWriterTest.java b/test/com/esotericsoftware/yamlbeans/emitter/EmitterWriterTest.java new file mode 100644 index 0000000..44e742e --- /dev/null +++ b/test/com/esotericsoftware/yamlbeans/emitter/EmitterWriterTest.java @@ -0,0 +1,73 @@ +package com.esotericsoftware.yamlbeans.emitter; + +import static org.junit.Assert.assertEquals; + +import java.io.StringWriter; +import org.junit.Test; + +import com.esotericsoftware.yamlbeans.YamlConfig; +import com.esotericsoftware.yamlbeans.YamlException; +import com.esotericsoftware.yamlbeans.YamlWriter; +import com.esotericsoftware.yamlbeans.YamlConfig.Quote; + +public class EmitterWriterTest { + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + @Test + public void testWriteDoubleQuoted() throws YamlException { + YamlConfig yamlConfig = new YamlConfig(); + yamlConfig.writeConfig.setQuoteChar(Quote.DOUBLE); + StringWriter stringWriter = new StringWriter(); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write("\u0001\u0011\u0111\u1111\u0007"); + yamlWriter.close(); + + assertEquals("\"\\u0001\\u0011\\u0111\\u1111\\a\"" + LINE_SEPARATOR, stringWriter.toString()); + + stringWriter = new StringWriter(); + yamlConfig.writeConfig.setWrapColumn(5); + yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write("testt est"); + yamlWriter.close(); + assertEquals("\"testt\\" + LINE_SEPARATOR + " \\ e\\" + LINE_SEPARATOR + " st\"" + LINE_SEPARATOR, + stringWriter.toString()); + } + + @Test + public void testWriteSingleQuoted() throws YamlException { + YamlConfig yamlConfig = new YamlConfig(); + yamlConfig.writeConfig.setQuoteChar(Quote.SINGLE); + + StringWriter stringWriter = new StringWriter(); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write("\ntest"); + yamlWriter.close(); + assertEquals("\'" + LINE_SEPARATOR + " test\'" + LINE_SEPARATOR, stringWriter.toString()); + + } + + @Test + public void testWriteFolded() throws YamlException { + YamlConfig yamlConfig = new YamlConfig(); + yamlConfig.writeConfig.setQuoteChar(Quote.FOLDED); + StringWriter stringWriter = new StringWriter(); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write("111\n222 333"); + yamlWriter.close(); + assertEquals(">-" + LINE_SEPARATOR + " 111" + LINE_SEPARATOR + LINE_SEPARATOR + " 222 333" + LINE_SEPARATOR, + stringWriter.toString()); + } + + @Test + public void testWriteLiteral() throws YamlException { + YamlConfig yamlConfig = new YamlConfig(); + yamlConfig.writeConfig.setQuoteChar(Quote.LITERAL); + StringWriter stringWriter = new StringWriter(); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write("111\n222"); + yamlWriter.close(); + assertEquals("|-" + LINE_SEPARATOR + " 111" + LINE_SEPARATOR + " 222" + LINE_SEPARATOR, + stringWriter.toString()); + } +}