1 28 package org.jvyamlb; 29 30 import java.util.Map ; 31 import java.util.HashMap ; 32 33 import junit.framework.TestCase; 34 35 import org.jruby.util.ByteList; 36 37 40 public class YAMLLoadTest extends TestCase { 41 public YAMLLoadTest(final String name) { 42 super(name); 43 } 44 45 private static ByteList s(String st) { 46 return new ByteList(st.getBytes()); 47 } 48 49 public void testBasicStringScalarLoad() throws Exception { 50 ByteList str = s("str"); 51 assertEquals(str,YAML.load(s("--- str"))); 52 assertEquals(str,YAML.load(s("---\nstr"))); 53 assertEquals(str,YAML.load(s("--- \nstr"))); 54 assertEquals(str,YAML.load(s("--- \n str"))); 55 assertEquals(str,YAML.load(s("str"))); 56 assertEquals(str,YAML.load(s(" str"))); 57 assertEquals(str,YAML.load(s("\nstr"))); 58 assertEquals(str,YAML.load(s("\n str"))); 59 assertEquals(str,YAML.load(s("\"str\""))); 60 assertEquals(str,YAML.load(s("'str'"))); 61 assertEquals(s("ΓΌ"),YAML.load(s("---\n\"\\xC3\\xBC\""))); 62 } 63 64 public void testBasicIntegerScalarLoad() { 65 assertEquals(new Long (47),YAML.load(s("47"))); 66 assertEquals(new Long (0),YAML.load(s("0"))); 67 assertEquals(new Long (-1),YAML.load(s("-1"))); 68 } 69 70 public void testBlockMappingLoad() { 71 Map expected = new HashMap (); 72 expected.put(s("a"),s("b")); 73 expected.put(s("c"),s("d")); 74 assertEquals(expected,YAML.load(s("a: b\nc: d"))); 75 assertEquals(expected,YAML.load(s("c: d\na: b\n"))); 76 } 77 78 public void testFlowMappingLoad() { 79 Map expected = new HashMap (); 80 expected.put(s("a"),s("b")); 81 expected.put(s("c"),s("d")); 82 assertEquals(expected,YAML.load(s("{a: b, c: d}"))); 83 assertEquals(expected,YAML.load(s("{c: d,\na: b}"))); 84 } 85 86 public void testInternalChar() { 87 Map expected = new HashMap (); 88 expected.put(s("bad_sample"),s("something:(")); 89 assertEquals(expected,YAML.load(s("--- \nbad_sample: something:(\n"))); 90 } 91 92 public void testBuiltinTag() { 93 assertEquals(s("str"),YAML.load(s("!!str str"))); 94 assertEquals(s("str"),YAML.load(s("%YAML 1.1\n---\n!!str str"))); 95 assertEquals(s("str"),YAML.load(s("%YAML 1.0\n---\n!str str"))); 96 assertEquals(s("str"),YAML.load(s("---\n!str str"),YAML.config().version("1.0"))); 97 assertEquals(new Long (123),YAML.load(s("---\n!int 123"),YAML.config().version("1.0"))); 98 assertEquals(new Long (123),YAML.load(s("%YAML 1.1\n---\n!!int 123"),YAML.config().version("1.0"))); 99 } 100 101 public void testDirectives() { 102 assertEquals(s("str"),YAML.load(s("%YAML 1.1\n--- !!str str"))); 103 assertEquals(s("str"),YAML.load(s("%YAML 1.1\n%TAG !yaml! tag:yaml.org,2002:\n--- !yaml!str str"))); 104 try { 105 YAML.load(s("%YAML 1.1\n%YAML 1.1\n--- !!str str")); 106 fail("should throw exception when repeating directive"); 107 } catch(final ParserException e) { 108 assertTrue(true); 109 } 110 } 111 112 public void testJavaBeanLoad() { 113 final java.util.Calendar cal = java.util.Calendar.getInstance(); 114 cal.clear(); 115 cal.set(1982,5-1,3); 117 final TestBean expected = new TestBean(s("Ola Bini"), 24, cal.getTime()); 118 assertEquals(expected, YAML.load(s("--- !java/object:org.jvyamlb.TestBean\nname: Ola Bini\nage: 24\nborn: 1982-05-03\n"))); 119 } 120 } | Popular Tags |