-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add emitter package to the unit test case (#153)
- Loading branch information
1 parent
0924b0a
commit 93bb8d2
Showing
3 changed files
with
233 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
test/com/esotericsoftware/yamlbeans/emitter/EmitterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> list = new ArrayList<String>(); | ||
yamlWriter.write(list); | ||
yamlWriter.close(); | ||
assertEquals("[]" + LINE_SEPARATOR, stringWriter.toString()); | ||
} | ||
|
||
@Test | ||
public void testStateFlowMapingKey() throws YamlException { | ||
Map<String, String> map = new LinkedHashMap<String, String>(); | ||
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<String, String> map = new LinkedHashMap<String, String>(); | ||
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<String, String> map = new HashMap<String, String>(); | ||
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(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
test/com/esotericsoftware/yamlbeans/emitter/EmitterWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |