From 3110d41dc3979934be27e7ebab9c01e2ba96332e Mon Sep 17 00:00:00 2001 From: nick evans Date: Fri, 8 Nov 2024 09:27:51 -0500 Subject: [PATCH] Fix tests for for versions of ruby before ::Data --- test/psych/test_object_references.rb | 6 +++-- test/psych/test_safe_load.rb | 34 +++++++++++++------------ test/psych/test_serialize_subclasses.rb | 23 +++++++++-------- test/psych/test_yaml.rb | 28 ++++++++++---------- test/psych/visitors/test_yaml_tree.rb | 28 ++++++++++---------- 5 files changed, 65 insertions(+), 54 deletions(-) diff --git a/test/psych/test_object_references.rb b/test/psych/test_object_references.rb index e1c415f3..9e0610d5 100644 --- a/test/psych/test_object_references.rb +++ b/test/psych/test_object_references.rb @@ -31,8 +31,10 @@ def test_struct_has_references assert_reference_trip Struct.new(:foo).new(1) end - def test_data_has_references - assert_reference_trip Data.define(:foo).new(1) + if defined?(::Data) + def test_data_has_references + assert_reference_trip Data.define(:foo).new(1) + end end def assert_reference_trip obj diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb index 1060ea54..5195eb27 100644 --- a/test/psych/test_safe_load.rb +++ b/test/psych/test_safe_load.rb @@ -114,32 +114,34 @@ def test_anon_struct end end - D = Data.define(:d) - def test_data_depends_on_sym - assert_safe_cycle(D.new(nil), permitted_classes: [D, Symbol]) - assert_raise(Psych::DisallowedClass) do - cycle D.new(nil), permitted_classes: [D] + if defined?(::Data) + D = Data.define(:d) + def test_data_depends_on_sym + assert_safe_cycle(D.new(nil), permitted_classes: [D, Symbol]) + assert_raise(Psych::DisallowedClass) do + cycle D.new(nil), permitted_classes: [D] + end end - end - def test_anon_data - assert Psych.safe_load(<<-eoyml, permitted_classes: [Data, Symbol]) + def test_anon_data + assert Psych.safe_load(<<-eoyml, permitted_classes: [Data, Symbol]) --- !ruby/data foo: bar - eoyml + eoyml - assert_raise(Psych::DisallowedClass) do - Psych.safe_load(<<-eoyml, permitted_classes: [Data]) + assert_raise(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, permitted_classes: [Data]) --- !ruby/data foo: bar - eoyml - end + eoyml + end - assert_raise(Psych::DisallowedClass) do - Psych.safe_load(<<-eoyml, permitted_classes: [Symbol]) + assert_raise(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, permitted_classes: [Symbol]) --- !ruby/data foo: bar - eoyml + eoyml + end end end diff --git a/test/psych/test_serialize_subclasses.rb b/test/psych/test_serialize_subclasses.rb index 7a7f2a1b..cfae422a 100644 --- a/test/psych/test_serialize_subclasses.rb +++ b/test/psych/test_serialize_subclasses.rb @@ -36,20 +36,23 @@ def test_struct_subclass assert_equal so, Psych.unsafe_load(Psych.dump(so)) end - class DataSubclass < Data.define(:foo) - def initialize(foo:) - @bar = "hello #{foo}" - super(foo:) + if defined?(::Data) + 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 - def == other - super(other) && @bar == other.instance_eval{ @bar } + def test_data_subclass + so = DataSubclass.new('foo') + assert_equal so, Psych.unsafe_load(Psych.dump(so)) end end - def test_data_subclass - so = DataSubclass.new('foo') - assert_equal so, Psych.unsafe_load(Psych.dump(so)) - end end end diff --git a/test/psych/test_yaml.rb b/test/psych/test_yaml.rb index 45bed12f..a81d510c 100644 --- a/test/psych/test_yaml.rb +++ b/test/psych/test_yaml.rb @@ -7,7 +7,7 @@ # [ruby-core:01946] module Psych_Tests StructTest = Struct::new( :c ) - DataTest = Data.define( :c ) + DataTest = Data.define( :c ) if defined?(::Data) end class Psych_Unit_Tests < Psych::TestCase @@ -1067,16 +1067,17 @@ def test_ruby_struct end - def test_ruby_data - Object.remove_const :MyBookData if Object.const_defined?(:MyBookData) - # Ruby Data value objects - book_class = Data.define(:author, :title, :year, :isbn) - Object.const_set(:MyBookData, book_class) - assert_to_yaml( - [ book_class.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ), - book_class.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002, - book_class.new( "This should be the ISBN", "but I have more data here", 2002, "None" ) - ) ], <