diff options
author | Marc-Andre Lafortune <github@marc-andre.ca> | 2020-09-25 20:58:48 -0400 |
---|---|---|
committer | Marc-André Lafortune <github@marc-andre.ca> | 2020-09-30 18:11:24 -0400 |
commit | 083fa6e5d22ea7eb9026a4e33e31a1d8abbce7f8 (patch) | |
tree | a74e4aee4eba6b065ba75562e8d9f0c730694eb6 /test/ostruct/test_ostruct.rb | |
parent | df4d08c44ac3e96336d29a360aafdc4b2a3e96fc (diff) |
[ruby/ostruct] Protect subclass' methods and our bang methods.
Internally, use only bang methods
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3593
Diffstat (limited to 'test/ostruct/test_ostruct.rb')
-rw-r--r-- | test/ostruct/test_ostruct.rb | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index 8e1aedd896..6105f37c42 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -255,8 +255,30 @@ class TC_OpenStruct < Test::Unit::TestCase end def test_access_original_methods - os = OpenStruct.new(method: :foo) + os = OpenStruct.new(method: :foo, hash: 42) assert_equal(os.object_id, os.method!(:object_id).call) + assert_not_equal(42, os.hash!) + end + + def test_override_subclass + c = Class.new(OpenStruct) { + def foo; :protect_me; end + private def bar; :protect_me; end + def inspect; 'protect me'; end + } + o = c.new( + foo: 1, bar: 2, inspect: '3', # in subclass: protected + table!: 4, # bang method: protected + each_pair: 5, to_s: 'hello', # others: not protected + ) + # protected: + assert_equal(:protect_me, o.foo) + assert_equal(:protect_me, o.send(:bar)) + assert_equal('protect me', o.inspect) + assert_not_equal(4, o.send(:table!)) + # not protected: + assert_equal(5, o.each_pair) + assert_equal('hello', o.to_s) end def test_mistaken_subclass |