Make prettyprint’s cycle detection aware of Delegator instances

Fixes [Bug #13144]

Co-Authored-By: Nobuyoshi Nakada <nobu@ruby-lang.org>
This commit is contained in:
Richard Viney 2017-01-22 14:50:08 +13:00 коммит произвёл Nobuyoshi Nakada
Родитель 251f5d8226
Коммит 6a75a46053
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4BC7D6DF58D8DF60
2 изменённых файлов: 16 добавлений и 0 удалений

Просмотреть файл

@ -149,6 +149,10 @@ class PP < PrettyPrint
# Object#pretty_print_cycle is used when +obj+ is already
# printed, a.k.a the object reference chain has a cycle.
def pp(obj)
# If obj is a Delegator then use the object being delegated to for cycle
# detection
obj = obj.__getobj__ if defined?(::Delegator) and obj.is_a?(::Delegator)
if check_inspect_key(obj)
group {obj.pretty_print_cycle self}
return

Просмотреть файл

@ -184,6 +184,18 @@ class PPDelegateTest < Test::Unit::TestCase
def test_delegate
assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]")
end
def test_delegate_cycle
a = HasPrettyPrint.new nil
a.instance_eval {@a = a}
cycle_pretty_inspect = a.pretty_inspect
a.instance_eval {@a = SimpleDelegator.new(a)}
delegator_cycle_pretty_inspect = a.pretty_inspect
assert_equal(cycle_pretty_inspect, delegator_cycle_pretty_inspect)
end
end
class PPFileStatTest < Test::Unit::TestCase