range_ext_test.rb 3.7 KB
Newer Older
1 2 3 4
require "abstract_unit"
require "active_support/time"
require "active_support/core_ext/numeric"
require "active_support/core_ext/range"
5

6
class RangeTest < ActiveSupport::TestCase
7 8 9 10 11 12 13 14 15
  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
16

A
Akshay Vishnoi 已提交
17 18 19 20 21
  def test_to_s_with_numeric
    number_range = (1..100)
    assert_equal "BETWEEN '1' AND '100'", number_range.to_s(:db)
  end

22 23 24
  def test_date_range
    assert_instance_of Range, DateTime.new..DateTime.new
    assert_instance_of Range, DateTime::Infinity.new..DateTime::Infinity.new
25
    assert_instance_of Range, DateTime.new..DateTime::Infinity.new
26
  end
27

28 29 30 31 32 33 34 35 36 37 38
  def test_overlaps_last_inclusive
    assert((1..5).overlaps?(5..10))
  end

  def test_overlaps_last_exclusive
    assert !(1...5).overlaps?(5..10)
  end

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

40 41 42
  def test_overlaps_first_exclusive
    assert !(5..10).overlaps?(1...5)
  end
43

44 45 46 47 48 49 50 51
  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 已提交
52
  def test_should_include_other_with_exclusive_end
53 54 55
    assert((1..10).include?(1...10))
  end

56 57 58 59 60 61 62 63
  def test_should_compare_identical_inclusive
    assert((1..10) === (1..10))
  end

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

64
  def test_should_compare_other_with_exclusive_end
65 66 67
    assert((1..10) === (1...10))
  end

68 69 70 71 72 73 74
  def test_exclusive_end_should_not_include_identical_with_inclusive_end
    assert !(1...10).include?(1..10)
  end

  def test_should_not_include_overlapping_first
    assert !(2..8).include?(1..3)
  end
75

76 77 78
  def test_should_not_include_overlapping_last
    assert !(2..8).include?(5..9)
  end
79

80
  def test_should_include_identical_exclusive_with_floats
81
    assert((1.0...10.0).include?(1.0...10.0))
82 83
  end

84 85 86
  def test_cover_is_not_override
    range = (1..3)
    assert range.method(:include?) != range.method(:cover?)
D
Diego Carrion 已提交
87
  end
88 89 90 91 92 93 94 95 96 97 98 99

  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)
    assert !time_range_1.overlaps?(time_range_2)
  end
100 101

  def test_each_on_time_with_zone
102
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"] , Time.utc(2006,11,28,10,30))
103 104 105 106 107 108
    assert_raises TypeError do
      ((twz - 1.hour)..twz).each {}
    end
  end

  def test_step_on_time_with_zone
109
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"] , Time.utc(2006,11,28,10,30))
110 111 112 113 114 115
    assert_raises TypeError do
      ((twz - 1.hour)..twz).step(1) {}
    end
  end

  def test_include_on_time_with_zone
116
    twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"] , Time.utc(2006,11,28,10,30))
117 118 119 120 121
    assert_raises TypeError do
      ((twz - 1.hour)..twz).include?(twz)
    end
  end

122 123
  def test_date_time_with_each
    datetime = DateTime.now
124
    assert(((datetime - 1.hour)..datetime).each {})
125
  end
126 127 128

  def test_date_time_with_step
    datetime = DateTime.now
129
    assert(((datetime - 1.hour)..datetime).step(1) {})
130
  end
131
end