diff options
Diffstat (limited to 'test/ostruct/test_ostruct.rb')
-rw-r--r-- | test/ostruct/test_ostruct.rb | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index d857d5c66a..19bb606145 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -280,6 +280,7 @@ class TC_OpenStruct < Test::Unit::TestCase os = OpenStruct.new(method: :foo, hash: 42) assert_equal(os.object_id, os.method!(:object_id).call) assert_not_equal(42, os.hash!) + refute os.methods.include?(:"!~!") end def test_override_subclass @@ -368,6 +369,18 @@ class TC_OpenStruct < Test::Unit::TestCase RUBY end if defined?(Ractor) + def test_access_methods_from_different_ractor + assert_ractor(<<~RUBY, require: 'ostruct') + os = OpenStruct.new + os.value = 100 + r = Ractor.new(os) do |x| + v = x.value + Ractor.yield v + end + assert 100 == r.take + RUBY + end if defined?(Ractor) + def test_legacy_yaml s = "--- !ruby/object:OpenStruct\ntable:\n :foo: 42\n" o = YAML.safe_load(s, permitted_classes: [Symbol, OpenStruct]) @@ -375,7 +388,7 @@ class TC_OpenStruct < Test::Unit::TestCase o = OpenStruct.new(table: {foo: 42}) assert_equal({foo: 42}, YAML.safe_load(YAML.dump(o), permitted_classes: [Symbol, OpenStruct]).table) - end + end if RUBY_VERSION >= '2.6' def test_yaml h = {name: "John Smith", age: 70, pension: 300.42} @@ -386,11 +399,36 @@ class TC_OpenStruct < Test::Unit::TestCase assert_equal os1, os2 assert_equal true, os1.eql?(os2) assert_equal 300.42, os2.pension - end + end if RUBY_VERSION >= '2.6' def test_marshal o = OpenStruct.new(name: "John Smith", age: 70, pension: 300.42) o2 = Marshal.load(Marshal.dump(o)) assert_equal o, o2 end + + def test_class + os = OpenStruct.new(class: 'my-class', method: 'post') + assert_equal('my-class', os.class) + assert_equal(OpenStruct, os.class!) + end + + has_performance_warnings = begin + Warning[:performance] + true + rescue NoMethodError, ArgumentError + false + end + + if has_performance_warnings + def test_performance_warning + assert_in_out_err( + %w(-Ilib -rostruct -w -W:performance -e) + ['OpenStruct.new(a: 1)'], + "", + [], + ["-e:1: warning: OpenStruct use is discouraged for performance reasons"], + success: true, + ) + end + end end |