Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into 1.8.7
Browse files Browse the repository at this point in the history
Conflicts:
	kernel/common/comparable.rb
	spec/ruby/core/comparable/equal_value_spec.rb
  • Loading branch information
brixen committed Jun 26, 2015
2 parents bbf6cfa + 291c7c7 commit 5c39cd1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
16 changes: 9 additions & 7 deletions kernel/common/comparable.rb
Expand Up @@ -2,14 +2,16 @@ module Comparable
def ==(other)
return true if equal?(other)

begin
unless comp = (self <=> other)
return nil
end
return if Thread.detect_recursion(self, other) do
begin
unless comp = (self <=> other)
return
end

return Comparable.compare_int(comp) == 0
rescue StandardError, SystemStackError
return nil
return Comparable.compare_int(comp) == 0
rescue StandardError
return
end
end
end

Expand Down
71 changes: 39 additions & 32 deletions spec/ruby/core/comparable/equal_value_spec.rb
Expand Up @@ -32,28 +32,14 @@
(@a == @b).should be_false
end

ruby_version_is ""..."1.9" do
it "returns nil if calling #<=> on self returns nil" do
@a.should_receive(:<=>).any_number_of_times.and_return(nil)
(@a == @b).should be_nil
end

it "returns nil if calling #<=> on self returns a non-Integer" do
@a.should_receive(:<=>).any_number_of_times.and_return("abc")
(@a == @b).should be_nil
end
it "returns nil if calling #<=> on self returns nil" do
@a.should_receive(:<=>).any_number_of_times.and_return(nil)
(@a == @b).should be_nil
end

ruby_version_is "1.9" do
it "returns false if calling #<=> on self returns nil" do
@a.should_receive(:<=>).any_number_of_times.and_return(nil)
(@a == @b).should be_false
end

it "returns false if calling #<=> on self returns a non-Integer" do
@a.should_receive(:<=>).any_number_of_times.and_return("abc")
(@a == @b).should be_false
end
it "returns nil if calling #<=> on self returns a non-Integer" do
@a.should_receive(:<=>).any_number_of_times.and_return("abc")
(@a == @b).should be_nil
end

describe "when calling #<=> on self raises an Exception" do
Expand All @@ -73,19 +59,40 @@ def @raise_not_standard_error.<=>(b) raise SyntaxError, "test"; end
lambda { @raise_not_standard_error == @b }.should raise_error(@not_standard_error)
end

ruby_version_is ""..."1.9" do
it "returns nil if #<=> raises a StandardError" do
(@raise_standard_error == @b).should be_nil
(@raise_sub_standard_error == @b).should be_nil
end
it "returns nil if #<=> raises a StandardError" do
(@raise_standard_error == @b).should be_nil
(@raise_sub_standard_error == @b).should be_nil
end
end

context "when #<=> is not defined" do
before :each do
@a = ComparableSpecs::WithoutCompareDefined.new
@b = ComparableSpecs::WithoutCompareDefined.new
end

it "returns true for identical objects" do
@a.should == @a
end

it "returns false and does not recurse infinitely" do
@a.should_not == @b
end
end

context "when #<=> calls super" do
before :each do
@a = ComparableSpecs::CompareCallingSuper.new
@b = ComparableSpecs::CompareCallingSuper.new
end

it "returns true for identical objects" do
@a.should == @a
end

ruby_version_is "1.9" do
# Behaviour confirmed by MRI test suite
it "returns false if #<=> raises a StandardError" do
(@raise_standard_error == @b).should be_false
(@raise_sub_standard_error == @b).should be_false
end
it "calls the defined #<=> only once for different objects" do
@a.should_not == @b
@a.calls.should == 1
end
end
end
end
19 changes: 19 additions & 0 deletions spec/ruby/core/comparable/fixtures/classes.rb
Expand Up @@ -12,4 +12,23 @@ def <=>(other)
self.value <=> other.value
end
end

class WithoutCompareDefined
include Comparable
end

class CompareCallingSuper
include Comparable

attr_reader :calls

def initialize
@calls = 0
end

def <=>(other)
@calls += 1
super(other)
end
end
end
10 changes: 10 additions & 0 deletions spec/ruby/core/marshal/shared/load.rb
Expand Up @@ -237,6 +237,16 @@
Marshal.send(@method, "\004\bI[\b\"\0065I\"\twell\006:\t@fooi\017\"\ahi\006:\t@mix@\a").should ==
obj
end

it "loads an extended Array object containing a user-marshaled object" do
obj = [UserMarshal.new, UserMarshal.new].extend(Meths)
new_obj = Marshal.send(@method, "\x04\be:\nMeths[\ao:\x10UserMarshal\x06:\n@dataI\"\nstuff\x06:\x06ETo;\x06\x06;\aI\"\nstuff\x06;\bT")

new_obj.should == obj
obj_ancestors = class << obj; ancestors[1..-1]; end
new_obj_ancestors = class << new_obj; ancestors[1..-1]; end
obj_ancestors.should == new_obj_ancestors
end
end

describe "for a Hash" do
Expand Down

0 comments on commit 5c39cd1

Please sign in to comment.