summaryrefslogtreecommitdiff
path: root/spec/ruby/core/module/define_method_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/module/define_method_spec.rb')
-rw-r--r--spec/ruby/core/module/define_method_spec.rb117
1 files changed, 60 insertions, 57 deletions
diff --git a/spec/ruby/core/module/define_method_spec.rb b/spec/ruby/core/module/define_method_spec.rb
index e04bb87ceb..f838e2b85f 100644
--- a/spec/ruby/core/module/define_method_spec.rb
+++ b/spec/ruby/core/module/define_method_spec.rb
@@ -12,7 +12,7 @@ describe "passed { |a, b = 1| } creates a method that" do
end
it "raises an ArgumentError when passed zero arguments" do
- -> { @klass.new.m }.should raise_error(ArgumentError)
+ -> { @klass.new.m }.should.raise(ArgumentError)
end
it "has a default value for b when passed one argument" do
@@ -24,7 +24,7 @@ describe "passed { |a, b = 1| } creates a method that" do
end
it "raises an ArgumentError when passed three arguments" do
- -> { @klass.new.m(1, 2, 3) }.should raise_error(ArgumentError)
+ -> { @klass.new.m(1, 2, 3) }.should.raise(ArgumentError)
end
end
@@ -47,7 +47,7 @@ describe "Module#define_method when given an UnboundMethod" do
end
define_method(:another_test_method, instance_method(:test_method))
end
- klass.new.should have_method(:another_test_method)
+ klass.new.should.respond_to?(:another_test_method)
end
describe "defining a method on a singleton class" do
@@ -83,7 +83,7 @@ describe "Module#define_method when given an UnboundMethod" do
define_method :piggy, instance_method(:ziggy)
end
- -> { foo.new.ziggy }.should raise_error(NoMethodError)
+ -> { foo.new.ziggy }.should.raise(NoMethodError)
foo.new.piggy.should == 'piggy'
end
end
@@ -115,8 +115,8 @@ describe "Module#define_method when name is not a special private name" do
define_method(:baz, ModuleSpecs::EmptyFooMethod)
end
- klass.should have_public_instance_method(:bar)
- klass.should have_private_instance_method(:baz)
+ klass.public_instance_methods(false).should.include?(:bar)
+ klass.private_instance_methods(false).should.include?(:baz)
end
end
@@ -129,8 +129,8 @@ describe "Module#define_method when name is not a special private name" do
klass.send(:define_method, :baz, ModuleSpecs::EmptyFooMethod)
end
- klass.should have_public_instance_method(:bar)
- klass.should have_public_instance_method(:baz)
+ klass.public_instance_methods(false).should.include?(:bar)
+ klass.public_instance_methods(false).should.include?(:baz)
end
end
@@ -155,8 +155,8 @@ describe "Module#define_method when name is not a special private name" do
define_method(:baz) {}
end
- klass.should have_public_instance_method(:bar)
- klass.should have_private_instance_method(:baz)
+ klass.public_instance_methods(false).should.include?(:bar)
+ klass.private_instance_methods(false).should.include?(:baz)
end
end
@@ -169,8 +169,8 @@ describe "Module#define_method when name is not a special private name" do
klass.send(:define_method, :baz) {}
end
- klass.should have_public_instance_method(:bar)
- klass.should have_public_instance_method(:baz)
+ klass.public_instance_methods(false).should.include?(:bar)
+ klass.public_instance_methods(false).should.include?(:baz)
end
end
end
@@ -182,7 +182,7 @@ describe "Module#define_method when name is :initialize" do
klass = Class.new do
define_method(:initialize) { }
end
- klass.should have_private_instance_method(:initialize)
+ klass.private_instance_methods(false).should.include?(:initialize)
end
end
@@ -193,7 +193,7 @@ describe "Module#define_method when name is :initialize" do
end
define_method(:initialize, instance_method(:test_method))
end
- klass.should have_private_instance_method(:initialize)
+ klass.private_instance_methods(false).should.include?(:initialize)
end
end
end
@@ -233,11 +233,11 @@ describe "Module#define_method" do
it "raises TypeError if name cannot converted to String" do
-> {
Class.new { define_method(1001, -> {}) }
- }.should raise_error(TypeError, /is not a symbol nor a string/)
+ }.should.raise(TypeError, /is not a symbol nor a string/)
-> {
Class.new { define_method([], -> {}) }
- }.should raise_error(TypeError, /is not a symbol nor a string/)
+ }.should.raise(TypeError, /is not a symbol nor a string/)
end
it "converts non-String name to String with #to_str" do
@@ -254,21 +254,21 @@ describe "Module#define_method" do
-> {
Class.new { define_method(obj, -> {}) }
- }.should raise_error(TypeError, /can't convert Object to String/)
+ }.should raise_consistent_error(TypeError, /can't convert Object into String/)
end
it "raises a TypeError when the given method is no Method/Proc" do
-> {
Class.new { define_method(:test, "self") }
- }.should raise_error(TypeError, "wrong argument type String (expected Proc/Method/UnboundMethod)")
+ }.should.raise(TypeError, "wrong argument type String (expected Proc/Method/UnboundMethod)")
-> {
Class.new { define_method(:test, 1234) }
- }.should raise_error(TypeError, "wrong argument type Integer (expected Proc/Method/UnboundMethod)")
+ }.should.raise(TypeError, "wrong argument type Integer (expected Proc/Method/UnboundMethod)")
-> {
Class.new { define_method(:test, nil) }
- }.should raise_error(TypeError, "wrong argument type NilClass (expected Proc/Method/UnboundMethod)")
+ }.should.raise(TypeError, "wrong argument type NilClass (expected Proc/Method/UnboundMethod)")
end
it "uses provided Method/Proc even if block is specified" do
@@ -284,7 +284,7 @@ describe "Module#define_method" do
it "raises an ArgumentError when no block is given" do
-> {
Class.new { define_method(:test) }
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
it "does not use the caller block when no block is given" do
@@ -297,7 +297,7 @@ describe "Module#define_method" do
-> {
o.define(:foo) { raise "not used" }
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
it "does not change the arity check style of the original proc" do
@@ -307,13 +307,13 @@ describe "Module#define_method" do
end
obj = DefineMethodSpecClass.new
- -> { obj.proc_style_test :arg }.should raise_error(ArgumentError)
+ -> { obj.proc_style_test :arg }.should.raise(ArgumentError)
end
it "raises a FrozenError if frozen" do
-> {
Class.new { freeze; define_method(:foo) {} }
- }.should raise_error(FrozenError)
+ }.should.raise(FrozenError)
end
it "accepts a Method (still bound)" do
@@ -326,13 +326,13 @@ describe "Module#define_method" do
o = DefineMethodSpecClass.new
o.data = :foo
m = o.method(:inspect_data)
- m.should be_an_instance_of(Method)
+ m.should.instance_of?(Method)
klass = Class.new(DefineMethodSpecClass)
klass.send(:define_method,:other_inspect, m)
c = klass.new
c.data = :bar
c.other_inspect.should == "data is bar"
- ->{o.other_inspect}.should raise_error(NoMethodError)
+ ->{o.other_inspect}.should.raise(NoMethodError)
end
it "raises a TypeError when a Method from a singleton class is defined on another class" do
@@ -346,7 +346,7 @@ describe "Module#define_method" do
-> {
Class.new { define_method :bar, m }
- }.should raise_error(TypeError, /can't bind singleton method to a different class/)
+ }.should.raise(TypeError, /can't bind singleton method to a different class/)
end
it "raises a TypeError when a Method from one class is defined on an unrelated class" do
@@ -358,7 +358,7 @@ describe "Module#define_method" do
-> {
Class.new { define_method :bar, m }
- }.should raise_error(TypeError)
+ }.should.raise(TypeError)
end
it "accepts an UnboundMethod from an attr_accessor method" do
@@ -370,7 +370,7 @@ describe "Module#define_method" do
o = DefineMethodSpecClass.new
DefineMethodSpecClass.send(:undef_method, :accessor_method)
- -> { o.accessor_method }.should raise_error(NoMethodError)
+ -> { o.accessor_method }.should.raise(NoMethodError)
DefineMethodSpecClass.send(:define_method, :accessor_method, m)
@@ -415,7 +415,7 @@ describe "Module#define_method" do
end
o = DefineMethodByProcClass.new
- o.proc_test.should be_true
+ o.proc_test.should == true
end
it "accepts a String method name" do
@@ -429,7 +429,7 @@ describe "Module#define_method" do
end
it "is a public method" do
- Module.should have_public_instance_method(:define_method)
+ Module.public_instance_methods(false).should.include?(:define_method)
end
it "returns its symbol" do
@@ -443,7 +443,7 @@ describe "Module#define_method" do
klass = Class.new {
define_method :bar, ModuleSpecs::UnboundMethodTest.instance_method(:foo)
}
- klass.new.should respond_to(:bar)
+ klass.new.should.respond_to?(:bar)
end
it "allows an UnboundMethod from a parent class to be defined on a child class" do
@@ -451,7 +451,7 @@ describe "Module#define_method" do
child = Class.new(parent) {
define_method :baz, parent.instance_method(:foo)
}
- child.new.should respond_to(:baz)
+ child.new.should.respond_to?(:baz)
end
it "allows an UnboundMethod from a module to be defined on another unrelated module" do
@@ -459,7 +459,7 @@ describe "Module#define_method" do
define_method :bar, ModuleSpecs::UnboundMethodTest.instance_method(:foo)
}
klass = Class.new { include mod }
- klass.new.should respond_to(:bar)
+ klass.new.should.respond_to?(:bar)
end
@@ -475,7 +475,10 @@ describe "Module#define_method" do
ParentClass = Class.new { define_method(:foo) { :bar } }
ChildClass = Class.new(ParentClass) { define_method(:foo) { :baz } }
ParentClass.send :define_method, :foo, ChildClass.instance_method(:foo)
- }.should raise_error(TypeError, /bind argument must be a subclass of ChildClass/)
+ }.should.raise(TypeError, /bind argument must be a subclass of ChildClass/)
+ ensure
+ Object.send(:remove_const, :ParentClass)
+ Object.send(:remove_const, :ChildClass)
end
it "raises a TypeError when an UnboundMethod from one class is defined on an unrelated class" do
@@ -483,7 +486,7 @@ describe "Module#define_method" do
DestinationClass = Class.new {
define_method :bar, ModuleSpecs::InstanceMeth.instance_method(:foo)
}
- }.should raise_error(TypeError, /bind argument must be a subclass of ModuleSpecs::InstanceMeth/)
+ }.should.raise(TypeError, /bind argument must be a subclass of ModuleSpecs::InstanceMeth/)
end
it "raises a TypeError when an UnboundMethod from a singleton class is defined on another class" do
@@ -497,7 +500,7 @@ describe "Module#define_method" do
-> {
Class.new { define_method :bar, m }
- }.should raise_error(TypeError, /can't bind singleton method to a different class/)
+ }.should.raise(TypeError, /can't bind singleton method to a different class/)
end
it "defines a new method with public visibility when a Method passed and the class/module of the context isn't equal to the receiver of #define_method" do
@@ -524,7 +527,7 @@ describe "Module#define_method" do
define_method(:bar, c.new.method(:foo))
end
- -> { object.bar }.should raise_error(NoMethodError)
+ -> { object.bar }.should.raise(NoMethodError)
end
end
@@ -541,11 +544,11 @@ describe "Module#define_method" do
end
it "raises an ArgumentError when passed one argument" do
- -> { @klass.new.m 1 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1 }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed two arguments" do
- -> { @klass.new.m 1, 2 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1, 2 }.should.raise(ArgumentError)
end
end
@@ -561,11 +564,11 @@ describe "Module#define_method" do
end
it "raises an ArgumentError when passed one argument" do
- -> { @klass.new.m 1 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1 }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed two arguments" do
- -> { @klass.new.m 1, 2 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1, 2 }.should.raise(ArgumentError)
end
end
@@ -577,15 +580,15 @@ describe "Module#define_method" do
end
it "raises an ArgumentError when passed zero arguments" do
- -> { @klass.new.m }.should raise_error(ArgumentError)
+ -> { @klass.new.m }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed zero arguments and a block" do
- -> { @klass.new.m { :computed } }.should raise_error(ArgumentError)
+ -> { @klass.new.m { :computed } }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed two arguments" do
- -> { @klass.new.m 1, 2 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1, 2 }.should.raise(ArgumentError)
end
it "receives the value passed as the argument when passed one argument" do
@@ -601,15 +604,15 @@ describe "Module#define_method" do
end
it "raises an ArgumentError when passed zero arguments" do
- -> { @klass.new.m }.should raise_error(ArgumentError)
+ -> { @klass.new.m }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed zero arguments and a block" do
- -> { @klass.new.m { :computed } }.should raise_error(ArgumentError)
+ -> { @klass.new.m { :computed } }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed two arguments" do
- -> { @klass.new.m 1, 2 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1, 2 }.should.raise(ArgumentError)
end
it "receives the value passed as the argument when passed one argument" do
@@ -651,7 +654,7 @@ describe "Module#define_method" do
end
it "raises an ArgumentError when passed zero arguments" do
- -> { @klass.new.m }.should raise_error(ArgumentError)
+ -> { @klass.new.m }.should.raise(ArgumentError)
end
it "returns the value computed by the block when passed one argument" do
@@ -679,19 +682,19 @@ describe "Module#define_method" do
end
it "raises an ArgumentError when passed zero arguments" do
- -> { @klass.new.m }.should raise_error(ArgumentError)
+ -> { @klass.new.m }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed one argument" do
- -> { @klass.new.m 1 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1 }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed one argument and a block" do
- -> { @klass.new.m(1) { } }.should raise_error(ArgumentError)
+ -> { @klass.new.m(1) { } }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed three arguments" do
- -> { @klass.new.m 1, 2, 3 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1, 2, 3 }.should.raise(ArgumentError)
end
end
@@ -703,15 +706,15 @@ describe "Module#define_method" do
end
it "raises an ArgumentError when passed zero arguments" do
- -> { @klass.new.m }.should raise_error(ArgumentError)
+ -> { @klass.new.m }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed one argument" do
- -> { @klass.new.m 1 }.should raise_error(ArgumentError)
+ -> { @klass.new.m 1 }.should.raise(ArgumentError)
end
it "raises an ArgumentError when passed one argument and a block" do
- -> { @klass.new.m(1) { } }.should raise_error(ArgumentError)
+ -> { @klass.new.m(1) { } }.should.raise(ArgumentError)
end
it "receives an empty array as the third argument when passed two arguments" do
@@ -792,7 +795,7 @@ describe "Module#define_method when passed a Proc object" do
o = c.new
o.test
- o.should_not have_method :nested_method_in_proc_for_define_method
+ o.should_not.respond_to? :nested_method_in_proc_for_define_method
t.new.nested_method_in_proc_for_define_method.should == 42
end