View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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  
17  package org.yaml.snakeyaml.emitter;
18  
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.DumperOptions;
25  import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
26  import org.yaml.snakeyaml.Yaml;
27  
28  public class EmitterTest extends TestCase {
29  
30      public void testWriteFolded() {
31          DumperOptions options = new DumperOptions();
32          options.setDefaultScalarStyle(ScalarStyle.FOLDED);
33          String folded = "0123456789 0123456789\n0123456789 0123456789";
34          Map<String, String> map = new LinkedHashMap<String, String>();
35          map.put("aaa", folded);
36          map.put("bbb", "\nbla-bla\n");
37          Yaml yaml = new Yaml(options);
38          String output = yaml.dump(map);
39          String etalon = "\"aaa\": >-\n  0123456789 0123456789\n\n  0123456789 0123456789\n\"bbb\": >2\n\n  bla-bla\n";
40          assertEquals(etalon, output);
41      }
42  
43      public void testWriteLiteral() {
44          DumperOptions options = new DumperOptions();
45          options.setDefaultScalarStyle(ScalarStyle.LITERAL);
46          String folded = "0123456789 0123456789 0123456789 0123456789";
47          Map<String, String> map = new LinkedHashMap<String, String>();
48          map.put("aaa", folded);
49          map.put("bbb", "\nbla-bla\n");
50          Yaml yaml = new Yaml(options);
51          String output = yaml.dump(map);
52          String etalon = "\"aaa\": |-\n  0123456789 0123456789 0123456789 0123456789\n\"bbb\": |2\n\n  bla-bla\n";
53          assertEquals(etalon, output);
54      }
55  
56      public void testWritePlain() {
57          DumperOptions options = new DumperOptions();
58          options.setDefaultScalarStyle(ScalarStyle.PLAIN);
59          String folded = "0123456789 0123456789\n0123456789 0123456789";
60          Map<String, String> map = new LinkedHashMap<String, String>();
61          map.put("aaa", folded);
62          map.put("bbb", "\nbla-bla");
63          Yaml yaml = new Yaml(options);
64          String output = yaml.dump(map);
65          String etalon = "{aaa: '0123456789 0123456789\n\n    0123456789 0123456789', bbb: '\n\n    bla-bla'}\n";
66          assertEquals(etalon, output);
67      }
68  
69      public void testWritePlainPretty() {
70          DumperOptions options = new DumperOptions();
71          options.setDefaultScalarStyle(ScalarStyle.PLAIN);
72          options.setPrettyFlow(true);
73  
74          String folded = "0123456789 0123456789\n0123456789 0123456789";
75          Map<String, String> map = new LinkedHashMap<String, String>();
76          map.put("aaa", folded);
77          map.put("bbb", "\nbla-bla");
78  
79          Yaml yaml = new Yaml(options);
80          String output = yaml.dump(map);
81          String etalon = "{\n  aaa: '0123456789 0123456789\n\n    0123456789 0123456789',\n  bbb: '\n\n    bla-bla'\n}\n";
82          assertEquals(etalon, output);
83      }
84  
85      public void testWriteSingleQuoted() {
86          DumperOptions options = new DumperOptions();
87          options.setDefaultScalarStyle(ScalarStyle.SINGLE_QUOTED);
88          String folded = "0123456789 0123456789\n0123456789 0123456789";
89          Map<String, String> map = new LinkedHashMap<String, String>();
90          map.put("aaa", folded);
91          map.put("bbb", "\nbla-bla");
92          Yaml yaml = new Yaml(options);
93          String output = yaml.dump(map);
94          String etalon = "'aaa': '0123456789 0123456789\n\n  0123456789 0123456789'\n'bbb': '\n\n  bla-bla'\n";
95          assertEquals(etalon, output);
96      }
97  
98      public void testWriteDoubleQuoted() {
99          DumperOptions options = new DumperOptions();
100         options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
101         String folded = "0123456789 0123456789\n0123456789 0123456789";
102         Map<String, String> map = new LinkedHashMap<String, String>();
103         map.put("aaa", folded);
104         map.put("bbb", "\nbla-bla");
105         Yaml yaml = new Yaml(options);
106         String output = yaml.dump(map);
107         String etalon = "\"aaa\": \"0123456789 0123456789\\n0123456789 0123456789\"\n\"bbb\": \"\\nbla-bla\"\n";
108         assertEquals(etalon, output);
109     }
110 }