Skip to content

Commit

Permalink
Fix comparison with Float::INFINITY
Browse files Browse the repository at this point in the history
Fixes [Bug #17945]
  • Loading branch information
jeremyevans committed Jun 15, 2021
1 parent f3c4836 commit 953d907
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/date.rb
Expand Up @@ -30,6 +30,8 @@ def +@() self.class.new(+d) end
def <=>(other)
case other
when Infinity; return d <=> other.d
when Float::INFINITY; return d <=> 1
when -Float::INFINITY; return d <=> -1
when Numeric; return d
else
begin
Expand Down
12 changes: 12 additions & 0 deletions test/date/test_date.rb
Expand Up @@ -163,4 +163,16 @@ def test_submillisecond_comparison
assert_equal(1, d2 <=> d1)
end

def test_infinity_comparison
assert_equal(0, Float::INFINITY <=> Date::Infinity.new)
assert_equal(0, Date::Infinity.new <=> Float::INFINITY)
assert_equal(0, -Float::INFINITY <=> -Date::Infinity.new)
assert_equal(0, -Date::Infinity.new <=> -Float::INFINITY)

assert_equal(1, Float::INFINITY <=> -Date::Infinity.new)
assert_equal(1, Date::Infinity.new <=> -Float::INFINITY)

assert_equal(-1, -Float::INFINITY <=> Date::Infinity.new)
assert_equal(-1, -Date::Infinity.new <=> Float::INFINITY)
end
end

0 comments on commit 953d907

Please sign in to comment.