|
1 | 1 | require_relative '../../spec_helper' |
2 | | -require_relative 'shared/eql' |
| 2 | +require_relative 'fixtures/classes' |
3 | 3 |
|
4 | 4 | describe "Method#eql?" do |
5 | | - it_behaves_like :method_equal, :eql? |
| 5 | + before :each do |
| 6 | + @m = MethodSpecs::Methods.new |
| 7 | + @m_foo = @m.method(:foo) |
| 8 | + @m2 = MethodSpecs::Methods.new |
| 9 | + @a = MethodSpecs::A.new |
| 10 | + end |
| 11 | + |
| 12 | + it "returns true if methods are the same" do |
| 13 | + m2 = @m.method(:foo) |
| 14 | + |
| 15 | + @m_foo.eql?(@m_foo).should == true |
| 16 | + @m_foo.eql?(m2).should == true |
| 17 | + end |
| 18 | + |
| 19 | + it "returns true on aliased methods" do |
| 20 | + m_bar = @m.method(:bar) |
| 21 | + |
| 22 | + m_bar.eql?(@m_foo).should == true |
| 23 | + end |
| 24 | + |
| 25 | + it "returns true if the two core methods are aliases" do |
| 26 | + s = "hello" |
| 27 | + a = s.method(:size) |
| 28 | + b = s.method(:length) |
| 29 | + a.eql?(b).should == true |
| 30 | + end |
| 31 | + |
| 32 | + it "returns false on a method which is neither aliased nor the same method" do |
| 33 | + m2 = @m.method(:zero) |
| 34 | + |
| 35 | + @m_foo.eql?(m2).should == false |
| 36 | + end |
| 37 | + |
| 38 | + it "returns false for a method which is not bound to the same object" do |
| 39 | + m2_foo = @m2.method(:foo) |
| 40 | + a_baz = @a.method(:baz) |
| 41 | + |
| 42 | + @m_foo.eql?(m2_foo).should == false |
| 43 | + @m_foo.eql?(a_baz).should == false |
| 44 | + end |
| 45 | + |
| 46 | + it "returns false if the two methods are bound to the same object but were defined independently" do |
| 47 | + m2 = @m.method(:same_as_foo) |
| 48 | + @m_foo.eql?(m2).should == false |
| 49 | + end |
| 50 | + |
| 51 | + it "returns true if a method was defined using the other one" do |
| 52 | + MethodSpecs::Methods.send :define_method, :defined_foo, MethodSpecs::Methods.instance_method(:foo) |
| 53 | + m2 = @m.method(:defined_foo) |
| 54 | + @m_foo.eql?(m2).should == true |
| 55 | + end |
| 56 | + |
| 57 | + it "returns false if comparing a method defined via define_method and def" do |
| 58 | + defn = @m.method(:zero) |
| 59 | + defined = @m.method(:zero_defined_method) |
| 60 | + |
| 61 | + defn.eql?(defined).should == false |
| 62 | + defined.eql?(defn).should == false |
| 63 | + end |
| 64 | + |
| 65 | + describe 'missing methods' do |
| 66 | + it "returns true for the same method missing" do |
| 67 | + miss1 = @m.method(:handled_via_method_missing) |
| 68 | + miss1bis = @m.method(:handled_via_method_missing) |
| 69 | + miss2 = @m.method(:also_handled) |
| 70 | + |
| 71 | + miss1.eql?(miss1bis).should == true |
| 72 | + miss1.eql?(miss2).should == false |
| 73 | + end |
| 74 | + |
| 75 | + it 'calls respond_to_missing? with true to include private methods' do |
| 76 | + @m.should_receive(:respond_to_missing?).with(:some_missing_method, true).and_return(true) |
| 77 | + @m.method(:some_missing_method) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + it "returns false if the two methods are bound to different objects, have the same names, and identical bodies" do |
| 82 | + a = MethodSpecs::Eql.instance_method(:same_body) |
| 83 | + b = MethodSpecs::Eql2.instance_method(:same_body) |
| 84 | + a.eql?(b).should == false |
| 85 | + end |
| 86 | + |
| 87 | + it "returns false if the argument is not a Method object" do |
| 88 | + String.instance_method(:size).eql?(7).should == false |
| 89 | + end |
| 90 | + |
| 91 | + it "returns false if the argument is an unbound version of self" do |
| 92 | + method(:load).eql?(method(:load).unbind).should == false |
| 93 | + end |
6 | 94 | end |
0 commit comments