Skip to content

Commit

Permalink
Fix tests for for versions of ruby before ::Data
Browse files Browse the repository at this point in the history
  • Loading branch information
nevans committed Nov 8, 2024
1 parent 01d1bc0 commit 3110d41
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 54 deletions.
6 changes: 4 additions & 2 deletions test/psych/test_object_references.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions test/psych/test_safe_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 13 additions & 10 deletions test/psych/test_serialize_subclasses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 15 additions & 13 deletions test/psych/test_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" )
) ], <<EOY
if defined?(::Data)
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" )
) ], <<EOY
- !ruby/data:MyBookData
author: Yukihiro Matsumoto
title: Ruby in a Nutshell
Expand All @@ -1094,13 +1095,14 @@ def test_ruby_data
year: 2002
isbn: None
EOY
)
)

assert_to_yaml( Psych_Tests::DataTest.new( 123 ), <<EOY )
assert_to_yaml( Psych_Tests::DataTest.new( 123 ), <<EOY )
--- !ruby/data:Psych_Tests::DataTest
c: 123
EOY

end
end

def test_ruby_rational
Expand Down
28 changes: 15 additions & 13 deletions test/psych/visitors/test_yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,24 @@ def test_override_method
assert_equal s.method, obj.method
end

D = Data.define(:foo)
if defined?(::Data)
D = Data.define(:foo)

def test_data
assert_cycle D.new('bar')
end
def test_data
assert_cycle D.new('bar')
end

def test_data_anon
d = Data.define(:foo).new('bar')
obj = Psych.unsafe_load(Psych.dump(d))
assert_equal d.foo, obj.foo
end
def test_data_anon
d = Data.define(:foo).new('bar')
obj = Psych.unsafe_load(Psych.dump(d))
assert_equal d.foo, obj.foo
end

def test_data_override_method
d = Data.define(:method).new('override')
obj = Psych.unsafe_load(Psych.dump(d))
assert_equal d.method, obj.method
def test_data_override_method
d = Data.define(:method).new('override')
obj = Psych.unsafe_load(Psych.dump(d))
assert_equal d.method, obj.method
end
end

def test_exception
Expand Down

0 comments on commit 3110d41

Please sign in to comment.