View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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  
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   * @see http://yaml.org/type/bool.html
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          // it looks like it is against the specification but it is like in
57          // PyYAML
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       * test flow style
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       * test block style
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         // possible values are here http://yaml.org/type/bool.html
122         // y, Y, n, N should not be used
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 }