diff options
author | nick evans <[email protected]> | 2024-10-28 15:41:01 -0400 |
---|---|---|
committer | git <[email protected]> | 2025-05-01 17:52:13 +0000 |
commit | a397e4d4b0a0e7e8499a33ec760dba97ce494c63 (patch) | |
tree | 5781fd294aede7309847dbfd90c217c0067cfc63 /test | |
parent | bd1d6e8cd725e84addfac8583634458a624929ae (diff) |
[ruby/psych] Add support for ruby 3.2 Data objects
https://github.com/ruby/psych/commit/788b844c83
Diffstat (limited to 'test')
-rw-r--r-- | test/psych/test_object_references.rb | 5 | ||||
-rw-r--r-- | test/psych/test_safe_load.rb | 32 | ||||
-rw-r--r-- | test/psych/test_yaml.rb | 39 | ||||
-rw-r--r-- | test/psych/visitors/test_yaml_tree.rb | 21 |
4 files changed, 97 insertions, 0 deletions
diff --git a/test/psych/test_object_references.rb b/test/psych/test_object_references.rb index 86bb9034b9..0498d54eec 100644 --- a/test/psych/test_object_references.rb +++ b/test/psych/test_object_references.rb @@ -31,6 +31,11 @@ module Psych assert_reference_trip Struct.new(:foo).new(1) end + def test_data_has_references + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert_reference_trip Data.define(:foo).new(1) + end + def assert_reference_trip obj yml = Psych.dump([obj, obj]) assert_match(/\*-?\d+/, yml) diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb index a9ed737528..e6ca1e142b 100644 --- a/test/psych/test_safe_load.rb +++ b/test/psych/test_safe_load.rb @@ -114,6 +114,38 @@ module Psych end end + D = Data.define(:d) unless RUBY_VERSION < "3.2" + + def test_data_depends_on_sym + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert_safe_cycle(D.new(nil), permitted_classes: [D, Symbol]) + assert_raise(Psych::DisallowedClass) do + cycle D.new(nil), permitted_classes: [D] + end + end + + def test_anon_data + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert Psych.safe_load(<<-eoyml, permitted_classes: [Data, Symbol]) +--- !ruby/data + foo: bar + eoyml + + assert_raise(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, permitted_classes: [Data]) +--- !ruby/data + foo: bar + eoyml + end + + assert_raise(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, permitted_classes: [Symbol]) +--- !ruby/data + foo: bar + eoyml + end + end + def test_safe_load_default_fallback assert_nil Psych.safe_load("") end diff --git a/test/psych/test_yaml.rb b/test/psych/test_yaml.rb index bb9b25f250..89bd3130e3 100644 --- a/test/psych/test_yaml.rb +++ b/test/psych/test_yaml.rb @@ -6,6 +6,7 @@ require_relative 'helper' # [ruby-core:01946] module Psych_Tests StructTest = Struct::new( :c ) + DataTest = Data.define( :c ) unless RUBY_VERSION < "3.2" end class Psych_Unit_Tests < Psych::TestCase @@ -1075,6 +1076,44 @@ EOY end + def test_ruby_data + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + 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 + year: 2002 + isbn: 0-596-00214-9 +- !ruby/data:MyBookData + author: + - Dave Thomas + - Andy Hunt + title: The Pickaxe + year: 2002 + isbn: !ruby/data:MyBookData + author: This should be the ISBN + title: but I have more data here + year: 2002 + isbn: None +EOY + ) + + assert_to_yaml( Psych_Tests::DataTest.new( 123 ), <<EOY ) +--- !ruby/data:Psych_Tests::DataTest +c: 123 +EOY + + end + def test_ruby_rational assert_to_yaml( Rational(1, 2), <<EOY ) --- !ruby/object:Rational diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb index 01e685134a..bd3919f83d 100644 --- a/test/psych/visitors/test_yaml_tree.rb +++ b/test/psych/visitors/test_yaml_tree.rb @@ -73,6 +73,27 @@ module Psych assert_equal s.method, obj.method end + D = Data.define(:foo) unless RUBY_VERSION < "3.2" + + def test_data + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + assert_cycle D.new('bar') + end + + def test_data_anon + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + 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 + omit "Data requires ruby >= 3.2" if RUBY_VERSION < "3.2" + d = Data.define(:method).new('override') + obj = Psych.unsafe_load(Psych.dump(d)) + assert_equal d.method, obj.method + end + def test_exception ex = Exception.new 'foo' loaded = Psych.unsafe_load(Psych.dump(ex)) |