Skip to content

Commit 0bb2154

Browse files
committed
Check exception messages for define_method errors
* To ensure a useful error message is given to the user.
1 parent f4af1b2 commit 0bb2154

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

spec/ruby/core/module/define_method_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def foo
281281

282282
lambda {
283283
Class.new { define_method :bar, m }
284-
}.should raise_error(TypeError)
284+
}.should raise_error(TypeError, /can't bind singleton method to a different class/)
285285
end
286286

287287
it "raises a TypeError when a Method from one class is defined on an unrelated class" do
@@ -409,15 +409,29 @@ class DefineMethodSpecClass
409409
ParentClass = Class.new { define_method(:foo) { :bar } }
410410
ChildClass = Class.new(ParentClass) { define_method(:foo) { :baz } }
411411
ParentClass.send :define_method, :foo, ChildClass.instance_method(:foo)
412-
}.should raise_error(TypeError)
412+
}.should raise_error(TypeError, /bind argument must be a subclass of ChildClass/)
413413
end
414414

415415
it "raises a TypeError when an UnboundMethod from one class is defined on an unrelated class" do
416416
lambda {
417417
DestinationClass = Class.new {
418418
define_method :bar, ModuleSpecs::InstanceMeth.instance_method(:foo)
419419
}
420-
}.should raise_error(TypeError)
420+
}.should raise_error(TypeError, /bind argument must be a subclass of ModuleSpecs::InstanceMeth/)
421+
end
422+
423+
it "raises a TypeError when an UnboundMethod from a singleton class is defined on another class" do
424+
c = Class.new do
425+
class << self
426+
def foo
427+
end
428+
end
429+
end
430+
m = c.method(:foo).unbind
431+
432+
lambda {
433+
Class.new { define_method :bar, m }
434+
}.should raise_error(TypeError, /can't bind singleton method to a different class/)
421435
end
422436
end
423437

0 commit comments

Comments
 (0)