1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml.representer;
17
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Pattern;
26
27 import junit.framework.TestCase;
28
29 import org.yaml.snakeyaml.DumperOptions;
30 import org.yaml.snakeyaml.Yaml;
31 import org.yaml.snakeyaml.reader.StreamReader;
32
33 public class SafeRepresenterTest extends TestCase {
34
35 public void testBinaryPattern() {
36 Pattern pattern = StreamReader.NON_PRINTABLE;
37 assertFalse(pattern.matcher("\tAndrey\r\n").find());
38 assertTrue(pattern.matcher("\u0005Andrey").find());
39 }
40
41 public void testFloat() {
42 assertEquals("1.0E12", String.valueOf(new Double("1e12")));
43 }
44
45 public void testNumber() {
46 List<Number> list = new ArrayList<Number>();
47 list.add(new Byte((byte) 3));
48 list.add(new Short((short) 4));
49 list.add(new Integer(5));
50 list.add(new BigInteger("6"));
51 list.add(new Long(7L));
52 list.add(Double.POSITIVE_INFINITY);
53 list.add(Double.NEGATIVE_INFINITY);
54 list.add(Double.NaN);
55 Yaml yaml = new Yaml();
56 String output = yaml.dump(list);
57 assertEquals("[3, 4, 5, 6, 7, .inf, -.inf, .NaN]\n", output);
58 }
59
60 public void testDate() {
61 List<Date> list = new ArrayList<Date>();
62 list.add(new Date(1229684761159L));
63 list.add(new Date(1229684761059L));
64 list.add(new Date(1229684761009L));
65 list.add(new Date(1229684761150L));
66 list.add(new Date(1229684761100L));
67 list.add(new Date(1229684761000L));
68 list.add(new Date(1229684760000L));
69 DumperOptions options = new DumperOptions();
70 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
71 Yaml yaml = new Yaml(options);
72 String output = yaml.dump(list);
73 assertEquals(
74 "- 2008-12-19T11:06:01.159Z\n- 2008-12-19T11:06:01.059Z\n- 2008-12-19T11:06:01.009Z\n- 2008-12-19T11:06:01.150Z\n- 2008-12-19T11:06:01.100Z\n- 2008-12-19T11:06:01Z\n- 2008-12-19T11:06:00Z\n",
75 output);
76 }
77
78 public void testEmptyArray() {
79 Yaml yaml = new Yaml();
80 String output = yaml.dump(new String[0]);
81 assertEquals("[]\n", output);
82 }
83
84 public void testStyle() {
85 List<Integer> list = new ArrayList<Integer>();
86 list.add(new Integer(1));
87 list.add(new Integer(1));
88 Map<String, Object> map = new HashMap<String, Object>();
89 map.put("list", list);
90 map.put("name", "Ubuntu");
91 map.put("age", 5);
92 DumperOptions options = new DumperOptions();
93 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
94 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
95 Yaml yaml = new Yaml(options);
96 String output = yaml.dump(map);
97 assertTrue(output.contains("\"age\": !!int \"5\""));
98 assertTrue(output.contains("\"name\": \"Ubuntu\""));
99 assertTrue(output.contains("- !!int \"1\""));
100 }
101
102 public void testStyle2() {
103 List<Integer> list = new ArrayList<Integer>();
104 list.add(new Integer(1));
105 list.add(new Integer(1));
106 Map<String, Object> map = new LinkedHashMap<String, Object>();
107 map.put("age", 5);
108 map.put("name", "Ubuntu");
109 map.put("list", list);
110 DumperOptions options = new DumperOptions();
111 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
112 options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
113 Yaml yaml = new Yaml(options);
114 String output = yaml.dump(map);
115 assertEquals("{'age': !!int '5', 'name': 'Ubuntu', 'list': [!!int '1', !!int '1']}\n",
116 output);
117 }
118
119 public void testStyle2Pretty() {
120 List<Integer> list = new ArrayList<Integer>();
121 list.add(new Integer(1));
122 list.add(new Integer(1));
123 Map<String, Object> map = new LinkedHashMap<String, Object>();
124 map.put("age", 5);
125 map.put("name", "Ubuntu");
126 map.put("list", list);
127 DumperOptions options = new DumperOptions();
128 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
129 options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
130 options.setPrettyFlow(true);
131 Yaml yaml = new Yaml(options);
132 String output = yaml.dump(map);
133 assertEquals(
134 "{\n 'age': !!int '5',\n 'name': 'Ubuntu',\n 'list': [\n !!int '1',\n !!int '1']\n \n}\n",
135 output);
136 }
137 }