1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
require 'test/unit'
class TestMethodMissing < Test::Unit::TestCase
class AParent
def method_missing *args
end
end
class AChild < AParent
def foo
super
end
end
class AMethodMissingClass
def method_missing name, *args
1
end
end
class AClassWithProtectedAndPrivateMethod
private
def a_private_method
end
protected
def a_protected_method
end
end
def test_super_method_missing
assert_nothing_raised{AChild.new.foo}
end
def test_private_method_missing
assert_raise(NoMethodError){AClassWithProtectedAndPrivateMethod.new.a_private_method}
begin
AClassWithProtectedAndPrivateMethod.new.a_private_method
rescue Exception => e
assert(e.message =~ /private method/)
end
end
def test_protected_method_missing
assert_raise(NoMethodError){AClassWithProtectedAndPrivateMethod.new.a_protected_method}
begin
AClassWithProtectedAndPrivateMethod.new.a_protected_method
rescue Exception => e
assert(e.message =~ /protected method/)
end
end
def test_undefined_method_missing
assert_raise(NoMethodError){AClassWithProtectedAndPrivateMethod.new.a_missing_method}
begin
AClassWithProtectedAndPrivateMethod.new.a_missing_method
rescue Exception => e
assert(e.message =~ /undefined method/)
end
end
def test_no_method_error_args
begin
some_method "a", 1
rescue Exception => e
assert_equal(e.args, ["a",1])
assert_equal(e.class, NoMethodError)
end
end
def test_undef_method_and_clone_singleton_class
s = "a string"
s.instance_eval do
(class << self;self;end).class_eval do
undef_method :length
end
end
assert_raise(NoMethodError){s.clone.length}
assert_nothing_raised{s.dup.length}
end
def test_attr_assign_missing_returns_rhs
assert_equal(AMethodMissingClass.new.foo=2, 2)
assert_equal(eval("AMethodMissingClass.new.foo=2"), 2)
end
def test_no_super_method_in_module
Kernel.module_eval do
def a_module_method arg
super
end
end
assert_raise(NoMethodError){a_module_method 'foo'}
end
class TestInspectNotCalled
def inspect
# raising here wont help since any exception will be swallowed in default method_missing
$inspect_not_called = true
end
end
def test_inspect_not_called_on_method_missing
TestInspectNotCalled.new.foo rescue nil
assert_equal nil, $inspect_not_called
end
end
|