diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2021-05-22 12:04:01 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2022-01-14 11:30:07 +0100 |
commit | 8d05047d72d0a4b97f57b23bddbca639375bbd03 (patch) | |
tree | d3caaf18c73b9f3423402162626c5dc5abc4965e /test | |
parent | 53a4e101465a4d2ae38ea3bfd3cb7dd3c9f12f61 (diff) | |
download | ruby-8d05047d72d0a4b97f57b23bddbca639375bbd03.tar.gz |
Add a Module#const_added callback
[Feature #17881]
Works similarly to `method_added` but for constants.
```ruby
Foo::BAR = 42 # call Foo.const_added(:FOO)
class Foo::Baz; end # call Foo.const_added(:Baz)
Foo.autoload(:Something, "path") # call Foo.const_added(:Something)
```
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_module.rb | 39 | ||||
-rw-r--r-- | test/ruby/test_settracefunc.rb | 10 |
2 files changed, 49 insertions, 0 deletions
diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 9368940050..0a6959c5e6 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1675,6 +1675,45 @@ class TestModule < Test::Unit::TestCase assert_match(/::X\u{df}:/, c.new.to_s) end + + def test_const_added + eval(<<~RUBY) + module TestConstAdded + @memo = [] + class << self + attr_accessor :memo + + def const_added(sym) + memo << sym + end + end + CONST = 1 + module SubModule + end + + class SubClass + end + end + TestConstAdded::OUTSIDE_CONST = 2 + module TestConstAdded::OutsideSubModule; end + class TestConstAdded::OutsideSubClass; end + RUBY + TestConstAdded.const_set(:CONST_SET, 3) + assert_equal [ + :CONST, + :SubModule, + :SubClass, + :OUTSIDE_CONST, + :OutsideSubModule, + :OutsideSubClass, + :CONST_SET, + ], TestConstAdded.memo + ensure + if self.class.const_defined? :TestConstAdded + self.class.send(:remove_const, :TestConstAdded) + end + end + def test_method_added memo = [] mod = Module.new do diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb index e973e3a384..0524c35873 100644 --- a/test/ruby/test_settracefunc.rb +++ b/test/ruby/test_settracefunc.rb @@ -108,6 +108,10 @@ class TestSetTraceFunc < Test::Unit::TestCase events.shift) assert_equal(["line", 4, __method__, self.class], events.shift) + assert_equal(["c-call", 4, :const_added, Module], + events.shift) + assert_equal(["c-return", 4, :const_added, Module], + events.shift) assert_equal(["c-call", 4, :inherited, Class], events.shift) assert_equal(["c-return", 4, :inherited, Class], @@ -345,6 +349,8 @@ class TestSetTraceFunc < Test::Unit::TestCase [["c-return", 2, :add_trace_func, Thread], ["line", 3, __method__, self.class], + ["c-call", 3, :const_added, Module], + ["c-return", 3, :const_added, Module], ["c-call", 3, :inherited, Class], ["c-return", 3, :inherited, Class], ["class", 3, nil, nil], @@ -487,6 +493,8 @@ class TestSetTraceFunc < Test::Unit::TestCase [:line, 5, 'xyzzy', self.class, method, self, :inner, :nothing], [:c_return, 4, "xyzzy", Integer, :times, 1, :outer, 1], [:line, 7, 'xyzzy', self.class, method, self, :outer, :nothing], + [:c_call, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, :outer, :nothing], + [:c_return, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, :outer, nil], [:c_call, 7, "xyzzy", Class, :inherited, Object, :outer, :nothing], [:c_return, 7, "xyzzy", Class, :inherited, Object, :outer, nil], [:class, 7, "xyzzy", nil, nil, xyzzy.class, nil, :nothing], @@ -620,6 +628,8 @@ CODE [:line, 7, 'xyzzy', self.class, method, self, :outer, :nothing], [:c_call, 7, "xyzzy", Class, :inherited, Object, :outer, :nothing], [:c_return, 7, "xyzzy", Class, :inherited, Object, :outer, nil], + [:c_call, 7, "xyzzy", Class, :const_added, Object, :outer, :nothing], + [:c_return, 7, "xyzzy", Class, :const_added, Object, :outer, nil], [:class, 7, "xyzzy", nil, nil, xyzzy.class, nil, :nothing], [:line, 8, "xyzzy", nil, nil, xyzzy.class, nil, :nothing], [:line, 9, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer, :nothing], |