1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/psych/lib/psych/visitors/to_ruby.rb: correctly register

self-referential strings. Fixes tenderlove/psych #135

* test/psych/test_string.rb: appropriate test.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tenderlove 2013-04-05 17:11:21 +00:00
parent fbb29bc08c
commit 7a7bb64464
3 changed files with 49 additions and 7 deletions

View file

@ -1,3 +1,10 @@
Sat Apr 6 02:06:04 2013 Aaron Patterson <aaron@tenderlovemaking.com>
* ext/psych/lib/psych/visitors/to_ruby.rb: correctly register
self-referential strings. Fixes tenderlove/psych #135
* test/psych/test_string.rb: appropriate test.
Sat Apr 6 01:21:56 2013 Tanaka Akira <akr@fsij.org> Sat Apr 6 01:21:56 2013 Tanaka Akira <akr@fsij.org>
* ext/socket/init.c (cloexec_accept): Fix a compile error on * ext/socket/init.c (cloexec_accept): Fix a compile error on

View file

@ -180,15 +180,25 @@ module Psych
end end
when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str' when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str'
klass = resolve_class($1) klass = resolve_class($1)
members = Hash[*o.children.map { |c| accept c }] members = {}
string = members.delete 'str' string = nil
if klass o.children.each_slice(2) do |k,v|
string = klass.allocate.replace string key = accept k
register(o, string) value = accept v
if key == 'str'
if klass
string = klass.allocate.replace value
else
string = value
end
register(o, string)
else
members[key] = value
end
end end
init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o) init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
when /^!ruby\/array:(.*)$/ when /^!ruby\/array:(.*)$/
klass = resolve_class($1) klass = resolve_class($1)

View file

@ -15,6 +15,31 @@ module Psych
end end
end end
def test_string_subclass_with_anchor
y = Psych.load <<-eoyml
---
body:
string: &70121654388580 !ruby/string
str: ! 'foo'
x:
body: *70121654388580
eoyml
assert_equal({"body"=>{"string"=>"foo", "x"=>{"body"=>"foo"}}}, y)
end
def test_self_referential_string
y = Psych.load <<-eoyml
---
string: &70121654388580 !ruby/string
str: ! 'foo'
body: *70121654388580
eoyml
assert_equal({"string"=>"foo"}, y)
value = y['string']
assert_equal value, value.instance_variable_get(:@body)
end
def test_another_subclass_with_attributes def test_another_subclass_with_attributes
y = Psych.load Psych.dump Y.new("foo").tap {|y| y.val = 1} y = Psych.load Psych.dump Y.new("foo").tap {|y| y.val = 1}
assert_equal "foo", y assert_equal "foo", y