1require 'psych/helper' 2 3module Psych 4 class TestDeprecated < TestCase 5 def teardown 6 Psych.domain_types.clear 7 end 8 9 class QuickEmitter 10 attr_reader :name 11 attr_reader :value 12 13 def initialize 14 @name = 'hello!!' 15 @value = 'Friday!' 16 end 17 18 def to_yaml opts = {} 19 Psych.quick_emit object_id, opts do |out| 20 out.map taguri, to_yaml_style do |map| 21 map.add 'name', @name 22 map.add 'value', nil 23 end 24 end 25 end 26 end 27 28 def setup 29 @qe = QuickEmitter.new 30 end 31 32 def test_quick_emit 33 qe2 = Psych.load @qe.to_yaml 34 assert_equal @qe.name, qe2.name 35 assert_instance_of QuickEmitter, qe2 36 assert_nil qe2.value 37 end 38 39 def test_recursive_quick_emit 40 hash = { :qe => @qe } 41 hash2 = Psych.load Psych.dump hash 42 qe = hash2[:qe] 43 44 assert_equal @qe.name, qe.name 45 assert_instance_of QuickEmitter, qe 46 assert_nil qe.value 47 end 48 49 class QuickEmitterEncodeWith 50 attr_reader :name 51 attr_reader :value 52 53 def initialize 54 @name = 'hello!!' 55 @value = 'Friday!' 56 end 57 58 def encode_with coder 59 coder.map do |map| 60 map.add 'name', @name 61 map.add 'value', nil 62 end 63 end 64 65 def to_yaml opts = {} 66 raise 67 end 68 end 69 70 ### 71 # An object that defines both to_yaml and encode_with should only call 72 # encode_with. 73 def test_recursive_quick_emit_encode_with 74 qeew = QuickEmitterEncodeWith.new 75 hash = { :qe => qeew } 76 hash2 = Psych.load Psych.dump hash 77 qe = hash2[:qe] 78 79 assert_equal qeew.name, qe.name 80 assert_instance_of QuickEmitterEncodeWith, qe 81 assert_nil qe.value 82 end 83 84 class YamlInit 85 attr_reader :name 86 attr_reader :value 87 88 def initialize 89 @name = 'hello!!' 90 @value = 'Friday!' 91 end 92 93 def yaml_initialize tag, vals 94 vals.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' } 95 end 96 end 97 98 def test_yaml_initialize 99 hash = { :yi => YamlInit.new } 100 hash2 = Psych.load Psych.dump hash 101 yi = hash2[:yi] 102 103 assert_equal 'TGIF!', yi.name 104 assert_equal 'TGIF!', yi.value 105 assert_instance_of YamlInit, yi 106 end 107 108 class YamlInitAndInitWith 109 attr_reader :name 110 attr_reader :value 111 112 def initialize 113 @name = 'shaners' 114 @value = 'Friday!' 115 end 116 117 def init_with coder 118 coder.map.each { |ivar, val| instance_variable_set "@#{ivar}", 'TGIF!' } 119 end 120 121 def yaml_initialize tag, vals 122 raise 123 end 124 end 125 126 ### 127 # An object that implements both yaml_initialize and init_with should not 128 # receive the yaml_initialize call. 129 def test_yaml_initialize_and_init_with 130 hash = { :yi => YamlInitAndInitWith.new } 131 hash2 = Psych.load Psych.dump hash 132 yi = hash2[:yi] 133 134 assert_equal 'TGIF!', yi.name 135 assert_equal 'TGIF!', yi.value 136 assert_instance_of YamlInitAndInitWith, yi 137 end 138 139 def test_coder_scalar 140 coder = Psych::Coder.new 'foo' 141 coder.scalar('tag', 'some string', :plain) 142 assert_equal 'tag', coder.tag 143 assert_equal 'some string', coder.scalar 144 assert_equal :scalar, coder.type 145 end 146 147 class YamlAs 148 psych_yaml_as 'helloworld' # this should be yaml_as but to avoid syck 149 end 150 151 def test_yaml_as 152 assert_match(/helloworld/, Psych.dump(YamlAs.new)) 153 end 154 155 def test_ruby_type 156 types = [] 157 appender = lambda { |*args| types << args } 158 159 Psych.add_ruby_type('foo', &appender) 160 Psych.load <<-eoyml 161- !ruby.yaml.org,2002/foo bar 162 eoyml 163 164 assert_equal [["tag:ruby.yaml.org,2002:foo", "bar"]], types 165 end 166 167 def test_detect_implicit 168 assert_equal '', Psych.detect_implicit(nil) 169 assert_equal '', Psych.detect_implicit(Object.new) 170 assert_equal '', Psych.detect_implicit(1.2) 171 assert_equal 'null', Psych.detect_implicit('') 172 assert_equal 'string', Psych.detect_implicit('foo') 173 end 174 175 def test_private_type 176 types = [] 177 Psych.add_private_type('foo') { |*args| types << args } 178 Psych.load <<-eoyml 179- !x-private:foo bar 180 eoyml 181 182 assert_equal [["x-private:foo", "bar"]], types 183 end 184 185 def test_tagurize 186 assert_nil Psych.tagurize nil 187 assert_equal Psych, Psych.tagurize(Psych) 188 assert_equal 'tag:yaml.org,2002:foo', Psych.tagurize('foo') 189 end 190 191 def test_read_type_class 192 things = Psych.read_type_class 'tag:yaml.org,2002:int:Psych::TestDeprecated::QuickEmitter', Object 193 assert_equal 'int', things.first 194 assert_equal Psych::TestDeprecated::QuickEmitter, things.last 195 end 196 197 def test_read_type_class_no_class 198 things = Psych.read_type_class 'tag:yaml.org,2002:int', Object 199 assert_equal 'int', things.first 200 assert_equal Object, things.last 201 end 202 203 def test_object_maker 204 thing = Psych.object_maker(Object, { 'a' => 'b', 'c' => 'd' }) 205 assert_instance_of(Object, thing) 206 assert_equal 'b', thing.instance_variable_get(:@a) 207 assert_equal 'd', thing.instance_variable_get(:@c) 208 end 209 end 210end 211