1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.types;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStreamWriter;
22 import java.io.Writer;
23 import java.nio.charset.Charset;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.yaml.snakeyaml.DumperOptions;
30 import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
31 import org.yaml.snakeyaml.Yaml;
32
33
34
35
36 public class StrTagTest extends AbstractTest {
37 private String getData(String data, String key) {
38 return (String) getMapValue(data, key);
39 }
40
41 public void testString() throws IOException {
42 assertEquals("abcd", getData("string: abcd", "string"));
43 }
44
45 public void testStringShorthand() throws IOException {
46 assertEquals("abcd", getData("string: !!str abcd", "string"));
47 }
48
49 public void testStringTag() throws IOException {
50 assertEquals("abcd", getData("string: !<tag:yaml.org,2002:str> abcd", "string"));
51 }
52
53 public void testUnicode() throws IOException {
54
55 assertEquals("\u00fc", load("\"\\xfc\""));
56 assertEquals("\\xfc", load("\\xfc"));
57
58
59 assertEquals("\u00fc\u00e8", load("\"\\xfc\\xe8\""));
60
61
62 assertEquals("\u2014", load("\"\\u2014\""));
63
64
65 assertEquals("\\U2014AAAA", load("'\\U2014AAAA'"));
66
67
68
69 assertEquals("\u2014", load("\u2014"));
70 }
71
72
73
74
75 @SuppressWarnings("unchecked")
76 public void testIssueId6() {
77 Map<String, String> map = (Map<String, String>) load("---\ntest: |-\n \"Test\r\r (* error here)\"");
78 assertEquals("\"Test\n\n(* error here)\"", map.get("test"));
79 }
80
81 public void testStrDump() {
82 assertEquals("abc\n", dump("abc"));
83 }
84
85 public void testUnicodeDump() {
86 assertEquals("\\u263a\n", dump("\\u263a"));
87 assertEquals("The leading zero must be preserved.", "\\u063a\n", dump("\\u063a"));
88 }
89
90 public void testStringIntOut() {
91 Map<String, String> map = new HashMap<String, String>();
92 map.put("number", "1");
93 String output = dump(map);
94 assertTrue(output, output.contains("number: '1'"));
95 }
96
97 public void testStringFloatOut() {
98 Map<String, String> map = new HashMap<String, String>();
99 map.put("number", "1.1");
100 String output = dump(map);
101 assertTrue(output, output.contains("number: '1.1'"));
102 }
103
104 public void testStringBoolOut() {
105 Map<String, String> map = new HashMap<String, String>();
106 map.put("number", "True");
107 String output = dump(map);
108 assertTrue(output, output.contains("number: 'True'"));
109 }
110
111 public void testEmitLongString() throws IOException {
112 String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
113 String output = dump(str);
114 assertEquals(str + "\n", output);
115 }
116
117 public void testEmitLongStringWithCR() throws IOException {
118 String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n";
119 String output = dump(str);
120 assertEquals("'" + str + "\n '\n", output);
121 }
122
123 public void testEmitDoubleQuoted() throws IOException {
124 String str = "\"xx\"";
125 String output = dump(str);
126 assertEquals("'" + str + "'\n", output);
127 }
128
129
130
131
132 public void testEmitQuoted() throws IOException {
133 List<String> list = new ArrayList<String>(3);
134 list.add("This is an 'example'.");
135 list.add("This is an \"example\".");
136 list.add("123");
137 String output = dump(list);
138 assertEquals("[This is an 'example'., This is an \"example\"., '123']\n", output);
139
140 DumperOptions options = new DumperOptions();
141 options.setDefaultScalarStyle(ScalarStyle.SINGLE_QUOTED);
142 Yaml yaml = new Yaml(options);
143 String output2 = yaml.dump(list);
144
145 assertEquals("- 'This is an ''example''.'\n- 'This is an \"example\".'\n- '123'\n", output2);
146
147 DumperOptions options2 = new DumperOptions();
148 options2.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
149 yaml = new Yaml(options2);
150 String output3 = yaml.dump(list);
151
152 assertEquals("- \"This is an 'example'.\"\n- \"This is an \\\"example\\\".\"\n- \"123\"\n",
153 output3);
154 }
155
156 public void testEmitEndOfLine() throws IOException {
157 String str = "xxxxxxx\n";
158 String output = dump(str);
159 assertEquals("'" + str + "\n '\n", output);
160 }
161
162 public void testDumpUtf16() throws IOException {
163 String str = "xxx";
164 assertEquals(3, str.toString().length());
165 Yaml yaml = new Yaml();
166 Charset charset = Charset.forName("UTF-16");
167 ByteArrayOutputStream stream = new ByteArrayOutputStream();
168 Writer writer = new OutputStreamWriter(stream, charset);
169 yaml.dump(str, writer);
170 assertEquals(str + "\n", stream.toString("UTF-16"));
171 assertEquals("Must include BOM: " + stream.toString(), (1 + 3 + 1) * 2, stream.toString()
172 .length());
173 }
174 }