aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/emitter/EmitterTest.java
blob: 2c5f2432680e3c11faa5a3556f8398bcd193f4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.emitter;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import junit.framework.TestCase;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.events.DocumentStartEvent;
import org.yaml.snakeyaml.events.ImplicitTuple;
import org.yaml.snakeyaml.events.ScalarEvent;
import org.yaml.snakeyaml.events.StreamStartEvent;

public class EmitterTest extends TestCase {

  public void testWriteFolded() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.FOLDED);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla\n");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon =
        "\"aaa\": >-\n  0123456789 0123456789\n\n  0123456789 0123456789\n\"bbb\": >2\n\n  bla-bla\n";
    assertEquals(etalon, output);
  }

  public void testWriteLiteral() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.LITERAL);
    String folded = "0123456789 0123456789 0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla\n");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon =
        "\"aaa\": |-\n  0123456789 0123456789 0123456789 0123456789\n\"bbb\": |2\n\n  bla-bla\n";
    assertEquals(etalon, output);
  }

  public void testWritePlain() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.PLAIN);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon =
        "aaa: |-\n  0123456789 0123456789\n  0123456789 0123456789\nbbb: |2-\n\n  bla-bla\n";
    assertEquals(etalon, output);
  }

  public void testWritePlainPretty() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.PLAIN);
    options.setPrettyFlow(true);

    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla");

    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon =
        "aaa: |-\n  0123456789 0123456789\n  0123456789 0123456789\nbbb: |2-\n\n  bla-bla\n";
    assertEquals(etalon, output);
  }

  public void testWriteSingleQuoted() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.SINGLE_QUOTED);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon =
        "'aaa': '0123456789 0123456789\n\n  0123456789 0123456789'\n'bbb': '\n\n  bla-bla'\n";
    assertEquals(etalon, output);
  }

  public void testWriteDoubleQuoted() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
    String folded = "0123456789 0123456789\n0123456789 0123456789";
    Map<String, String> map = new LinkedHashMap<String, String>();
    map.put("aaa", folded);
    map.put("bbb", "\nbla-bla");
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon =
        "\"aaa\": \"0123456789 0123456789\\n0123456789 0123456789\"\n\"bbb\": \"\\nbla-bla\"\n";
    assertEquals(etalon, output);
  }

  // Issue #158
  public void testWriteSupplementaryUnicode() throws IOException {
    DumperOptions options = new DumperOptions();
    String burger = new String(Character.toChars(0x1f354));
    String halfBurger = "\uD83C";
    StringWriter output = new StringWriter();
    Emitter emitter = new Emitter(output, options);

    emitter.emit(new StreamStartEvent(null, null));
    emitter.emit(new DocumentStartEvent(null, null, false, null, null));
    emitter.emit(new ScalarEvent(null, null, new ImplicitTuple(true, false), burger + halfBurger,
        null, null, DumperOptions.ScalarStyle.DOUBLE_QUOTED));
    String expected = "! \"🍔\\ud83c\"";
    assertEquals(expected, output.toString());
  }

  public void testSplitLineExpectFirstFlowSequenceItem() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    options.setWidth(8);
    Yaml yaml;
    String output;
    Map<String, Object> map = new TreeMap<String, Object>();
    map.put("12345", Collections.singletonList("1111111111"));

    // Split lines enabled (default)
    yaml = new Yaml(options);
    output = yaml.dump(map);
    assertEquals("{\"12345\": [\n    \"1111111111\"]}\n", output);

    // Split lines disabled
    options.setSplitLines(false);
    assertFalse(options.getSplitLines());
    yaml = new Yaml(options);
    output = yaml.dump(map);
    assertEquals("{\"12345\": [\"1111111111\"]}\n", output);
  }

  public void testWriteIndicatorIndent() {
    DumperOptions options = new DumperOptions();
    options.setIndent(5);
    options.setIndicatorIndent(2);
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    List<?> topLevel =
        Arrays.asList(Collections.singletonMap("k1", "v1"), Collections.singletonMap("k2", "v2"));
    Map<String, ?> map = Collections.singletonMap("aaa", topLevel);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon = "aaa:\n  -  k1: v1\n  -  k2: v2\n";
    assertEquals(etalon, output);
  }

  public void testSplitLineExpectFlowSequenceItem() {

    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    options.setWidth(8);
    Yaml yaml;
    String output;

    // Split lines enabled (default)
    yaml = new Yaml(options);
    output = yaml.dump(Arrays.asList("1111111111", "2222222222"));
    assertEquals("[\"1111111111\",\n  \"2222222222\"]\n", output);
    output = yaml.dump(Arrays.asList("1", "2"));
    assertEquals("[\"1\", \"2\"]\n", output);

    // Split lines disabled
    options.setSplitLines(false);
    assertFalse(options.getSplitLines());
    yaml = new Yaml(options);
    output = yaml.dump(Arrays.asList("1111111111", "2222222222"));
    assertEquals("[\"1111111111\", \"2222222222\"]\n", output);
    output = yaml.dump(Arrays.asList("1", "2"));
    assertEquals("[\"1\", \"2\"]\n", output);
  }

  public void testSplitLineExpectFirstFlowMappingKey() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    options.setWidth(16);
    Yaml yaml;
    String output;
    Map<String, String> nonSplitMap = new TreeMap<String, String>();
    nonSplitMap.put("3", "4");
    Map<String, Map<String, String>> nonSplitContainerMap =
        new TreeMap<String, Map<String, String>>();
    nonSplitContainerMap.put("1 2", nonSplitMap);
    Map<String, String> splitMap = new TreeMap<String, String>();
    splitMap.put("3333333333", "4444444444");
    Map<String, Map<String, String>> splitContainerMap = new TreeMap<String, Map<String, String>>();
    splitContainerMap.put("1111111111 2222222222", splitMap);

    // Split lines enabled (default)
    yaml = new Yaml(options);
    output = yaml.dump(splitContainerMap);
    assertEquals("{\"1111111111 2222222222\": {\n    \"3333333333\": \"4444444444\"}}\n", output);
    output = yaml.dump(nonSplitContainerMap);
    assertEquals("{\"1 2\": {\"3\": \"4\"}}\n", output);

    // Split lines disabled
    options.setSplitLines(false);
    assertFalse(options.getSplitLines());
    yaml = new Yaml(options);
    output = yaml.dump(splitContainerMap);
    assertEquals("{\"1111111111 2222222222\": {\"3333333333\": \"4444444444\"}}\n", output);
    output = yaml.dump(nonSplitContainerMap);
    assertEquals("{\"1 2\": {\"3\": \"4\"}}\n", output);
  }

  public void testSplitLineExpectFlowMappingKey() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    options.setWidth(16);
    Yaml yaml;
    String output;
    Map<String, String> nonSplitMap = new TreeMap<String, String>();
    nonSplitMap.put("1", "2");
    nonSplitMap.put("3", "4");
    Map<String, String> splitMap = new TreeMap<String, String>();
    splitMap.put("1111111111", "2222222222");
    splitMap.put("3333333333", "4444444444");

    // Split lines enabled (default)
    yaml = new Yaml(options);
    output = yaml.dump(splitMap);
    assertEquals("{\"1111111111\": \"2222222222\",\n  \"3333333333\": \"4444444444\"}\n", output);
    output = yaml.dump(nonSplitMap);
    assertEquals("{\"1\": \"2\", \"3\": \"4\"}\n", output);

    // Split lines disabled
    options.setSplitLines(false);
    assertFalse(options.getSplitLines());
    yaml = new Yaml(options);
    output = yaml.dump(splitMap);
    assertEquals("{\"1111111111\": \"2222222222\", \"3333333333\": \"4444444444\"}\n", output);
    output = yaml.dump(nonSplitMap);
    assertEquals("{\"1\": \"2\", \"3\": \"4\"}\n", output);
  }

  public void testAnchors() {
    assertEquals("a", Emitter.prepareAnchor("a"));
    assertEquals("Anchor may not contain spaces: a ", checkAnchor("a "));
    assertEquals("Anchor may not contain spaces: a \t", checkAnchor("a \t"));
    assertEquals("Invalid character '[' in the anchor: a[", checkAnchor("a["));
    assertEquals("Invalid character ']' in the anchor: a]", checkAnchor("a]"));
    assertEquals("Invalid character '{' in the anchor: {a", checkAnchor("{a"));
    assertEquals("Invalid character '}' in the anchor: }a", checkAnchor("}a"));
    assertEquals("Invalid character ',' in the anchor: a,b", checkAnchor("a,b"));
    assertEquals("Invalid character '*' in the anchor: a*b", checkAnchor("a*b"));
    assertEquals("Invalid character '&' in the anchor: a&b", checkAnchor("a&b"));
  }

  private String checkAnchor(String anchor) {
    try {
      Emitter.prepareAnchor(anchor);
      throw new IllegalStateException("Invalid must not be accepted");
    } catch (Exception e) {
      return e.getMessage();
    }
  }
}