• 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;
15 
16 import java.util.List;
17 import java.util.Map;
18 import junit.framework.TestCase;
19 
20 /**
21  * Test Chapter 2.1 from the YAML specification
22  */
23 public class Chapter2_1Test extends TestCase {
24 
25   @SuppressWarnings("unchecked")
testExample_2_1()26   public void testExample_2_1() {
27     YamlDocument document = new YamlDocument("example2_1.yaml");
28     List<String> list = (List<String>) document.getNativeData();
29     assertEquals(3, list.size());
30     assertEquals("Mark McGwire", list.get(0));
31     assertEquals("Sammy Sosa", list.get(1));
32     assertEquals("Ken Griffey", list.get(2));
33     assertEquals("[Mark McGwire, Sammy Sosa, Ken Griffey]\n", document.getPresentation());
34   }
35 
36   @SuppressWarnings("unchecked")
testExample_2_2()37   public void testExample_2_2() {
38     YamlDocument document = new YamlDocument("example2_2.yaml");
39     Map<String, Object> map = (Map<String, Object>) document.getNativeData();
40     assertEquals(3, map.size());
41     assertEquals("Expect 65 to be a Integer", Integer.class, map.get("hr").getClass());
42     assertEquals(Integer.valueOf(65), map.get("hr"));
43     assertEquals(Float.valueOf(0.278f), Float.valueOf("0.278"));
44     assertEquals("Expect 0.278 to be a Float", Double.class, map.get("avg").getClass());
45     assertEquals(Double.valueOf(0.278), map.get("avg"));
46     assertEquals("Expect 147 to be an Integer", Integer.class, map.get("rbi").getClass());
47     assertEquals(Integer.valueOf(147), map.get("rbi"));
48   }
49 
50   @SuppressWarnings("unchecked")
testExample_2_3()51   public void testExample_2_3() {
52     YamlDocument document = new YamlDocument("example2_3.yaml");
53     Map<String, List<String>> map = (Map<String, List<String>>) document.getNativeData();
54     assertEquals(2, map.size());
55     List<String> list1 = map.get("american");
56     assertEquals(3, list1.size());
57     assertEquals("Boston Red Sox", list1.get(0));
58     assertEquals("Detroit Tigers", list1.get(1));
59     assertEquals("New York Yankees", list1.get(2));
60     List<String> list2 = map.get("national");
61     assertEquals(3, list2.size());
62     assertEquals("New York Mets", list2.get(0));
63     assertEquals("Chicago Cubs", list2.get(1));
64     assertEquals("Atlanta Braves", list2.get(2));
65   }
66 
67   @SuppressWarnings("unchecked")
testExample_2_4()68   public void testExample_2_4() {
69     YamlDocument document = new YamlDocument("example2_4.yaml");
70     List<Map<String, Object>> list = (List<Map<String, Object>>) document.getNativeData();
71     assertEquals(2, list.size());
72     Map<String, Object> map1 = list.get(0);
73     assertEquals(3, map1.size());
74     assertEquals("Mark McGwire", map1.get("name"));
75   }
76 
77   @SuppressWarnings("unchecked")
testExample_2_5()78   public void testExample_2_5() {
79     YamlDocument document = new YamlDocument("example2_5.yaml");
80     List<List<Object>> list = (List<List<Object>>) document.getNativeData();
81     assertEquals(3, list.size());
82     List<Object> list1 = list.get(0);
83     assertEquals(3, list1.size());
84     assertEquals("name", list1.get(0));
85     assertEquals("hr", list1.get(1));
86     assertEquals("avg", list1.get(2));
87     assertEquals(3, list.get(1).size());
88     assertEquals(3, list.get(2).size());
89   }
90 
91   @SuppressWarnings("unchecked")
testExample_2_6()92   public void testExample_2_6() {
93     YamlDocument document = new YamlDocument("example2_6.yaml");
94     Map<String, Map<String, Object>> map =
95         (Map<String, Map<String, Object>>) document.getNativeData();
96     assertEquals(2, map.size());
97     Map<String, Object> map1 = map.get("Mark McGwire");
98     assertEquals(2, map1.size());
99     Map<String, Object> map2 = map.get("Sammy Sosa");
100     assertEquals(2, map2.size());
101   }
102 }
103