Skip to content

Commit 3110d41

Browse files
committed
Fix tests for for versions of ruby before ::Data
1 parent 01d1bc0 commit 3110d41

File tree

5 files changed

+65
-54
lines changed

5 files changed

+65
-54
lines changed

test/psych/test_object_references.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ def test_struct_has_references
3131
assert_reference_trip Struct.new(:foo).new(1)
3232
end
3333

34-
def test_data_has_references
35-
assert_reference_trip Data.define(:foo).new(1)
34+
if defined?(::Data)
35+
def test_data_has_references
36+
assert_reference_trip Data.define(:foo).new(1)
37+
end
3638
end
3739

3840
def assert_reference_trip obj

test/psych/test_safe_load.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,34 @@ def test_anon_struct
114114
end
115115
end
116116

117-
D = Data.define(:d)
118-
def test_data_depends_on_sym
119-
assert_safe_cycle(D.new(nil), permitted_classes: [D, Symbol])
120-
assert_raise(Psych::DisallowedClass) do
121-
cycle D.new(nil), permitted_classes: [D]
117+
if defined?(::Data)
118+
D = Data.define(:d)
119+
def test_data_depends_on_sym
120+
assert_safe_cycle(D.new(nil), permitted_classes: [D, Symbol])
121+
assert_raise(Psych::DisallowedClass) do
122+
cycle D.new(nil), permitted_classes: [D]
123+
end
122124
end
123-
end
124125

125-
def test_anon_data
126-
assert Psych.safe_load(<<-eoyml, permitted_classes: [Data, Symbol])
126+
def test_anon_data
127+
assert Psych.safe_load(<<-eoyml, permitted_classes: [Data, Symbol])
127128
--- !ruby/data
128129
foo: bar
129-
eoyml
130+
eoyml
130131

131-
assert_raise(Psych::DisallowedClass) do
132-
Psych.safe_load(<<-eoyml, permitted_classes: [Data])
132+
assert_raise(Psych::DisallowedClass) do
133+
Psych.safe_load(<<-eoyml, permitted_classes: [Data])
133134
--- !ruby/data
134135
foo: bar
135-
eoyml
136-
end
136+
eoyml
137+
end
137138

138-
assert_raise(Psych::DisallowedClass) do
139-
Psych.safe_load(<<-eoyml, permitted_classes: [Symbol])
139+
assert_raise(Psych::DisallowedClass) do
140+
Psych.safe_load(<<-eoyml, permitted_classes: [Symbol])
140141
--- !ruby/data
141142
foo: bar
142-
eoyml
143+
eoyml
144+
end
143145
end
144146
end
145147

test/psych/test_serialize_subclasses.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,23 @@ def test_struct_subclass
3636
assert_equal so, Psych.unsafe_load(Psych.dump(so))
3737
end
3838

39-
class DataSubclass < Data.define(:foo)
40-
def initialize(foo:)
41-
@bar = "hello #{foo}"
42-
super(foo:)
39+
if defined?(::Data)
40+
class DataSubclass < Data.define(:foo)
41+
def initialize(foo:)
42+
@bar = "hello #{foo}"
43+
super(foo: foo)
44+
end
45+
46+
def == other
47+
super(other) && @bar == other.instance_eval{ @bar }
48+
end
4349
end
4450

45-
def == other
46-
super(other) && @bar == other.instance_eval{ @bar }
51+
def test_data_subclass
52+
so = DataSubclass.new('foo')
53+
assert_equal so, Psych.unsafe_load(Psych.dump(so))
4754
end
4855
end
4956

50-
def test_data_subclass
51-
so = DataSubclass.new('foo')
52-
assert_equal so, Psych.unsafe_load(Psych.dump(so))
53-
end
5457
end
5558
end

test/psych/test_yaml.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# [ruby-core:01946]
88
module Psych_Tests
99
StructTest = Struct::new( :c )
10-
DataTest = Data.define( :c )
10+
DataTest = Data.define( :c ) if defined?(::Data)
1111
end
1212

1313
class Psych_Unit_Tests < Psych::TestCase
@@ -1067,16 +1067,17 @@ def test_ruby_struct
10671067

10681068
end
10691069

1070-
def test_ruby_data
1071-
Object.remove_const :MyBookData if Object.const_defined?(:MyBookData)
1072-
# Ruby Data value objects
1073-
book_class = Data.define(:author, :title, :year, :isbn)
1074-
Object.const_set(:MyBookData, book_class)
1075-
assert_to_yaml(
1076-
[ book_class.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ),
1077-
book_class.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002,
1078-
book_class.new( "This should be the ISBN", "but I have more data here", 2002, "None" )
1079-
) ], <<EOY
1070+
if defined?(::Data)
1071+
def test_ruby_data
1072+
Object.remove_const :MyBookData if Object.const_defined?(:MyBookData)
1073+
# Ruby Data value objects
1074+
book_class = Data.define(:author, :title, :year, :isbn)
1075+
Object.const_set(:MyBookData, book_class)
1076+
assert_to_yaml(
1077+
[ book_class.new( "Yukihiro Matsumoto", "Ruby in a Nutshell", 2002, "0-596-00214-9" ),
1078+
book_class.new( [ 'Dave Thomas', 'Andy Hunt' ], "The Pickaxe", 2002,
1079+
book_class.new( "This should be the ISBN", "but I have more data here", 2002, "None" )
1080+
) ], <<EOY
10801081
- !ruby/data:MyBookData
10811082
author: Yukihiro Matsumoto
10821083
title: Ruby in a Nutshell
@@ -1094,13 +1095,14 @@ def test_ruby_data
10941095
year: 2002
10951096
isbn: None
10961097
EOY
1097-
)
1098+
)
10981099

1099-
assert_to_yaml( Psych_Tests::DataTest.new( 123 ), <<EOY )
1100+
assert_to_yaml( Psych_Tests::DataTest.new( 123 ), <<EOY )
11001101
--- !ruby/data:Psych_Tests::DataTest
11011102
c: 123
11021103
EOY
11031104

1105+
end
11041106
end
11051107

11061108
def test_ruby_rational

test/psych/visitors/test_yaml_tree.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,24 @@ def test_override_method
7373
assert_equal s.method, obj.method
7474
end
7575

76-
D = Data.define(:foo)
76+
if defined?(::Data)
77+
D = Data.define(:foo)
7778

78-
def test_data
79-
assert_cycle D.new('bar')
80-
end
79+
def test_data
80+
assert_cycle D.new('bar')
81+
end
8182

82-
def test_data_anon
83-
d = Data.define(:foo).new('bar')
84-
obj = Psych.unsafe_load(Psych.dump(d))
85-
assert_equal d.foo, obj.foo
86-
end
83+
def test_data_anon
84+
d = Data.define(:foo).new('bar')
85+
obj = Psych.unsafe_load(Psych.dump(d))
86+
assert_equal d.foo, obj.foo
87+
end
8788

88-
def test_data_override_method
89-
d = Data.define(:method).new('override')
90-
obj = Psych.unsafe_load(Psych.dump(d))
91-
assert_equal d.method, obj.method
89+
def test_data_override_method
90+
d = Data.define(:method).new('override')
91+
obj = Psych.unsafe_load(Psych.dump(d))
92+
assert_equal d.method, obj.method
93+
end
9294
end
9395

9496
def test_exception

0 commit comments

Comments
 (0)