• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.yaml.snakeyaml.issues.issue176;
15 
16 import java.util.LinkedHashMap;
17 import java.util.Map;
18 import junit.framework.TestCase;
19 import org.yaml.snakeyaml.DumperOptions;
20 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.constructor.SafeConstructor;
23 import org.yaml.snakeyaml.representer.Representer;
24 
25 public class SingleQuoteTest extends TestCase {
26 
testNoSingleQuoteForBlockStyle()27   public void testNoSingleQuoteForBlockStyle() throws Exception {
28     checkQuotes(true, "cows:\n    steak:cow: '11'");
29   }
30 
testSingleQuoteForFlowStyle()31   public void testSingleQuoteForFlowStyle() throws Exception {
32     checkQuotes(false, "cows: {'steak:cow': '11'}");
33   }
34 
checkQuotes(boolean isBlock, String expectation)35   private void checkQuotes(boolean isBlock, String expectation) {
36     DumperOptions options = new DumperOptions();
37     options.setIndent(4);
38     if (isBlock) {
39       options.setDefaultFlowStyle(FlowStyle.BLOCK);
40     }
41     Representer representer = new Representer();
42 
43     Yaml yaml = new Yaml(new SafeConstructor(), representer, options);
44 
45     LinkedHashMap<String, Object> lvl1 = new LinkedHashMap<String, Object>();
46     lvl1.put("steak:cow", "11");
47     LinkedHashMap<String, Object> root = new LinkedHashMap<String, Object>();
48     root.put("cows", lvl1);
49     String output = yaml.dump(root);
50     assertEquals(expectation + "\n", output);
51 
52     // parse the value back
53     @SuppressWarnings("unchecked")
54     Map<String, Object> cows = yaml.load(output);
55     @SuppressWarnings("unchecked")
56     Map<String, String> cow = (Map<String, String>) cows.get("cows");
57     assertEquals("11", cow.get("steak:cow"));
58   }
59 }
60