1require_relative '../../spec_helper' 2require_relative 'fixtures/classes' 3 4describe "Module#method_defined?" do 5 it "returns true if a public or private method with the given name is defined in self, self's ancestors or one of self's included modules" do 6 # Defined in Child 7 ModuleSpecs::Child.method_defined?(:public_child).should == true 8 ModuleSpecs::Child.method_defined?("private_child").should == false 9 ModuleSpecs::Child.method_defined?(:accessor_method).should == true 10 11 # Defined in Parent 12 ModuleSpecs::Child.method_defined?("public_parent").should == true 13 ModuleSpecs::Child.method_defined?(:private_parent).should == false 14 15 # Defined in Module 16 ModuleSpecs::Child.method_defined?(:public_module).should == true 17 ModuleSpecs::Child.method_defined?(:protected_module).should == true 18 ModuleSpecs::Child.method_defined?(:private_module).should == false 19 20 # Defined in SuperModule 21 ModuleSpecs::Child.method_defined?(:public_super_module).should == true 22 ModuleSpecs::Child.method_defined?(:protected_super_module).should == true 23 ModuleSpecs::Child.method_defined?(:private_super_module).should == false 24 end 25 26 # unlike alias_method, module_function, public, and friends, 27 it "does not search Object or Kernel when called on a module" do 28 m = Module.new 29 30 m.method_defined?(:module_specs_public_method_on_kernel).should be_false 31 end 32 33 it "raises a TypeError when the given object is not a string/symbol/fixnum" do 34 c = Class.new 35 o = mock('123') 36 37 lambda { c.method_defined?(o) }.should raise_error(TypeError) 38 39 o.should_receive(:to_str).and_return(123) 40 lambda { c.method_defined?(o) }.should raise_error(TypeError) 41 end 42 43 it "converts the given name to a string using to_str" do 44 c = Class.new { def test(); end } 45 (o = mock('test')).should_receive(:to_str).and_return("test") 46 47 c.method_defined?(o).should == true 48 end 49end 50