1require_relative '../../spec_helper'
2require_relative 'fixtures/classes'
3
4describe "Module#public_method_defined?" do
5  it "returns true if the named public method is defined by module or its ancestors" do
6    ModuleSpecs::CountsMixin.public_method_defined?("public_3").should == true
7
8    ModuleSpecs::CountsParent.public_method_defined?("public_3").should == true
9    ModuleSpecs::CountsParent.public_method_defined?("public_2").should == true
10
11    ModuleSpecs::CountsChild.public_method_defined?("public_3").should == true
12    ModuleSpecs::CountsChild.public_method_defined?("public_2").should == true
13    ModuleSpecs::CountsChild.public_method_defined?("public_1").should == true
14  end
15
16  it "returns false if method is not a public method" do
17    ModuleSpecs::CountsChild.public_method_defined?("private_3").should == false
18    ModuleSpecs::CountsChild.public_method_defined?("private_2").should == false
19    ModuleSpecs::CountsChild.public_method_defined?("private_1").should == false
20
21    ModuleSpecs::CountsChild.public_method_defined?("protected_3").should == false
22    ModuleSpecs::CountsChild.public_method_defined?("protected_2").should == false
23    ModuleSpecs::CountsChild.public_method_defined?("protected_1").should == false
24  end
25
26  it "returns false if the named method is not defined by the module or its ancestors" do
27    ModuleSpecs::CountsMixin.public_method_defined?(:public_10).should == false
28  end
29
30  it "accepts symbols for the method name" do
31    ModuleSpecs::CountsMixin.public_method_defined?(:public_3).should == true
32  end
33
34  it "raises a TypeError if passed a Fixnum" do
35    lambda do
36      ModuleSpecs::CountsMixin.public_method_defined?(1)
37    end.should raise_error(TypeError)
38  end
39
40  it "raises a TypeError if passed nil" do
41    lambda do
42      ModuleSpecs::CountsMixin.public_method_defined?(nil)
43    end.should raise_error(TypeError)
44  end
45
46  it "raises a TypeError if passed false" do
47    lambda do
48      ModuleSpecs::CountsMixin.public_method_defined?(false)
49    end.should raise_error(TypeError)
50  end
51
52  it "raises a TypeError if passed an object that does not defined #to_str" do
53    lambda do
54      ModuleSpecs::CountsMixin.public_method_defined?(mock('x'))
55    end.should raise_error(TypeError)
56  end
57
58  it "raises a TypeError if passed an object that defines #to_sym" do
59    sym = mock('symbol')
60    def sym.to_sym() :public_3 end
61
62    lambda do
63      ModuleSpecs::CountsMixin.public_method_defined?(sym)
64    end.should raise_error(TypeError)
65  end
66
67  it "calls #to_str to convert an Object" do
68    str = mock('public_3')
69    def str.to_str() 'public_3' end
70    ModuleSpecs::CountsMixin.public_method_defined?(str).should == true
71  end
72end
73