aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_module.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2021-05-22 12:04:01 +0200
committerJean Boussier <jean.boussier@gmail.com>2022-01-14 11:30:07 +0100
commit8d05047d72d0a4b97f57b23bddbca639375bbd03 (patch)
treed3caaf18c73b9f3423402162626c5dc5abc4965e /test/ruby/test_module.rb
parent53a4e101465a4d2ae38ea3bfd3cb7dd3c9f12f61 (diff)
downloadruby-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/ruby/test_module.rb')
-rw-r--r--test/ruby/test_module.rb39
1 files changed, 39 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