diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-03-21 13:32:01 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-03-21 13:32:01 +0900 |
| commit | 607cf951fc874e4af9c9f0e8a631d4ae6fe9d836 (patch) | |
| tree | 1ef7d4abb8b08a9687de5c2dfc90226720382114 | |
| parent | 2c44f96e78832dd42e35a9cae6e90dccaf5c2178 (diff) | |
| parent | 72fe19225900d795a501755f4b492486e1bcb726 (diff) | |
| download | mruby-607cf951fc874e4af9c9f0e8a631d4ae6fe9d836.tar.gz mruby-607cf951fc874e4af9c9f0e8a631d4ae6fe9d836.zip | |
Merge pull request #1901 from ksss/compar-other
Fix behavior of Comparable methods when <=> return nil
| -rw-r--r-- | mrblib/compar.rb | 8 | ||||
| -rw-r--r-- | test/t/comparable.rb | 32 |
2 files changed, 24 insertions, 16 deletions
diff --git a/mrblib/compar.rb b/mrblib/compar.rb index 40fb2e7f0..44595974a 100644 --- a/mrblib/compar.rb +++ b/mrblib/compar.rb @@ -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 diff --git a/test/t/comparable.rb b/test/t/comparable.rb index b5718d2d2..2ee28de7b 100644 --- a/test/t/comparable.rb +++ b/test/t/comparable.rb @@ -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 |
