Skip to content

Commit

Permalink
Add emitter package to the unit test case (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr14huashao authored Aug 3, 2020
1 parent 0924b0a commit 93bb8d2
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 13 deletions.
13 changes: 0 additions & 13 deletions src/com/esotericsoftware/yamlbeans/emitter/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
160 changes: 160 additions & 0 deletions test/com/esotericsoftware/yamlbeans/emitter/EmitterTest.java
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 test/com/esotericsoftware/yamlbeans/emitter/EmitterWriterTest.java
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());
}
}

0 comments on commit 93bb8d2

Please sign in to comment.