mirror of
https://github.com/ruby/ruby.git
synced 2025-08-23 13:04:13 +02:00

This sets the ivars _before_ calling initialize, which feels wrong. But Data doesn't give us any mechanism for setting the members other than 1) initialize, or 2) drop down into the C API. Since initialize freezes the object, we need to set the ivars before that. I think this is a reasonable compromise—if users need better handling, they can implement their own `encode_with` and `init_with`. But it will lead to unhappy surprises for some users. Alternatively, we could use the C API, similarly to Marshal. Psych _is_ already using the C API for path2class and build_exception. This would be the least surprising behavior for users, I think.
57 lines
1.3 KiB
Ruby
57 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
require_relative 'helper'
|
|
|
|
module Psych
|
|
class TestSerializeSubclasses < TestCase
|
|
class SomeObject
|
|
def initialize one, two
|
|
@one = one
|
|
@two = two
|
|
end
|
|
|
|
def == other
|
|
@one == other.instance_eval { @one } &&
|
|
@two == other.instance_eval { @two }
|
|
end
|
|
end
|
|
|
|
def test_some_object
|
|
so = SomeObject.new('foo', [1,2,3])
|
|
assert_equal so, Psych.unsafe_load(Psych.dump(so))
|
|
end
|
|
|
|
class StructSubclass < Struct.new(:foo)
|
|
def initialize foo, bar
|
|
super(foo)
|
|
@bar = bar
|
|
end
|
|
|
|
def == other
|
|
super(other) && @bar == other.instance_eval{ @bar }
|
|
end
|
|
end
|
|
|
|
def test_struct_subclass
|
|
so = StructSubclass.new('foo', [1,2,3])
|
|
assert_equal so, Psych.unsafe_load(Psych.dump(so))
|
|
end
|
|
|
|
class DataSubclass < Data.define(:foo)
|
|
def initialize(foo:)
|
|
@bar = "hello #{foo}"
|
|
super(foo: foo)
|
|
end
|
|
|
|
def == other
|
|
super(other) && @bar == other.instance_eval{ @bar }
|
|
end
|
|
end unless RUBY_VERSION < "3.2"
|
|
|
|
def test_data_subclass
|
|
omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2"
|
|
so = DataSubclass.new('foo')
|
|
assert_equal so, Psych.unsafe_load(Psych.dump(so))
|
|
end
|
|
|
|
end
|
|
end
|