View Javadoc

1   /**
2    * Copyright (c) 2008-2012, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.yaml.snakeyaml.emitter;
17  
18  import java.io.IOException;
19  import java.io.StringWriter;
20  import java.util.LinkedHashMap;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  import org.yaml.snakeyaml.DumperOptions;
26  import org.yaml.snakeyaml.Yaml;
27  import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
28  import org.yaml.snakeyaml.events.DocumentStartEvent;
29  import org.yaml.snakeyaml.events.ImplicitTuple;
30  import org.yaml.snakeyaml.events.ScalarEvent;
31  import org.yaml.snakeyaml.events.StreamStartEvent;
32  
33  public class EmitterTest extends TestCase {
34  
35      public void testWriteFolded() {
36          DumperOptions options = new DumperOptions();
37          options.setDefaultScalarStyle(ScalarStyle.FOLDED);
38          String folded = "0123456789 0123456789\n0123456789 0123456789";
39          Map<String, String> map = new LinkedHashMap<String, String>();
40          map.put("aaa", folded);
41          map.put("bbb", "\nbla-bla\n");
42          Yaml yaml = new Yaml(options);
43          String output = yaml.dump(map);
44          String etalon = "\"aaa\": >-\n  0123456789 0123456789\n\n  0123456789 0123456789\n\"bbb\": >2\n\n  bla-bla\n";
45          assertEquals(etalon, output);
46      }
47  
48      public void testWriteLiteral() {
49          DumperOptions options = new DumperOptions();
50          options.setDefaultScalarStyle(ScalarStyle.LITERAL);
51          String folded = "0123456789 0123456789 0123456789 0123456789";
52          Map<String, String> map = new LinkedHashMap<String, String>();
53          map.put("aaa", folded);
54          map.put("bbb", "\nbla-bla\n");
55          Yaml yaml = new Yaml(options);
56          String output = yaml.dump(map);
57          String etalon = "\"aaa\": |-\n  0123456789 0123456789 0123456789 0123456789\n\"bbb\": |2\n\n  bla-bla\n";
58          assertEquals(etalon, output);
59      }
60  
61      public void testWritePlain() {
62          DumperOptions options = new DumperOptions();
63          options.setDefaultScalarStyle(ScalarStyle.PLAIN);
64          String folded = "0123456789 0123456789\n0123456789 0123456789";
65          Map<String, String> map = new LinkedHashMap<String, String>();
66          map.put("aaa", folded);
67          map.put("bbb", "\nbla-bla");
68          Yaml yaml = new Yaml(options);
69          String output = yaml.dump(map);
70          String etalon = "aaa: |-\n  0123456789 0123456789\n  0123456789 0123456789\nbbb: |2-\n\n  bla-bla\n";
71          assertEquals(etalon, output);
72      }
73  
74      public void testWritePlainPretty() {
75          DumperOptions options = new DumperOptions();
76          options.setDefaultScalarStyle(ScalarStyle.PLAIN);
77          options.setPrettyFlow(true);
78  
79          String folded = "0123456789 0123456789\n0123456789 0123456789";
80          Map<String, String> map = new LinkedHashMap<String, String>();
81          map.put("aaa", folded);
82          map.put("bbb", "\nbla-bla");
83  
84          Yaml yaml = new Yaml(options);
85          String output = yaml.dump(map);
86          String etalon = "aaa: |-\n  0123456789 0123456789\n  0123456789 0123456789\nbbb: |2-\n\n  bla-bla\n";
87          assertEquals(etalon, output);
88      }
89  
90      public void testWriteSingleQuoted() {
91          DumperOptions options = new DumperOptions();
92          options.setDefaultScalarStyle(ScalarStyle.SINGLE_QUOTED);
93          String folded = "0123456789 0123456789\n0123456789 0123456789";
94          Map<String, String> map = new LinkedHashMap<String, String>();
95          map.put("aaa", folded);
96          map.put("bbb", "\nbla-bla");
97          Yaml yaml = new Yaml(options);
98          String output = yaml.dump(map);
99          String etalon = "'aaa': '0123456789 0123456789\n\n  0123456789 0123456789'\n'bbb': '\n\n  bla-bla'\n";
100         assertEquals(etalon, output);
101     }
102 
103     public void testWriteDoubleQuoted() {
104         DumperOptions options = new DumperOptions();
105         options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
106         String folded = "0123456789 0123456789\n0123456789 0123456789";
107         Map<String, String> map = new LinkedHashMap<String, String>();
108         map.put("aaa", folded);
109         map.put("bbb", "\nbla-bla");
110         Yaml yaml = new Yaml(options);
111         String output = yaml.dump(map);
112         String etalon = "\"aaa\": \"0123456789 0123456789\\n0123456789 0123456789\"\n\"bbb\": \"\\nbla-bla\"\n";
113         assertEquals(etalon, output);
114     }
115 
116     // Issue #158
117     public void testWriteSupplementaryUnicode() throws IOException {
118         DumperOptions options = new DumperOptions();
119         String burger = new String(Character.toChars(0x1f354));
120         String halfBurger = "\uD83C";
121         StringWriter output = new StringWriter();
122         Emitter emitter = new Emitter(output, options);
123 
124         emitter.emit(new StreamStartEvent(null, null));
125         emitter.emit(new DocumentStartEvent(null, null, false, null, null));
126         emitter.emit(new ScalarEvent(null, null, new ImplicitTuple(true, false), burger
127                 + halfBurger, null, null, '"'));
128         String expected = "! \"\\U0001f354\\ud83c\"";
129         assertEquals(expected, output.toString());
130     }
131 }