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.IOException;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.yaml.snakeyaml.DumperOptions;
24 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.nodes.Node;
27 import org.yaml.snakeyaml.nodes.Tag;
28 import org.yaml.snakeyaml.representer.Represent;
29 import org.yaml.snakeyaml.representer.Representer;
30
31
32
33
34 public class BoolTagTest extends AbstractTest {
35 public void testBool() throws IOException {
36 assertEquals(Boolean.TRUE, getMapValue("canonical: true", "canonical"));
37 assertEquals(Boolean.FALSE, getMapValue("answer: NO", "answer"));
38 assertEquals(Boolean.TRUE, getMapValue("logical: True", "logical"));
39 assertEquals(Boolean.TRUE, getMapValue("option: on", "option"));
40 }
41
42 public void testBoolCanonical() throws IOException {
43 assertEquals(Boolean.TRUE, getMapValue("canonical: Yes", "canonical"));
44 assertEquals(Boolean.TRUE, getMapValue("canonical: yes", "canonical"));
45 assertEquals(Boolean.TRUE, getMapValue("canonical: YES", "canonical"));
46 assertEquals("yES", getMapValue("canonical: yES", "canonical"));
47 assertEquals(Boolean.FALSE, getMapValue("canonical: No", "canonical"));
48 assertEquals(Boolean.FALSE, getMapValue("canonical: NO", "canonical"));
49 assertEquals(Boolean.FALSE, getMapValue("canonical: no", "canonical"));
50 assertEquals(Boolean.FALSE, getMapValue("canonical: off", "canonical"));
51 assertEquals(Boolean.FALSE, getMapValue("canonical: Off", "canonical"));
52 assertEquals(Boolean.FALSE, getMapValue("canonical: OFF", "canonical"));
53 assertEquals(Boolean.TRUE, getMapValue("canonical: ON", "canonical"));
54 assertEquals(Boolean.TRUE, getMapValue("canonical: On", "canonical"));
55 assertEquals(Boolean.TRUE, getMapValue("canonical: on", "canonical"));
56
57
58 assertEquals("n", getMapValue("canonical: n", "canonical"));
59 assertEquals("N", getMapValue("canonical: N", "canonical"));
60 assertEquals("y", getMapValue("canonical: y", "canonical"));
61 assertEquals("Y", getMapValue("canonical: Y", "canonical"));
62 }
63
64 public void testBoolShorthand() throws IOException {
65 assertEquals(Boolean.TRUE, getMapValue("boolean: !!bool true", "boolean"));
66 }
67
68 public void testBoolTag() throws IOException {
69 assertEquals(Boolean.TRUE,
70 getMapValue("boolean: !<tag:yaml.org,2002:bool> true", "boolean"));
71 }
72
73 public void testBoolOut() throws IOException {
74 Map<String, Boolean> map = new HashMap<String, Boolean>();
75 map.put("boolean", Boolean.TRUE);
76 String output = dump(map);
77 assertTrue(output, output.contains("boolean: true"));
78 }
79
80 public void testBoolOutAsYes() throws IOException {
81 Yaml yaml = new Yaml(new BoolRepresenter("YES"));
82 String output = yaml.dump(true);
83 assertEquals("YES\n", output);
84 }
85
86
87
88
89 public void testBoolOutAsEmpty2() throws IOException {
90 Yaml yaml = new Yaml(new BoolRepresenter("on"));
91 Map<String, Boolean> map = new HashMap<String, Boolean>();
92 map.put("aaa", false);
93 map.put("bbb", true);
94 String output = yaml.dump(map);
95 assertEquals("{aaa: false, bbb: on}\n", output);
96 }
97
98
99
100
101 public void testBoolOutAsEmpty3() throws IOException {
102 DumperOptions options = new DumperOptions();
103 options.setDefaultFlowStyle(FlowStyle.BLOCK);
104 Yaml yaml = new Yaml(new BoolRepresenter("True"), options);
105 Map<String, Boolean> map = new HashMap<String, Boolean>();
106 map.put("aaa", false);
107 map.put("bbb", true);
108 String output = yaml.dump(map);
109 assertEquals("aaa: false\nbbb: True\n", output);
110 }
111
112 private class BoolRepresenter extends Representer {
113 private String value;
114
115 public BoolRepresenter(String value) {
116 super();
117 this.value = value;
118 this.representers.put(Boolean.class, new RepresentBool());
119 }
120
121
122
123 private class RepresentBool implements Represent {
124 public Node representData(Object data) {
125 String v;
126 if (Boolean.TRUE.equals(data)) {
127 v = value;
128 } else {
129 v = "false";
130 }
131 return representScalar(Tag.BOOL, v);
132 }
133 }
134 }
135 }