2020-12-13 20:50:22 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestMethodCache < Test::Unit::TestCase
|
|
|
|
def test_undef
|
|
|
|
# clear same
|
|
|
|
c0 = Class.new do
|
|
|
|
def foo; end
|
|
|
|
undef foo
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(NoMethodError) do
|
|
|
|
c0.new.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
c0.class_eval do
|
|
|
|
def foo; :ok; end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal :ok, c0.new.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_undef_with_subclasses
|
|
|
|
# with subclasses
|
|
|
|
c0 = Class.new do
|
|
|
|
def foo; end
|
|
|
|
undef foo
|
|
|
|
end
|
|
|
|
|
2020-12-14 01:56:13 -05:00
|
|
|
_c1 = Class.new(c0)
|
2020-12-13 20:50:22 -05:00
|
|
|
|
|
|
|
assert_raise(NoMethodError) do
|
|
|
|
c0.new.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
c0.class_eval do
|
|
|
|
def foo; :ok; end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal :ok, c0.new.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_undef_with_subclasses_complicated
|
|
|
|
c0 = Class.new{ def foo; end }
|
|
|
|
c1 = Class.new(c0){ undef foo }
|
|
|
|
c2 = Class.new(c1)
|
|
|
|
c3 = Class.new(c2)
|
2020-12-14 01:56:13 -05:00
|
|
|
_c4 = Class.new(c3)
|
2020-12-13 20:50:22 -05:00
|
|
|
|
|
|
|
assert_raise(NoMethodError) do
|
|
|
|
c3.new.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
c2.class_eval do
|
|
|
|
def foo; :c2; end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(NoMethodError) do
|
|
|
|
c1.new.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal :c2, c3.new.foo
|
|
|
|
end
|
2021-02-19 00:57:59 -05:00
|
|
|
|
|
|
|
def test_negative_cache_with_and_without_subclasses
|
|
|
|
c0 = Class.new{}
|
|
|
|
c1 = Class.new(c0){}
|
|
|
|
c0.new.foo rescue nil
|
|
|
|
c1.new.foo rescue nil
|
|
|
|
c1.module_eval{ def foo = :c1 }
|
|
|
|
c0.module_eval{ def foo = :c0 }
|
|
|
|
|
|
|
|
assert_equal :c0, c0.new.foo
|
|
|
|
end
|
2020-12-13 20:50:22 -05:00
|
|
|
end
|
|
|
|
|