Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5c39cd1

Browse files
committedJun 26, 2015
Merge remote-tracking branch 'origin' into 1.8.7
Conflicts: kernel/common/comparable.rb spec/ruby/core/comparable/equal_value_spec.rb
2 parents bbf6cfa + 291c7c7 commit 5c39cd1

File tree

4 files changed

+77
-39
lines changed

4 files changed

+77
-39
lines changed
 

‎kernel/common/comparable.rb

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ module Comparable
22
def ==(other)
33
return true if equal?(other)
44

5-
begin
6-
unless comp = (self <=> other)
7-
return nil
8-
end
5+
return if Thread.detect_recursion(self, other) do
6+
begin
7+
unless comp = (self <=> other)
8+
return
9+
end
910

10-
return Comparable.compare_int(comp) == 0
11-
rescue StandardError, SystemStackError
12-
return nil
11+
return Comparable.compare_int(comp) == 0
12+
rescue StandardError
13+
return
14+
end
1315
end
1416
end
1517

‎spec/ruby/core/comparable/equal_value_spec.rb

+39-32
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,14 @@
3232
(@a == @b).should be_false
3333
end
3434

35-
ruby_version_is ""..."1.9" do
36-
it "returns nil if calling #<=> on self returns nil" do
37-
@a.should_receive(:<=>).any_number_of_times.and_return(nil)
38-
(@a == @b).should be_nil
39-
end
40-
41-
it "returns nil if calling #<=> on self returns a non-Integer" do
42-
@a.should_receive(:<=>).any_number_of_times.and_return("abc")
43-
(@a == @b).should be_nil
44-
end
35+
it "returns nil if calling #<=> on self returns nil" do
36+
@a.should_receive(:<=>).any_number_of_times.and_return(nil)
37+
(@a == @b).should be_nil
4538
end
4639

47-
ruby_version_is "1.9" do
48-
it "returns false if calling #<=> on self returns nil" do
49-
@a.should_receive(:<=>).any_number_of_times.and_return(nil)
50-
(@a == @b).should be_false
51-
end
52-
53-
it "returns false if calling #<=> on self returns a non-Integer" do
54-
@a.should_receive(:<=>).any_number_of_times.and_return("abc")
55-
(@a == @b).should be_false
56-
end
40+
it "returns nil if calling #<=> on self returns a non-Integer" do
41+
@a.should_receive(:<=>).any_number_of_times.and_return("abc")
42+
(@a == @b).should be_nil
5743
end
5844

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

76-
ruby_version_is ""..."1.9" do
77-
it "returns nil if #<=> raises a StandardError" do
78-
(@raise_standard_error == @b).should be_nil
79-
(@raise_sub_standard_error == @b).should be_nil
80-
end
62+
it "returns nil if #<=> raises a StandardError" do
63+
(@raise_standard_error == @b).should be_nil
64+
(@raise_sub_standard_error == @b).should be_nil
65+
end
66+
end
67+
68+
context "when #<=> is not defined" do
69+
before :each do
70+
@a = ComparableSpecs::WithoutCompareDefined.new
71+
@b = ComparableSpecs::WithoutCompareDefined.new
72+
end
73+
74+
it "returns true for identical objects" do
75+
@a.should == @a
76+
end
77+
78+
it "returns false and does not recurse infinitely" do
79+
@a.should_not == @b
80+
end
81+
end
82+
83+
context "when #<=> calls super" do
84+
before :each do
85+
@a = ComparableSpecs::CompareCallingSuper.new
86+
@b = ComparableSpecs::CompareCallingSuper.new
87+
end
88+
89+
it "returns true for identical objects" do
90+
@a.should == @a
8191
end
8292

83-
ruby_version_is "1.9" do
84-
# Behaviour confirmed by MRI test suite
85-
it "returns false if #<=> raises a StandardError" do
86-
(@raise_standard_error == @b).should be_false
87-
(@raise_sub_standard_error == @b).should be_false
88-
end
93+
it "calls the defined #<=> only once for different objects" do
94+
@a.should_not == @b
95+
@a.calls.should == 1
8996
end
9097
end
91-
end
98+
end

‎spec/ruby/core/comparable/fixtures/classes.rb

+19
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,23 @@ def <=>(other)
1212
self.value <=> other.value
1313
end
1414
end
15+
16+
class WithoutCompareDefined
17+
include Comparable
18+
end
19+
20+
class CompareCallingSuper
21+
include Comparable
22+
23+
attr_reader :calls
24+
25+
def initialize
26+
@calls = 0
27+
end
28+
29+
def <=>(other)
30+
@calls += 1
31+
super(other)
32+
end
33+
end
1534
end

‎spec/ruby/core/marshal/shared/load.rb

+10
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@
237237
Marshal.send(@method, "\004\bI[\b\"\0065I\"\twell\006:\t@fooi\017\"\ahi\006:\t@mix@\a").should ==
238238
obj
239239
end
240+
241+
it "loads an extended Array object containing a user-marshaled object" do
242+
obj = [UserMarshal.new, UserMarshal.new].extend(Meths)
243+
new_obj = Marshal.send(@method, "\x04\be:\nMeths[\ao:\x10UserMarshal\x06:\n@dataI\"\nstuff\x06:\x06ETo;\x06\x06;\aI\"\nstuff\x06;\bT")
244+
245+
new_obj.should == obj
246+
obj_ancestors = class << obj; ancestors[1..-1]; end
247+
new_obj_ancestors = class << new_obj; ancestors[1..-1]; end
248+
obj_ancestors.should == new_obj_ancestors
249+
end
240250
end
241251

242252
describe "for a Hash" do

0 commit comments

Comments
 (0)
Please sign in to comment.