1 /** 2 * Copyright (c) 2008, 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; 17 18 import java.io.InputStream; 19 import java.util.Map; 20 21 import junit.framework.TestCase; 22 23 import org.yaml.snakeyaml.DumperOptions.ScalarStyle; 24 25 /** 26 * Test Chapter 2.3 from the YAML specification 27 * 28 * @see <a href="http://yaml.org/spec/1.1/"></a> 29 */ 30 public class Chapter2_3Test extends TestCase { 31 testExample_2_13()32 public void testExample_2_13() { 33 YamlDocument document = new YamlDocument("example2_13.yaml"); 34 String data = (String) document.getNativeData(); 35 assertEquals("\\//||\\/||\n// || ||__\n", data); 36 } 37 testExample_2_14()38 public void testExample_2_14() { 39 YamlDocument document = new YamlDocument("example2_14.yaml"); 40 String data = (String) document.getNativeData(); 41 assertEquals("Mark McGwire's year was crippled by a knee injury.", data); 42 } 43 testExample_2_15()44 public void testExample_2_15() { 45 String etalon = "Sammy Sosa completed another fine season with great stats.\n\n 63 Home Runs\n 0.288 Batting Average\n\nWhat a year!\n"; 46 InputStream input = YamlDocument.class.getClassLoader().getResourceAsStream( 47 YamlDocument.ROOT + "example2_15.yaml"); 48 DumperOptions options = new DumperOptions(); 49 options.setDefaultScalarStyle(ScalarStyle.FOLDED); 50 Yaml yaml = new Yaml(options); 51 String data = (String) yaml.load(input); 52 assertEquals(etalon, data); 53 // 54 String dumped = yaml.dump(data); 55 String etalonDumped = Util.getLocalResource("specification/example2_15_dumped.yaml"); 56 assertEquals(etalonDumped, dumped); 57 } 58 59 @SuppressWarnings("unchecked") testExample_2_16()60 public void testExample_2_16() { 61 YamlDocument document = new YamlDocument("example2_16.yaml"); 62 Map<String, String> map = (Map<String, String>) document.getNativeData(); 63 assertEquals(map.toString(), 3, map.size()); 64 assertEquals("Mark McGwire", map.get("name")); 65 assertEquals("Mark set a major league home run record in 1998.\n", 66 map.get("accomplishment")); 67 assertEquals("65 Home Runs\n0.278 Batting Average\n", map.get("stats")); 68 69 } 70 71 @SuppressWarnings("unchecked") testExample_2_17()72 public void testExample_2_17() { 73 YamlDocument document = new YamlDocument("example2_17.yaml", false); 74 Map<String, String> map = (Map<String, String>) document.getNativeData(); 75 assertEquals(map.toString(), 6, map.size()); 76 assertEquals("Sosa did fine.\u263A", map.get("unicode")); 77 assertEquals("\b1998\t1999\t2000\n", map.get("control")); 78 assertEquals("\r\n is \r\n", map.get("hexesc")); 79 assertEquals("\"Howdy!\" he cried.", map.get("single")); 80 assertEquals(" # not a 'comment'.", map.get("quoted")); 81 assertEquals("|\\-*-/|", map.get("tie-fighter")); 82 } 83 84 @SuppressWarnings("unchecked") testExample_2_17_unicode()85 public void testExample_2_17_unicode() { 86 YamlDocument document = new YamlDocument("example2_17_unicode.yaml"); 87 Map<String, String> map = (Map<String, String>) document.getNativeData(); 88 assertEquals("Sosa did fine.\u263A", map.get("unicode")); 89 } 90 91 @SuppressWarnings("unchecked") testExample_2_17_control()92 public void testExample_2_17_control() { 93 YamlDocument document = new YamlDocument("example2_17_control.yaml", false); 94 Map<String, String> map = (Map<String, String>) document.getNativeData(); 95 assertEquals("\b1998\t1999\t2000\n", map.get("control")); 96 } 97 98 @SuppressWarnings("unchecked") testExample_2_17_hexesc()99 public void testExample_2_17_hexesc() { 100 YamlDocument document = new YamlDocument("example2_17_hexesc.yaml"); 101 Map<String, String> map = (Map<String, String>) document.getNativeData(); 102 assertEquals("\r\n is \r\n", map.get("hexesc")); 103 } 104 105 @SuppressWarnings("unchecked") testExample_2_17_single()106 public void testExample_2_17_single() { 107 YamlDocument document = new YamlDocument("example2_17_single.yaml"); 108 Map<String, String> map = (Map<String, String>) document.getNativeData(); 109 assertEquals("\"Howdy!\" he cried.", map.get("single")); 110 } 111 112 @SuppressWarnings("unchecked") testExample_2_17_quoted()113 public void testExample_2_17_quoted() { 114 YamlDocument document = new YamlDocument("example2_17_quoted.yaml"); 115 Map<String, String> map = (Map<String, String>) document.getNativeData(); 116 assertEquals(" # not a 'comment'.", map.get("quoted")); 117 } 118 119 @SuppressWarnings("unchecked") testExample_2_17_tie_fighter()120 public void testExample_2_17_tie_fighter() { 121 YamlDocument document = new YamlDocument("example2_17_tie_fighter.yaml"); 122 Map<String, String> map = (Map<String, String>) document.getNativeData(); 123 assertEquals("|\\-*-/|", map.get("tie-fighter")); 124 } 125 126 @SuppressWarnings("unchecked") testExample_2_18()127 public void testExample_2_18() { 128 YamlDocument document = new YamlDocument("example2_18.yaml"); 129 Map<String, String> map = (Map<String, String>) document.getNativeData(); 130 assertEquals(map.toString(), 2, map.size()); 131 assertEquals("This unquoted scalar spans many lines.", map.get("plain")); 132 assertEquals("So does this quoted scalar.\n", map.get("quoted")); 133 } 134 } 135