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