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