2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2013-03-26 10:55:04 -04:00
|
|
|
require_relative 'helper'
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
module Psych
|
|
|
|
class Tagged
|
|
|
|
yaml_tag '!foo'
|
|
|
|
|
|
|
|
attr_accessor :baz
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@baz = 'bar'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-21 14:22:54 -04:00
|
|
|
class Foo
|
|
|
|
attr_accessor :parent
|
|
|
|
|
|
|
|
def initialize parent
|
|
|
|
@parent = parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-28 17:49:37 -04:00
|
|
|
class TestObject < TestCase
|
|
|
|
def test_dump_with_tag
|
|
|
|
tag = Tagged.new
|
|
|
|
assert_match('foo', Psych.dump(tag))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tag_round_trip
|
|
|
|
tag = Tagged.new
|
2021-05-10 12:50:06 -04:00
|
|
|
tag2 = Psych.unsafe_load(Psych.dump(tag))
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_equal tag.baz, tag2.baz
|
|
|
|
assert_instance_of(Tagged, tag2)
|
|
|
|
end
|
2011-06-21 14:22:54 -04:00
|
|
|
|
|
|
|
def test_cyclic_references
|
|
|
|
foo = Foo.new(nil)
|
|
|
|
foo.parent = foo
|
2022-07-27 10:19:37 -04:00
|
|
|
loaded = Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: true)
|
2011-06-21 14:22:54 -04:00
|
|
|
|
|
|
|
assert_instance_of(Foo, loaded)
|
2022-07-27 10:19:37 -04:00
|
|
|
assert_same loaded, loaded.parent
|
2011-06-21 14:22:54 -04:00
|
|
|
end
|
2022-07-22 16:45:03 -04:00
|
|
|
|
|
|
|
def test_cyclic_reference_uses_alias
|
|
|
|
foo = Foo.new(nil)
|
|
|
|
foo.parent = foo
|
|
|
|
|
2022-07-21 15:07:39 -04:00
|
|
|
assert_raise(AliasesNotEnabled) do
|
2022-07-27 10:19:37 -04:00
|
|
|
Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: false)
|
|
|
|
end
|
2022-07-22 16:45:03 -04:00
|
|
|
end
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
end
|