diff options
author | Marc-Andre Lafortune <github@marc-andre.ca> | 2020-09-14 13:48:29 -0400 |
---|---|---|
committer | Marc-André Lafortune <github@marc-andre.ca> | 2020-09-14 16:10:37 -0400 |
commit | 606c009ce24bd8e9e07ecb8f920a77c005062ff5 (patch) | |
tree | ce09e8c5a70da914079172c4b89f00577bf9be8d /test/ostruct | |
parent | 67e5f7a9e508d6f33c1dd927753161e8b1d40a09 (diff) | |
download | ruby-606c009ce24bd8e9e07ecb8f920a77c005062ff5.tar.gz |
[ruby/ostruct] Avoid self calling our public methods.
Found because `json` has a bad example in its test suite.
This implementation still offers better encapsulation.
Diffstat (limited to 'test/ostruct')
-rw-r--r-- | test/ostruct/test_ostruct.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index d07fef3a83..560979e887 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -246,4 +246,22 @@ class TC_OpenStruct < Test::Unit::TestCase os = OpenStruct.new(method: :foo) assert_equal(os.object_id, os.method!(:object_id).call) end + + def test_mistaken_subclass + sub = Class.new(OpenStruct) do + def [](k) + __send__(k) + super + end + + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + super + end + end + o = sub.new + o.foo = 42 + assert_equal 42, o.foo + end end |