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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
module ModuleSpecs
class Parent
def method_to_undef() 1 end
def another_method_to_undef() 1 end
end
class Ancestor
def method_to_undef() 1 end
def another_method_to_undef() 1 end
end
end
describe "Module#undef_method" do
before :each do
@module = Module.new { def method_to_undef; end }
end
it "is a public method" do
Module.should have_public_instance_method(:undef_method, false)
end
it "requires multiple arguments" do
Module.instance_method(:undef_method).arity.should < 0
end
it "allows multiple methods to be removed at once" do
klass = Class.new do
def method_to_undef() 1 end
def another_method_to_undef() 1 end
end
x = klass.new
klass.send(:undef_method, :method_to_undef, :another_method_to_undef)
-> { x.method_to_undef }.should raise_error(NoMethodError)
-> { x.another_method_to_undef }.should raise_error(NoMethodError)
end
it "does not undef any instance methods when argument not given" do
before = @module.instance_methods(true) + @module.private_instance_methods(true)
@module.send :undef_method
after = @module.instance_methods(true) + @module.private_instance_methods(true)
before.sort.should == after.sort
end
it "returns self" do
@module.send(:undef_method, :method_to_undef).should equal(@module)
end
it "raises a NameError when passed a missing name for a module" do
-> { @module.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for module `#{@module}'/) { |e|
# a NameError and not a NoMethodError
e.class.should == NameError
}
end
it "raises a NameError when passed a missing name for a class" do
klass = Class.new
-> { klass.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for class `#{klass}'/) { |e|
# a NameError and not a NoMethodError
e.class.should == NameError
}
end
it "raises a NameError when passed a missing name for a singleton class" do
klass = Class.new
obj = klass.new
sclass = obj.singleton_class
-> { sclass.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for class `#{sclass}'/) { |e|
e.message.should include('`#<Class:#<#<Class:')
# a NameError and not a NoMethodError
e.class.should == NameError
}
end
it "raises a NameError when passed a missing name for a metaclass" do
klass = String.singleton_class
-> { klass.send :undef_method, :not_exist }.should raise_error(NameError, /undefined method `not_exist' for class `String'/) { |e|
# a NameError and not a NoMethodError
e.class.should == NameError
}
end
describe "on frozen instance" do
before :each do
@frozen = @module.dup.freeze
end
it "raises a FrozenError when passed a name" do
-> { @frozen.send :undef_method, :method_to_undef }.should raise_error(FrozenError)
end
it "raises a FrozenError when passed a missing name" do
-> { @frozen.send :undef_method, :not_exist }.should raise_error(FrozenError)
end
it "raises a TypeError when passed a not name" do
-> { @frozen.send :undef_method, Object.new }.should raise_error(TypeError)
end
it "does not raise exceptions when no arguments given" do
@frozen.send(:undef_method).should equal(@frozen)
end
end
end
describe "Module#undef_method with symbol" do
it "removes a method defined in a class" do
klass = Class.new do
def method_to_undef() 1 end
def another_method_to_undef() 1 end
end
x = klass.new
x.method_to_undef.should == 1
klass.send :undef_method, :method_to_undef
-> { x.method_to_undef }.should raise_error(NoMethodError)
end
it "removes a method defined in a super class" do
child_class = Class.new(ModuleSpecs::Parent)
child = child_class.new
child.method_to_undef.should == 1
child_class.send :undef_method, :method_to_undef
-> { child.method_to_undef }.should raise_error(NoMethodError)
end
it "does not remove a method defined in a super class when removed from a subclass" do
descendant = Class.new(ModuleSpecs::Ancestor)
ancestor = ModuleSpecs::Ancestor.new
ancestor.method_to_undef.should == 1
descendant.send :undef_method, :method_to_undef
ancestor.method_to_undef.should == 1
end
end
describe "Module#undef_method with string" do
it "removes a method defined in a class" do
klass = Class.new do
def method_to_undef() 1 end
def another_method_to_undef() 1 end
end
x = klass.new
x.another_method_to_undef.should == 1
klass.send :undef_method, 'another_method_to_undef'
-> { x.another_method_to_undef }.should raise_error(NoMethodError)
end
it "removes a method defined in a super class" do
child_class = Class.new(ModuleSpecs::Parent)
child = child_class.new
child.another_method_to_undef.should == 1
child_class.send :undef_method, 'another_method_to_undef'
-> { child.another_method_to_undef }.should raise_error(NoMethodError)
end
it "does not remove a method defined in a super class when removed from a subclass" do
descendant = Class.new(ModuleSpecs::Ancestor)
ancestor = ModuleSpecs::Ancestor.new
ancestor.another_method_to_undef.should == 1
descendant.send :undef_method, 'another_method_to_undef'
ancestor.another_method_to_undef.should == 1
end
end
|