diff options
Diffstat (limited to 'spec/ruby/core/method')
| -rw-r--r-- | spec/ruby/core/method/call_spec.rb | 51 | ||||
| -rw-r--r-- | spec/ruby/core/method/case_compare_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/method/element_reference_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/method/eql_spec.rb | 5 | ||||
| -rw-r--r-- | spec/ruby/core/method/equal_value_spec.rb | 92 | ||||
| -rw-r--r-- | spec/ruby/core/method/inspect_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/core/method/shared/call.rb | 51 | ||||
| -rw-r--r-- | spec/ruby/core/method/shared/eql.rb | 94 |
8 files changed, 151 insertions, 161 deletions
diff --git a/spec/ruby/core/method/call_spec.rb b/spec/ruby/core/method/call_spec.rb index 6d997325fa..cb11545e91 100644 --- a/spec/ruby/core/method/call_spec.rb +++ b/spec/ruby/core/method/call_spec.rb @@ -1,7 +1,54 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/call' describe "Method#call" do - it_behaves_like :method_call, :call + it "invokes the method with the specified arguments, returning the method's return value" do + m = 12.method("+") + m.call(3).should == 15 + m.call(20).should == 32 + + m = MethodSpecs::Methods.new.method(:attr=) + m.call(42).should == 42 + end + + it "raises an ArgumentError when given incorrect number of arguments" do + -> { + MethodSpecs::Methods.new.method(:two_req).call(1, 2, 3) + }.should.raise(ArgumentError) + -> { + MethodSpecs::Methods.new.method(:two_req).call(1) + }.should.raise(ArgumentError) + end + + describe "for a Method generated by respond_to_missing?" do + it "invokes method_missing with the specified arguments and returns the result" do + @m = MethodSpecs::Methods.new + meth = @m.method(:handled_via_method_missing) + meth.call(:argument).should == [:argument] + end + + it "invokes method_missing with the method name and the specified arguments" do + @m = MethodSpecs::Methods.new + meth = @m.method(:handled_via_method_missing) + + @m.should_receive(:method_missing).with(:handled_via_method_missing, :argument) + meth.call(:argument) + end + + it "invokes method_missing dynamically" do + @m = MethodSpecs::Methods.new + meth = @m.method(:handled_via_method_missing) + + def @m.method_missing(*); :changed; end + meth.call(:argument).should == :changed + end + + it "does not call the original method name even if it now exists" do + @m = MethodSpecs::Methods.new + meth = @m.method(:handled_via_method_missing) + + def @m.handled_via_method_missing(*); :not_called; end + meth.call(:argument).should == [:argument] + end + end end diff --git a/spec/ruby/core/method/case_compare_spec.rb b/spec/ruby/core/method/case_compare_spec.rb index a78953e8ad..771fea1ce5 100644 --- a/spec/ruby/core/method/case_compare_spec.rb +++ b/spec/ruby/core/method/case_compare_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/call' describe "Method#===" do - it_behaves_like :method_call, :=== + it "is an alias of Method#call" do + Method.instance_method(:===).should == Method.instance_method(:call) + end end diff --git a/spec/ruby/core/method/element_reference_spec.rb b/spec/ruby/core/method/element_reference_spec.rb index aa6c54d1cb..65c13cf32b 100644 --- a/spec/ruby/core/method/element_reference_spec.rb +++ b/spec/ruby/core/method/element_reference_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/call' describe "Method#[]" do - it_behaves_like :method_call, :[] + it "is an alias of Method#call" do + Method.instance_method(:[]).should == Method.instance_method(:call) + end end diff --git a/spec/ruby/core/method/eql_spec.rb b/spec/ruby/core/method/eql_spec.rb index b97c9e4db0..81fd086bd5 100644 --- a/spec/ruby/core/method/eql_spec.rb +++ b/spec/ruby/core/method/eql_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/eql' describe "Method#eql?" do - it_behaves_like :method_equal, :eql? + it "is an alias of Method#==" do + Method.instance_method(:eql?).should == Method.instance_method(:==) + end end diff --git a/spec/ruby/core/method/equal_value_spec.rb b/spec/ruby/core/method/equal_value_spec.rb index 0431d0c5f6..ca9ef9f108 100644 --- a/spec/ruby/core/method/equal_value_spec.rb +++ b/spec/ruby/core/method/equal_value_spec.rb @@ -1,6 +1,94 @@ require_relative '../../spec_helper' -require_relative 'shared/eql' +require_relative 'fixtures/classes' describe "Method#==" do - it_behaves_like :method_equal, :== + before :each do + @m = MethodSpecs::Methods.new + @m_foo = @m.method(:foo) + @m2 = MethodSpecs::Methods.new + @a = MethodSpecs::A.new + end + + it "returns true if methods are the same" do + m2 = @m.method(:foo) + + (@m_foo == @m_foo).should == true + (@m_foo == m2).should == true + end + + it "returns true on aliased methods" do + m_bar = @m.method(:bar) + + (m_bar == @m_foo).should == true + end + + it "returns true if the two core methods are aliases" do + s = "hello" + a = s.method(:size) + b = s.method(:length) + (a == b).should == true + end + + it "returns false on a method which is neither aliased nor the same method" do + m2 = @m.method(:zero) + + (@m_foo == m2).should == false + end + + it "returns false for a method which is not bound to the same object" do + m2_foo = @m2.method(:foo) + a_baz = @a.method(:baz) + + (@m_foo == m2_foo).should == false + (@m_foo == a_baz).should == false + end + + it "returns false if the two methods are bound to the same object but were defined independently" do + m2 = @m.method(:same_as_foo) + (@m_foo == m2).should == false + end + + it "returns true if a method was defined using the other one" do + MethodSpecs::Methods.send :define_method, :defined_foo, MethodSpecs::Methods.instance_method(:foo) + m2 = @m.method(:defined_foo) + (@m_foo == m2).should == true + end + + it "returns false if comparing a method defined via define_method and def" do + defn = @m.method(:zero) + defined = @m.method(:zero_defined_method) + + (defn == defined).should == false + (defined == defn).should == false + end + + describe 'missing methods' do + it "returns true for the same method missing" do + miss1 = @m.method(:handled_via_method_missing) + miss1bis = @m.method(:handled_via_method_missing) + miss2 = @m.method(:also_handled) + + (miss1 == miss1bis).should == true + (miss1 == miss2).should == false + end + + it 'calls respond_to_missing? with true to include private methods' do + @m.should_receive(:respond_to_missing?).with(:some_missing_method, true).and_return(true) + @m.method(:some_missing_method) + end + end + + it "returns false if the two methods are bound to different objects, have the same names, and identical bodies" do + a = MethodSpecs::Eql.instance_method(:same_body) + b = MethodSpecs::Eql2.instance_method(:same_body) + (a == b).should == false + end + + it "returns false if the argument is not a Method object" do + (String.instance_method(:size) == 7).should == false + end + + it "returns false if the argument is an unbound version of self" do + (method(:load) == method(:load).unbind).should == false + end end diff --git a/spec/ruby/core/method/inspect_spec.rb b/spec/ruby/core/method/inspect_spec.rb index 97ff2d8c11..5eb8850ff3 100644 --- a/spec/ruby/core/method/inspect_spec.rb +++ b/spec/ruby/core/method/inspect_spec.rb @@ -1,8 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/to_s' -require_relative 'shared/aliased_inspect' describe "Method#inspect" do - it_behaves_like :method_to_s, :inspect - it_behaves_like :method_to_s_aliased, :inspect, -> meth { meth } + it "is an alias of Method#to_s" do + Method.instance_method(:inspect).should == Method.instance_method(:to_s) + end end diff --git a/spec/ruby/core/method/shared/call.rb b/spec/ruby/core/method/shared/call.rb deleted file mode 100644 index 41ee2b06cb..0000000000 --- a/spec/ruby/core/method/shared/call.rb +++ /dev/null @@ -1,51 +0,0 @@ -describe :method_call, shared: true do - it "invokes the method with the specified arguments, returning the method's return value" do - m = 12.method("+") - m.send(@method, 3).should == 15 - m.send(@method, 20).should == 32 - - m = MethodSpecs::Methods.new.method(:attr=) - m.send(@method, 42).should == 42 - end - - it "raises an ArgumentError when given incorrect number of arguments" do - -> { - MethodSpecs::Methods.new.method(:two_req).send(@method, 1, 2, 3) - }.should.raise(ArgumentError) - -> { - MethodSpecs::Methods.new.method(:two_req).send(@method, 1) - }.should.raise(ArgumentError) - end - - describe "for a Method generated by respond_to_missing?" do - it "invokes method_missing with the specified arguments and returns the result" do - @m = MethodSpecs::Methods.new - meth = @m.method(:handled_via_method_missing) - meth.send(@method, :argument).should == [:argument] - end - - it "invokes method_missing with the method name and the specified arguments" do - @m = MethodSpecs::Methods.new - meth = @m.method(:handled_via_method_missing) - - @m.should_receive(:method_missing).with(:handled_via_method_missing, :argument) - meth.send(@method, :argument) - end - - it "invokes method_missing dynamically" do - @m = MethodSpecs::Methods.new - meth = @m.method(:handled_via_method_missing) - - def @m.method_missing(*); :changed; end - meth.send(@method, :argument).should == :changed - end - - it "does not call the original method name even if it now exists" do - @m = MethodSpecs::Methods.new - meth = @m.method(:handled_via_method_missing) - - def @m.handled_via_method_missing(*); :not_called; end - meth.send(@method, :argument).should == [:argument] - end - end -end diff --git a/spec/ruby/core/method/shared/eql.rb b/spec/ruby/core/method/shared/eql.rb deleted file mode 100644 index 3c340202b7..0000000000 --- a/spec/ruby/core/method/shared/eql.rb +++ /dev/null @@ -1,94 +0,0 @@ -require_relative '../../../spec_helper' -require_relative '../fixtures/classes' - -describe :method_equal, shared: true do - before :each do - @m = MethodSpecs::Methods.new - @m_foo = @m.method(:foo) - @m2 = MethodSpecs::Methods.new - @a = MethodSpecs::A.new - end - - it "returns true if methods are the same" do - m2 = @m.method(:foo) - - @m_foo.send(@method, @m_foo).should == true - @m_foo.send(@method, m2).should == true - end - - it "returns true on aliased methods" do - m_bar = @m.method(:bar) - - m_bar.send(@method, @m_foo).should == true - end - - it "returns true if the two core methods are aliases" do - s = "hello" - a = s.method(:size) - b = s.method(:length) - a.send(@method, b).should == true - end - - it "returns false on a method which is neither aliased nor the same method" do - m2 = @m.method(:zero) - - @m_foo.send(@method, m2).should == false - end - - it "returns false for a method which is not bound to the same object" do - m2_foo = @m2.method(:foo) - a_baz = @a.method(:baz) - - @m_foo.send(@method, m2_foo).should == false - @m_foo.send(@method, a_baz).should == false - end - - it "returns false if the two methods are bound to the same object but were defined independently" do - m2 = @m.method(:same_as_foo) - @m_foo.send(@method, m2).should == false - end - - it "returns true if a method was defined using the other one" do - MethodSpecs::Methods.send :define_method, :defined_foo, MethodSpecs::Methods.instance_method(:foo) - m2 = @m.method(:defined_foo) - @m_foo.send(@method, m2).should == true - end - - it "returns false if comparing a method defined via define_method and def" do - defn = @m.method(:zero) - defined = @m.method(:zero_defined_method) - - defn.send(@method, defined).should == false - defined.send(@method, defn).should == false - end - - describe 'missing methods' do - it "returns true for the same method missing" do - miss1 = @m.method(:handled_via_method_missing) - miss1bis = @m.method(:handled_via_method_missing) - miss2 = @m.method(:also_handled) - - miss1.send(@method, miss1bis).should == true - miss1.send(@method, miss2).should == false - end - - it 'calls respond_to_missing? with true to include private methods' do - @m.should_receive(:respond_to_missing?).with(:some_missing_method, true).and_return(true) - @m.method(:some_missing_method) - end - end - - it "returns false if the two methods are bound to different objects, have the same names, and identical bodies" do - a = MethodSpecs::Eql.instance_method(:same_body) - b = MethodSpecs::Eql2.instance_method(:same_body) - a.send(@method, b).should == false - end - - it "returns false if the argument is not a Method object" do - String.instance_method(:size).send(@method, 7).should == false - end - - it "returns false if the argument is an unbound version of self" do - method(:load).send(@method, method(:load).unbind).should == false - end -end |
