提交 e4068f5f 编写于 作者: M Michael Groeneman

Align `Range#cover?` extension behavior with Ruby behavior for backwards ranges.

`(1..10).cover?(5..3)` now returns `false`, as it does in plain Ruby. Previously this returned `true`.

Also update `#include?` and `#===` behavior to match.
上级 ba20b7a4
* Align `Range#cover?` extension behavior with Ruby behavior for backwards ranges.
`(1..10).cover?(5..3)` now returns `false`, as it does in plain Ruby.
Also update `#include?` and `#===` behavior to match.
*Michael Groeneman*
* Update to TZInfo v2.0.0.
This changes the output of `ActiveSupport::TimeZone.utc_to_local`, but
......
......@@ -15,6 +15,8 @@ module CompareWithRange
# The given range must be fully bounded, with both start and end.
def ===(value)
if value.is_a?(::Range)
is_backwards_op = value.exclude_end? ? :>= : :>
return false if value.begin && value.end && value.begin.send(is_backwards_op, value.end)
# 1...10 includes 1..9 but it does not include 1..10.
# 1..10 includes 1...11 but it does not include 1...12.
operator = exclude_end? && !value.exclude_end? ? :< : :<=
......@@ -38,6 +40,8 @@ def ===(value)
# The given range must be fully bounded, with both start and end.
def include?(value)
if value.is_a?(::Range)
is_backwards_op = value.exclude_end? ? :>= : :>
return false if value.begin && value.end && value.begin.send(is_backwards_op, value.end)
# 1...10 includes 1..9 but it does not include 1..10.
# 1..10 includes 1...11 but it does not include 1...12.
operator = exclude_end? && !value.exclude_end? ? :< : :<=
......@@ -61,6 +65,8 @@ def include?(value)
# The given range must be fully bounded, with both start and end.
def cover?(value)
if value.is_a?(::Range)
is_backwards_op = value.exclude_end? ? :>= : :>
return false if value.begin && value.end && value.begin.send(is_backwards_op, value.end)
# 1...10 covers 1..9 but it does not cover 1..10.
# 1..10 covers 1...11 but it does not cover 1...12.
operator = exclude_end? && !value.exclude_end? ? :< : :<=
......
......@@ -60,6 +60,15 @@ def test_should_include_other_with_exclusive_end
assert((1..10).include?(1...11))
end
def test_include_returns_false_for_backwards
assert_not((1..10).include?(5..3))
end
# Match quirky plain-Ruby behavior
def test_include_returns_false_for_empty_exclusive_end
assert_not((1..5).include?(3...3))
end
if RUBY_VERSION >= "2.6"
def test_include_with_endless_range
assert(eval("1..").include?(2))
......@@ -100,6 +109,15 @@ def test_should_compare_other_with_exclusive_end
assert((1..10) === (1...11))
end
def test_compare_returns_false_for_backwards
assert_not((1..10) === (5..3))
end
# Match quirky plain-Ruby behavior
def test_compare_returns_false_for_empty_exclusive_end
assert_not((1..5) === (3...3))
end
if RUBY_VERSION >= "2.6"
def test_should_compare_range_with_endless_range
assert(eval("1..") === (2..4))
......@@ -145,6 +163,15 @@ def test_should_cover_other_with_exclusive_end
assert((1..10).cover?(1...11))
end
def test_cover_returns_false_for_backwards
assert_not((1..10).cover?(5..3))
end
# Match quirky plain-Ruby behavior
def test_cover_returns_false_for_empty_exclusive_end
assert_not((1..5).cover?(3...3))
end
if RUBY_VERSION >= "2.6"
def test_should_cover_range_with_endless_range
assert(eval("1..").cover?(2..4))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册