mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Fix behavior of Comparable methods
when <=> return nil
This commit is contained in:
+4
-4
@@ -13,7 +13,7 @@ module Comparable
|
||||
def < other
|
||||
cmp = self <=> other
|
||||
if cmp.nil?
|
||||
false
|
||||
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
|
||||
elsif cmp < 0
|
||||
true
|
||||
else
|
||||
@@ -30,7 +30,7 @@ module Comparable
|
||||
def <= other
|
||||
cmp = self <=> other
|
||||
if cmp.nil?
|
||||
false
|
||||
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
|
||||
elsif cmp <= 0
|
||||
true
|
||||
else
|
||||
@@ -62,7 +62,7 @@ module Comparable
|
||||
def > other
|
||||
cmp = self <=> other
|
||||
if cmp.nil?
|
||||
false
|
||||
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
|
||||
elsif cmp > 0
|
||||
true
|
||||
else
|
||||
@@ -79,7 +79,7 @@ module Comparable
|
||||
def >= other
|
||||
cmp = self <=> other
|
||||
if cmp.nil?
|
||||
false
|
||||
raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
|
||||
elsif cmp >= 0
|
||||
true
|
||||
else
|
||||
|
||||
+20
-12
@@ -3,22 +3,26 @@ assert('Comparable#<', '15.3.3.2.1') do
|
||||
class Foo
|
||||
include Comparable
|
||||
def <=>(x)
|
||||
0
|
||||
x
|
||||
end
|
||||
end
|
||||
|
||||
assert_false(Foo.new < Foo.new)
|
||||
assert_false(Foo.new < 0)
|
||||
assert_false(Foo.new < 1)
|
||||
assert_true(Foo.new < -1)
|
||||
assert_raise(ArgumentError){ Foo.new < nil }
|
||||
end
|
||||
|
||||
assert('Comparable#<=', '15.3.3.2.2') do
|
||||
class Foo
|
||||
include Comparable
|
||||
def <=>(x)
|
||||
0
|
||||
x
|
||||
end
|
||||
end
|
||||
|
||||
assert_true(Foo.new <= Foo.new)
|
||||
assert_true(Foo.new <= 0)
|
||||
assert_false(Foo.new <= 1)
|
||||
assert_true(Foo.new <= -1)
|
||||
assert_raise(ArgumentError){ Foo.new <= nil }
|
||||
end
|
||||
|
||||
assert('Comparable#==', '15.3.3.2.3') do
|
||||
@@ -36,22 +40,26 @@ assert('Comparable#>', '15.3.3.2.4') do
|
||||
class Foo
|
||||
include Comparable
|
||||
def <=>(x)
|
||||
0
|
||||
x
|
||||
end
|
||||
end
|
||||
|
||||
assert_false(Foo.new > Foo.new)
|
||||
assert_false(Foo.new > 0)
|
||||
assert_true(Foo.new > 1)
|
||||
assert_false(Foo.new > -1)
|
||||
assert_raise(ArgumentError){ Foo.new > nil }
|
||||
end
|
||||
|
||||
assert('Comparable#>=', '15.3.3.2.5') do
|
||||
class Foo
|
||||
include Comparable
|
||||
def <=>(x)
|
||||
0
|
||||
x
|
||||
end
|
||||
end
|
||||
|
||||
assert_true(Foo.new >= Foo.new)
|
||||
assert_true(Foo.new >= 0)
|
||||
assert_true(Foo.new >= 1)
|
||||
assert_false(Foo.new >= -1)
|
||||
assert_raise(ArgumentError){ Foo.new >= nil }
|
||||
end
|
||||
|
||||
assert('Comparable#between?', '15.3.3.2.6') do
|
||||
|
||||
Reference in New Issue
Block a user