2010-11-12 21:45:12 +01:00
|
|
|
require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
|
2007-11-22 21:39:26 +01:00
|
|
|
|
|
|
|
class DateTests < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@date = Date.civil(2008,1,1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extending_date_class
|
2009-12-02 11:34:56 +01:00
|
|
|
assert @date.respond_to?('holidays')
|
2007-11-22 21:39:26 +01:00
|
|
|
assert @date.respond_to?('holiday?')
|
|
|
|
end
|
|
|
|
|
2010-11-12 23:26:33 +01:00
|
|
|
def test_extending_datetime_class
|
|
|
|
dt = DateTime.civil(2008,1,1)
|
|
|
|
assert dt.respond_to?('holidays')
|
|
|
|
assert dt.respond_to?('holiday?')
|
|
|
|
end
|
|
|
|
|
2007-11-22 21:39:26 +01:00
|
|
|
def test_calculating_mdays
|
|
|
|
# US Memorial day
|
|
|
|
assert_equal 29, Date.calculate_mday(2006, 5, :last, 1)
|
|
|
|
assert_equal 28, Date.calculate_mday(2007, 5, :last, 1)
|
|
|
|
assert_equal 26, Date.calculate_mday(2008, 5, :last, 1)
|
|
|
|
assert_equal 25, Date.calculate_mday(2009, 5, :last, 1)
|
|
|
|
assert_equal 31, Date.calculate_mday(2010, 5, :last, 1)
|
|
|
|
assert_equal 30, Date.calculate_mday(2011, 5, :last, 1)
|
|
|
|
|
|
|
|
# Labour day
|
|
|
|
assert_equal 3, Date.calculate_mday(2007, 9, :first, 1)
|
2007-12-19 22:18:56 +01:00
|
|
|
assert_equal 1, Date.calculate_mday(2008, 9, :first, :monday)
|
2007-11-22 21:39:26 +01:00
|
|
|
assert_equal 7, Date.calculate_mday(2009, 9, :first, 1)
|
|
|
|
assert_equal 5, Date.calculate_mday(2011, 9, :first, 1)
|
|
|
|
assert_equal 5, Date.calculate_mday(2050, 9, :first, 1)
|
|
|
|
assert_equal 4, Date.calculate_mday(2051, 9, :first, 1)
|
|
|
|
|
|
|
|
# Canadian thanksgiving
|
|
|
|
assert_equal 8, Date.calculate_mday(2007, 10, :second, 1)
|
2007-12-19 22:18:56 +01:00
|
|
|
assert_equal 13, Date.calculate_mday(2008, 10, :second, :monday)
|
2007-11-22 21:39:26 +01:00
|
|
|
assert_equal 12, Date.calculate_mday(2009, 10, :second, 1)
|
|
|
|
assert_equal 11, Date.calculate_mday(2010, 10, :second, 1)
|
|
|
|
|
|
|
|
# Misc
|
|
|
|
assert_equal 21, Date.calculate_mday(2008, 1, :third, 1)
|
|
|
|
assert_equal 1, Date.calculate_mday(2007, 1, :first, 1)
|
2007-12-19 22:18:56 +01:00
|
|
|
assert_equal 2, Date.calculate_mday(2007, 3, :first, :friday)
|
2009-11-17 20:17:31 +01:00
|
|
|
assert_equal 30, Date.calculate_mday(2012, 1, :last, 1)
|
|
|
|
assert_equal 29, Date.calculate_mday(2016, 2, :last, 1)
|
2010-01-12 05:53:11 +01:00
|
|
|
|
|
|
|
# From end of month
|
|
|
|
assert_equal 26, Date.calculate_mday(2009, 8, -1, :wednesday)
|
|
|
|
assert_equal 19, Date.calculate_mday(2009, 8, -2, :wednesday)
|
|
|
|
assert_equal 12, Date.calculate_mday(2009, 8, -3, :wednesday)
|
|
|
|
|
|
|
|
assert_equal 13, Date.calculate_mday(2008, 8, -3, :wednesday)
|
|
|
|
assert_equal 12, Date.calculate_mday(2009, 8, -3, :wednesday)
|
|
|
|
assert_equal 11, Date.calculate_mday(2010, 8, -3, :wednesday)
|
|
|
|
assert_equal 17, Date.calculate_mday(2011, 8, -3, :wednesday)
|
|
|
|
assert_equal 15, Date.calculate_mday(2012, 8, -3, :wednesday)
|
|
|
|
assert_equal 14, Date.calculate_mday(2013, 8, -3, :wednesday)
|
2007-11-22 21:39:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mday_allows_integers_or_symbols
|
|
|
|
assert_nothing_raised do
|
|
|
|
Date.calculate_mday(2008, 1, 1, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
Date.calculate_mday(2008, 1, -1, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
Date.calculate_mday(2008, 1, :last, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mday_requires_valid_week
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Date.calculate_mday(2008, 1, :none, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Date.calculate_mday(2008, 1, nil, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Date.calculate_mday(2008, 1, 0, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-19 22:18:56 +01:00
|
|
|
def test_mday_requires_valid_day
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Date.calculate_mday(2008, 1, 1, :october)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Date.calculate_mday(2008, 1, 1, nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
|
|
|
Date.calculate_mday(2008, 1, 1, 7)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-12 23:26:33 +01:00
|
|
|
def test_date_holiday?
|
2007-11-22 21:39:26 +01:00
|
|
|
assert Date.civil(2008,1,1).holiday?('ca')
|
2010-11-12 23:26:33 +01:00
|
|
|
assert Date.today.holiday?('test')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_datetime_holiday?
|
2010-11-12 23:39:53 +01:00
|
|
|
# in situations with activesupport
|
|
|
|
assert DateTime.now.to_date.holiday?('test') if DateTime.now.respond_to?(:to_date)
|
2010-11-12 23:26:33 +01:00
|
|
|
assert DateTime.now.holiday?('test')
|
|
|
|
end
|
|
|
|
|
|
|
|
# ensure that dates are compared in the same timezone
|
|
|
|
def test_datetime_offset_holiday?
|
|
|
|
dt = DateTime.now.new_offset(Rational(23,24))
|
|
|
|
assert dt.holiday?('test'), dt.inspect
|
|
|
|
|
|
|
|
dt = DateTime.now.new_offset(Rational(-23,24))
|
|
|
|
assert dt.holiday?('test'), dt.inspect
|
2007-11-22 21:39:26 +01:00
|
|
|
end
|
|
|
|
end
|