1require 'psych/helper' 2 3module Psych 4 module Visitors 5 class TestYAMLTree < TestCase 6 def setup 7 super 8 @v = Visitors::YAMLTree.new 9 end 10 11 def test_tree_can_be_called_twice 12 @v.start 13 @v << Object.new 14 t = @v.tree 15 assert_equal t, @v.tree 16 end 17 18 def test_yaml_tree_can_take_an_emitter 19 io = StringIO.new 20 e = Psych::Emitter.new io 21 v = Visitors::YAMLTree.new({}, e) 22 v.start 23 v << "hello world" 24 v.finish 25 26 assert_match "hello world", io.string 27 end 28 29 def test_binary_formatting 30 gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;" 31 @v << gif 32 scalar = @v.tree.children.first.children.first 33 assert_equal Psych::Nodes::Scalar::LITERAL, scalar.style 34 end 35 36 def test_object_has_no_class 37 yaml = Psych.dump(Object.new) 38 assert(Psych.dump(Object.new) !~ /Object/, yaml) 39 end 40 41 def test_struct_const 42 foo = Struct.new("Foo", :bar) 43 assert_cycle foo.new('bar') 44 Struct.instance_eval { remove_const(:Foo) } 45 end 46 47 A = Struct.new(:foo) 48 49 def test_struct 50 assert_cycle A.new('bar') 51 end 52 53 def test_struct_anon 54 s = Struct.new(:foo).new('bar') 55 obj = Psych.load(Psych.dump(s)) 56 assert_equal s.foo, obj.foo 57 end 58 59 def test_override_method 60 s = Struct.new(:method).new('override') 61 obj = Psych.load(Psych.dump(s)) 62 assert_equal s.method, obj.method 63 end 64 65 def test_exception 66 ex = Exception.new 'foo' 67 loaded = Psych.load(Psych.dump(ex)) 68 69 assert_equal ex.message, loaded.message 70 assert_equal ex.class, loaded.class 71 end 72 73 def test_regexp 74 assert_cycle(/foo/) 75 assert_cycle(/foo/i) 76 assert_cycle(/foo/mx) 77 end 78 79 def test_time 80 t = Time.now 81 assert_equal t, Psych.load(Psych.dump(t)) 82 end 83 84 def test_date 85 date = Date.strptime('2002-12-14', '%Y-%m-%d') 86 assert_cycle date 87 end 88 89 def test_rational 90 assert_cycle Rational(1,2) 91 end 92 93 def test_complex 94 assert_cycle Complex(1,2) 95 end 96 97 def test_scalar 98 assert_cycle 'foo' 99 assert_cycle ':foo' 100 assert_cycle '' 101 assert_cycle ':' 102 end 103 104 def test_boolean 105 assert_cycle true 106 assert_cycle 'true' 107 assert_cycle false 108 assert_cycle 'false' 109 end 110 111 def test_range_inclusive 112 assert_cycle 1..2 113 end 114 115 def test_range_exclusive 116 assert_cycle 1...2 117 end 118 119 def test_anon_class 120 assert_raises(TypeError) do 121 @v.accept Class.new 122 end 123 124 assert_raises(TypeError) do 125 Psych.dump(Class.new) 126 end 127 end 128 129 def test_hash 130 assert_cycle('a' => 'b') 131 end 132 133 def test_list 134 assert_cycle(%w{ a b }) 135 assert_cycle([1, 2.2]) 136 end 137 138 def test_symbol 139 assert_cycle :foo 140 end 141 142 def test_int 143 assert_cycle 1 144 assert_cycle(-1) 145 assert_cycle '1' 146 assert_cycle '-1' 147 end 148 149 def test_float 150 assert_cycle 1.2 151 assert_cycle '1.2' 152 153 assert Psych.load(Psych.dump(0.0 / 0.0)).nan? 154 assert_equal 1, Psych.load(Psych.dump(1 / 0.0)).infinite? 155 assert_equal(-1, Psych.load(Psych.dump(-1 / 0.0)).infinite?) 156 end 157 158 # http://yaml.org/type/null.html 159 def test_nil 160 assert_cycle nil 161 assert_equal nil, Psych.load('null') 162 assert_equal nil, Psych.load('Null') 163 assert_equal nil, Psych.load('NULL') 164 assert_equal nil, Psych.load('~') 165 assert_equal({'foo' => nil}, Psych.load('foo: ')) 166 167 assert_cycle 'null' 168 assert_cycle 'nUll' 169 assert_cycle '~' 170 end 171 end 172 end 173end 174