range_ext_test.rb 6.5 KB
Newer Older
1
# frozen_string_literal: true
2

3
require_relative "../abstract_unit"
4 5 6
require "active_support/time"
require "active_support/core_ext/numeric"
require "active_support/core_ext/range"
7

8
class RangeTest < ActiveSupport::TestCase
9 10 11 12 13 14 15 16 17
  def test_to_s_from_dates
    date_range = Date.new(2005, 12, 10)..Date.new(2005, 12, 12)
    assert_equal "BETWEEN '2005-12-10' AND '2005-12-12'", date_range.to_s(:db)
  end

  def test_to_s_from_times
    date_range = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
    assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db)
  end
18

19 20 21 22 23
  def test_to_s_with_alphabets
    alphabet_range = ("a".."z")
    assert_equal "BETWEEN 'a' AND 'z'", alphabet_range.to_s(:db)
  end

A
Akshay Vishnoi 已提交
24 25 26 27 28
  def test_to_s_with_numeric
    number_range = (1..100)
    assert_equal "BETWEEN '1' AND '100'", number_range.to_s(:db)
  end

29 30 31
  def test_date_range
    assert_instance_of Range, DateTime.new..DateTime.new
    assert_instance_of Range, DateTime::Infinity.new..DateTime::Infinity.new
32
    assert_instance_of Range, DateTime.new..DateTime::Infinity.new
33
  end
34

35 36 37 38 39
  def test_overlaps_last_inclusive
    assert((1..5).overlaps?(5..10))
  end

  def test_overlaps_last_exclusive
40
    assert_not (1...5).overlaps?(5..10)
41 42 43 44 45
  end

  def test_overlaps_first_inclusive
    assert((5..10).overlaps?(1..5))
  end
46

47
  def test_overlaps_first_exclusive
48
    assert_not (5..10).overlaps?(1...5)
49
  end
50

51 52 53 54 55 56 57 58
  def test_should_include_identical_inclusive
    assert((1..10).include?(1..10))
  end

  def test_should_include_identical_exclusive
    assert((1...10).include?(1...10))
  end

A
Akshay Vishnoi 已提交
59
  def test_should_include_other_with_exclusive_end
60
    assert((1..10).include?(1...11))
61 62
  end

63 64 65 66
  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
    def test_include_with_endless_range
      assert(eval("1..").include?(2))
    end
67 68 69 70 71 72 73 74

    def test_should_include_range_with_endless_range
      assert(eval("1..").include?(2..4))
    end

    def test_should_not_include_range_with_endless_range
      assert_not(eval("1..").include?(0..4))
    end
75 76 77 78 79 80
  end

  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.0")
    def test_include_with_beginless_range
      assert(eval("..2").include?(1))
    end
81 82 83 84 85 86 87 88

    def test_should_include_range_with_beginless_range
      assert(eval("..2").include?(-1..1))
    end

    def test_should_not_include_range_with_beginless_range
      assert_not(eval("..2").include?(-1..3))
    end
89 90
  end

91 92 93 94 95 96 97 98
  def test_should_compare_identical_inclusive
    assert((1..10) === (1..10))
  end

  def test_should_compare_identical_exclusive
    assert((1...10) === (1...10))
  end

99
  def test_should_compare_other_with_exclusive_end
100
    assert((1..10) === (1...11))
101 102
  end

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
    def test_should_compare_range_with_endless_range
      assert(eval("1..") === (2..4))
    end

    def test_should_not_compare_range_with_endless_range
      assert_not(eval("1..") === (0..4))
    end
  end

  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.0")
    def test_should_compare_range_with_beginless_range
      assert(eval("..2") === (-1..1))
    end

    def test_should_not_compare_range_with_beginless_range
      assert_not(eval("..2") === (-1..3))
    end
  end

123
  def test_exclusive_end_should_not_include_identical_with_inclusive_end
124
    assert_not_includes (1...10), 1..10
125 126 127
  end

  def test_should_not_include_overlapping_first
128
    assert_not_includes (2..8), 1..3
129
  end
130

131
  def test_should_not_include_overlapping_last
132
    assert_not_includes (2..8), 5..9
133
  end
134

135
  def test_should_include_identical_exclusive_with_floats
136
    assert((1.0...10.0).include?(1.0...10.0))
137 138
  end

139 140 141
  def test_cover_is_not_override
    range = (1..3)
    assert range.method(:include?) != range.method(:cover?)
D
Diego Carrion 已提交
142
  end
143

144 145 146 147
  def test_should_cover_other_with_exclusive_end
    assert((1..10).cover?(1...11))
  end

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
    def test_should_cover_range_with_endless_range
      assert(eval("1..").cover?(2..4))
    end

    def test_should_not_cover_range_with_endless_range
      assert_not(eval("1..").cover?(0..4))
    end
  end

  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.0")
    def test_should_cover_range_with_beginless_range
      assert(eval("..2").cover?(-1..1))
    end

    def test_should_not_cover_range_with_beginless_range
      assert_not(eval("..2").cover?(-1..3))
    end
  end

168 169 170 171 172 173 174 175 176
  def test_overlaps_on_time
    time_range_1 = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
    time_range_2 = Time.utc(2005, 12, 10, 17, 00)..Time.utc(2005, 12, 10, 18, 00)
    assert time_range_1.overlaps?(time_range_2)
  end

  def test_no_overlaps_on_time
    time_range_1 = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
    time_range_2 = Time.utc(2005, 12, 10, 17, 31)..Time.utc(2005, 12, 10, 18, 00)
177
    assert_not time_range_1.overlaps?(time_range_2)
178
  end
179 180

  def test_each_on_time_with_zone
181
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
182
    assert_raises TypeError do
183
      ((twz - 1.hour)..twz).each { }
184 185 186 187
    end
  end

  def test_step_on_time_with_zone
188
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
189
    assert_raises TypeError do
190
      ((twz - 1.hour)..twz).step(1) { }
191 192 193
    end
  end

194 195 196 197 198
  def test_cover_on_time_with_zone
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
    assert ((twz - 1.hour)..twz).cover?(twz)
  end

199
  def test_include_on_time_with_zone
200
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
201 202 203
    assert ((twz - 1.hour)..twz).include?(twz)
  end

204 205 206 207 208 209 210
  def test_include_on_time_with_zone_deprecation
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
    assert_deprecated do
      ((twz - 1.hour)..twz).include?(twz)
    end
  end

211
  def test_case_equals_on_time_with_zone
212
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30))
213
    assert ((twz - 1.hour)..twz) === twz
214 215
  end

216 217
  def test_date_time_with_each
    datetime = DateTime.now
218
    assert(((datetime - 1.hour)..datetime).each { })
219
  end
220 221 222

  def test_date_time_with_step
    datetime = DateTime.now
223
    assert(((datetime - 1.hour)..datetime).step(1) { })
224
  end
225
end