1 /** 2 * Copyright (c) 2008, 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.Arrays; 21 import java.util.Collections; 22 import java.util.LinkedHashMap; 23 import java.util.List; 24 import java.util.Map; 25 import java.util.TreeMap; 26 27 import junit.framework.TestCase; 28 29 import org.yaml.snakeyaml.DumperOptions; 30 import org.yaml.snakeyaml.Yaml; 31 import org.yaml.snakeyaml.DumperOptions.FlowStyle; 32 import org.yaml.snakeyaml.DumperOptions.ScalarStyle; 33 import org.yaml.snakeyaml.events.DocumentStartEvent; 34 import org.yaml.snakeyaml.events.ImplicitTuple; 35 import org.yaml.snakeyaml.events.ScalarEvent; 36 import org.yaml.snakeyaml.events.StreamStartEvent; 37 38 public class EmitterTest extends TestCase { 39 40 public void testWriteFolded() { 41 DumperOptions options = new DumperOptions(); 42 options.setDefaultScalarStyle(ScalarStyle.FOLDED); 43 String folded = "0123456789 0123456789\n0123456789 0123456789"; 44 Map<String, String> map = new LinkedHashMap<String, String>(); 45 map.put("aaa", folded); 46 map.put("bbb", "\nbla-bla\n"); 47 Yaml yaml = new Yaml(options); 48 String output = yaml.dump(map); 49 String etalon = "\"aaa\": >-\n 0123456789 0123456789\n\n 0123456789 0123456789\n\"bbb\": >2\n\n bla-bla\n"; 50 assertEquals(etalon, output); 51 } 52 53 public void testWriteLiteral() { 54 DumperOptions options = new DumperOptions(); 55 options.setDefaultScalarStyle(ScalarStyle.LITERAL); 56 String folded = "0123456789 0123456789 0123456789 0123456789"; 57 Map<String, String> map = new LinkedHashMap<String, String>(); 58 map.put("aaa", folded); 59 map.put("bbb", "\nbla-bla\n"); 60 Yaml yaml = new Yaml(options); 61 String output = yaml.dump(map); 62 String etalon = "\"aaa\": |-\n 0123456789 0123456789 0123456789 0123456789\n\"bbb\": |2\n\n bla-bla\n"; 63 assertEquals(etalon, output); 64 } 65 66 public void testWritePlain() { 67 DumperOptions options = new DumperOptions(); 68 options.setDefaultScalarStyle(ScalarStyle.PLAIN); 69 String folded = "0123456789 0123456789\n0123456789 0123456789"; 70 Map<String, String> map = new LinkedHashMap<String, String>(); 71 map.put("aaa", folded); 72 map.put("bbb", "\nbla-bla"); 73 Yaml yaml = new Yaml(options); 74 String output = yaml.dump(map); 75 String etalon = "aaa: |-\n 0123456789 0123456789\n 0123456789 0123456789\nbbb: |2-\n\n bla-bla\n"; 76 assertEquals(etalon, output); 77 } 78 79 public void testWritePlainPretty() { 80 DumperOptions options = new DumperOptions(); 81 options.setDefaultScalarStyle(ScalarStyle.PLAIN); 82 options.setPrettyFlow(true); 83 84 String folded = "0123456789 0123456789\n0123456789 0123456789"; 85 Map<String, String> map = new LinkedHashMap<String, String>(); 86 map.put("aaa", folded); 87 map.put("bbb", "\nbla-bla"); 88 89 Yaml yaml = new Yaml(options); 90 String output = yaml.dump(map); 91 String etalon = "aaa: |-\n 0123456789 0123456789\n 0123456789 0123456789\nbbb: |2-\n\n bla-bla\n"; 92 assertEquals(etalon, output); 93 } 94 95 public void testWriteSingleQuoted() { 96 DumperOptions options = new DumperOptions(); 97 options.setDefaultScalarStyle(ScalarStyle.SINGLE_QUOTED); 98 String folded = "0123456789 0123456789\n0123456789 0123456789"; 99 Map<String, String> map = new LinkedHashMap<String, String>(); 100 map.put("aaa", folded); 101 map.put("bbb", "\nbla-bla"); 102 Yaml yaml = new Yaml(options); 103 String output = yaml.dump(map); 104 String etalon = "'aaa': '0123456789 0123456789\n\n 0123456789 0123456789'\n'bbb': '\n\n bla-bla'\n"; 105 assertEquals(etalon, output); 106 } 107 108 public void testWriteDoubleQuoted() { 109 DumperOptions options = new DumperOptions(); 110 options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED); 111 String folded = "0123456789 0123456789\n0123456789 0123456789"; 112 Map<String, String> map = new LinkedHashMap<String, String>(); 113 map.put("aaa", folded); 114 map.put("bbb", "\nbla-bla"); 115 Yaml yaml = new Yaml(options); 116 String output = yaml.dump(map); 117 String etalon = "\"aaa\": \"0123456789 0123456789\\n0123456789 0123456789\"\n\"bbb\": \"\\nbla-bla\"\n"; 118 assertEquals(etalon, output); 119 } 120 121 // Issue #158 122 public void testWriteSupplementaryUnicode() throws IOException { 123 DumperOptions options = new DumperOptions(); 124 String burger = new String(Character.toChars(0x1f354)); 125 String halfBurger = "\uD83C"; 126 StringWriter output = new StringWriter(); 127 Emitter emitter = new Emitter(output, options); 128 129 emitter.emit(new StreamStartEvent(null, null)); 130 emitter.emit(new DocumentStartEvent(null, null, false, null, null)); 131 emitter.emit(new ScalarEvent(null, null, new ImplicitTuple(true, false), burger 132 + halfBurger, null, null, '"')); 133 String expected = "! \"\\U0001f354\\ud83c\""; 134 assertEquals(expected, output.toString()); 135 } 136 137 public void testSplitLineExpectFirstFlowSequenceItem() { 138 139 DumperOptions options = new DumperOptions(); 140 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); 141 options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); 142 options.setWidth(8); 143 Yaml yaml; 144 String output; 145 Map<String, Object> map = new TreeMap<String, Object>(); 146 map.put("12345", Arrays.asList("1111111111")); 147 148 // Split lines enabled (default) 149 yaml = new Yaml(options); 150 output = yaml.dump(map); 151 assertEquals("{\"12345\": [\n \"1111111111\"]}\n", output); 152 153 // Split lines disabled 154 options.setSplitLines(false); 155 assertFalse(options.getSplitLines()); 156 yaml = new Yaml(options); 157 output = yaml.dump(map); 158 assertEquals("{\"12345\": [\"1111111111\"]}\n", output); 159 } 160 161 public void testWriteIndicatorIndent() { 162 DumperOptions options = new DumperOptions(); 163 options.setIndent(5); 164 options.setIndicatorIndent(2); 165 options.setDefaultFlowStyle(FlowStyle.BLOCK); 166 List<?> topLevel = Arrays.asList(Collections.singletonMap("k1", "v1"), Collections.singletonMap("k2", "v2")); 167 Map<String, ?> map = Collections.singletonMap("aaa", topLevel); 168 Yaml yaml = new Yaml(options); 169 String output = yaml.dump(map); 170 String etalon = "aaa:\n - k1: v1\n - k2: v2\n"; 171 assertEquals(etalon, output); 172 } 173 174 public void testSplitLineExpectFlowSequenceItem() { 175 176 DumperOptions options = new DumperOptions(); 177 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); 178 options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); 179 options.setWidth(8); 180 Yaml yaml; 181 String output; 182 183 // Split lines enabled (default) 184 yaml = new Yaml(options); 185 output = yaml.dump(Arrays.asList("1111111111", "2222222222")); 186 assertEquals("[\"1111111111\",\n \"2222222222\"]\n", output); 187 output = yaml.dump(Arrays.asList("1", "2")); 188 assertEquals("[\"1\", \"2\"]\n", output); 189 190 // Split lines disabled 191 options.setSplitLines(false); 192 assertFalse(options.getSplitLines()); 193 yaml = new Yaml(options); 194 output = yaml.dump(Arrays.asList("1111111111", "2222222222")); 195 assertEquals("[\"1111111111\", \"2222222222\"]\n", output); 196 output = yaml.dump(Arrays.asList("1", "2")); 197 assertEquals("[\"1\", \"2\"]\n", output); 198 } 199 200 public void testSplitLineExpectFirstFlowMappingKey() { 201 DumperOptions options = new DumperOptions(); 202 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); 203 options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); 204 options.setWidth(16); 205 Yaml yaml; 206 String output; 207 Map<String, String> nonSplitMap = new TreeMap<String, String>(); 208 nonSplitMap.put("3", "4"); 209 Map<String, Map<String, String>> nonSplitContainerMap = new TreeMap<String, Map<String, String>>(); 210 nonSplitContainerMap.put("1 2", nonSplitMap); 211 Map<String, String> splitMap = new TreeMap<String, String>(); 212 splitMap.put("3333333333", "4444444444"); 213 Map<String, Map<String, String>> splitContainerMap = new TreeMap<String, Map<String, String>>(); 214 splitContainerMap.put("1111111111 2222222222", splitMap); 215 216 // Split lines enabled (default) 217 yaml = new Yaml(options); 218 output = yaml.dump(splitContainerMap); 219 assertEquals("{\"1111111111 2222222222\": {\n \"3333333333\": \"4444444444\"}}\n", output); 220 output = yaml.dump(nonSplitContainerMap); 221 assertEquals("{\"1 2\": {\"3\": \"4\"}}\n", output); 222 223 // Split lines disabled 224 options.setSplitLines(false); 225 assertFalse(options.getSplitLines()); 226 yaml = new Yaml(options); 227 output = yaml.dump(splitContainerMap); 228 assertEquals("{\"1111111111 2222222222\": {\"3333333333\": \"4444444444\"}}\n", output); 229 output = yaml.dump(nonSplitContainerMap); 230 assertEquals("{\"1 2\": {\"3\": \"4\"}}\n", output); 231 } 232 233 public void testSplitLineExpectFlowMappingKey() { 234 DumperOptions options = new DumperOptions(); 235 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); 236 options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW); 237 options.setWidth(16); 238 Yaml yaml; 239 String output; 240 Map<String, String> nonSplitMap = new TreeMap<String, String>(); 241 nonSplitMap.put("1", "2"); 242 nonSplitMap.put("3", "4"); 243 Map<String, String> splitMap = new TreeMap<String, String>(); 244 splitMap.put("1111111111", "2222222222"); 245 splitMap.put("3333333333", "4444444444"); 246 247 // Split lines enabled (default) 248 yaml = new Yaml(options); 249 output = yaml.dump(splitMap); 250 assertEquals("{\"1111111111\": \"2222222222\",\n \"3333333333\": \"4444444444\"}\n", output); 251 output = yaml.dump(nonSplitMap); 252 assertEquals("{\"1\": \"2\", \"3\": \"4\"}\n", output); 253 254 // Split lines disabled 255 options.setSplitLines(false); 256 assertFalse(options.getSplitLines()); 257 yaml = new Yaml(options); 258 output = yaml.dump(splitMap); 259 assertEquals("{\"1111111111\": \"2222222222\", \"3333333333\": \"4444444444\"}\n", output); 260 output = yaml.dump(nonSplitMap); 261 assertEquals("{\"1\": \"2\", \"3\": \"4\"}\n", output); 262 } 263 } 264