Skip to content

Commit 7750ecb

Browse files
committed
Add tests for Kernel#respond_to? without #respond_to_missing?
`#respond_to?` should work with unknown names even if `#respond_to_missing?` is not defined.
1 parent af627d6 commit 7750ecb

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

core/kernel/fixtures/classes.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ class B < A
180180
alias aliased_pub_method pub_method
181181
end
182182

183+
class BasicA < BasicObject
184+
define_method(:respond_to?, ::Kernel.instance_method(:respond_to?))
185+
186+
def pub_method; :public_method; end
187+
188+
def undefed_method; :undefed_method; end
189+
undef_method :undefed_method
190+
191+
protected
192+
def protected_method; :protected_method; end
193+
194+
private
195+
def private_method; :private_method; end
196+
end
197+
198+
class MissingA < A
199+
undef :respond_to_missing?
200+
end
201+
183202
class VisibilityChange
184203
class << self
185204
private :new

core/kernel/respond_to_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,31 @@ class KernelSpecs::Foo; def bar; 'done'; end; end
6969
KernelSpecs::Foo.new.respond_to?(:bar).should == true
7070
KernelSpecs::Foo.new.respond_to?(:invalid_and_silly_method_name).should == false
7171
end
72+
73+
context "if object does not have #respond_to_missing?" do
74+
it "returns true if object responds to the given public method" do
75+
KernelSpecs::BasicA.new.respond_to?(:pub_method).should == true
76+
KernelSpecs::MissingA.new.respond_to?(:pub_method).should == true
77+
end
78+
79+
it "returns false if object responds to the given protected method" do
80+
KernelSpecs::BasicA.new.respond_to?(:protected_method).should == false
81+
KernelSpecs::MissingA.new.respond_to?(:protected_method).should == false
82+
end
83+
84+
it "returns false if object responds to the given private method" do
85+
KernelSpecs::BasicA.new.respond_to?(:private_method).should == false
86+
KernelSpecs::MissingA.new.respond_to?(:private_method).should == false
87+
end
88+
89+
it "returns false if the given method was undefined" do
90+
KernelSpecs::BasicA.new.respond_to?(:undefed_method).should == false
91+
KernelSpecs::MissingA.new.respond_to?(:undefed_method).should == false
92+
end
93+
94+
it "returns false if the given method never existed" do
95+
KernelSpecs::BasicA.new.respond_to?(:invalid_and_silly_method_name).should == false
96+
KernelSpecs::MissingA.new.respond_to?(:invalid_and_silly_method_name).should == false
97+
end
98+
end
7299
end

0 commit comments

Comments
 (0)