2017-01-26 19:29:21 -05:00
|
|
|
# frozen_string_literal: true
|
2008-09-28 06:06:13 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'date'
|
|
|
|
|
|
|
|
class DateSub < Date; end
|
|
|
|
class DateTimeSub < DateTime; end
|
|
|
|
|
|
|
|
class TestDate < Test::Unit::TestCase
|
2019-08-25 13:01:13 -04:00
|
|
|
def test_range_infinite_float
|
|
|
|
today = Date.today
|
|
|
|
r = today...Float::INFINITY
|
|
|
|
assert_equal today, r.begin
|
|
|
|
assert_equal Float::INFINITY, r.end
|
|
|
|
assert_equal true, r.cover?(today+1)
|
|
|
|
assert_equal false, r.cover?(today-1)
|
|
|
|
r = (-Float::INFINITY)...today
|
|
|
|
assert_equal(-Float::INFINITY, r.begin)
|
|
|
|
assert_equal today, r.end
|
|
|
|
assert_equal false, r.cover?(today+1)
|
|
|
|
assert_equal true, r.cover?(today-1)
|
|
|
|
end
|
2008-09-28 06:06:13 -04:00
|
|
|
|
|
|
|
def test__const
|
|
|
|
assert_nil(Date::MONTHNAMES[0])
|
|
|
|
assert_equal('January', Date::MONTHNAMES[1])
|
|
|
|
assert_equal(13, Date::MONTHNAMES.size)
|
|
|
|
assert_equal('Sunday', Date::DAYNAMES[0])
|
|
|
|
assert_equal(7, Date::DAYNAMES.size)
|
|
|
|
|
|
|
|
assert_nil(Date::ABBR_MONTHNAMES[0])
|
|
|
|
assert_equal('Jan', Date::ABBR_MONTHNAMES[1])
|
|
|
|
assert_equal(13, Date::ABBR_MONTHNAMES.size)
|
|
|
|
assert_equal('Sun', Date::ABBR_DAYNAMES[0])
|
|
|
|
assert_equal(7, Date::ABBR_DAYNAMES.size)
|
|
|
|
|
|
|
|
assert(Date::MONTHNAMES.frozen?)
|
|
|
|
assert(Date::MONTHNAMES[1].frozen?)
|
|
|
|
assert(Date::DAYNAMES.frozen?)
|
|
|
|
assert(Date::DAYNAMES[0].frozen?)
|
|
|
|
|
|
|
|
assert(Date::ABBR_MONTHNAMES.frozen?)
|
|
|
|
assert(Date::ABBR_MONTHNAMES[1].frozen?)
|
|
|
|
assert(Date::ABBR_DAYNAMES.frozen?)
|
|
|
|
assert(Date::ABBR_DAYNAMES[0].frozen?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sub
|
|
|
|
d = DateSub.new
|
|
|
|
dt = DateTimeSub.new
|
|
|
|
|
|
|
|
assert_instance_of(DateSub, d)
|
2008-10-07 11:08:42 -04:00
|
|
|
assert_instance_of(DateTimeSub, dt)
|
|
|
|
|
|
|
|
assert_instance_of(DateSub, DateSub.today)
|
|
|
|
assert_instance_of(DateTimeSub, DateTimeSub.now)
|
|
|
|
|
2008-09-28 06:06:13 -04:00
|
|
|
assert_equal('-4712-01-01', d.to_s)
|
|
|
|
assert_equal('-4712-01-01T00:00:00+00:00', dt.to_s)
|
|
|
|
|
|
|
|
d2 = d + 1
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d - 1
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d >> 1
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d << 1
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d.succ
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d.next
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d.italy
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d.england
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d.julian
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
d2 = d.gregorian
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
s = Marshal.dump(d)
|
|
|
|
d2 = Marshal.load(s)
|
|
|
|
assert_equal(d2, d)
|
|
|
|
assert_instance_of(DateSub, d2)
|
|
|
|
|
|
|
|
dt2 = dt + 1
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt - 1
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt >> 1
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt << 1
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt.succ
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt.next
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt.italy
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt.england
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt.julian
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
dt2 = dt.gregorian
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
s = Marshal.dump(dt)
|
|
|
|
dt2 = Marshal.load(s)
|
|
|
|
assert_equal(dt2, dt)
|
|
|
|
assert_instance_of(DateTimeSub, dt2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql_p
|
|
|
|
d = Date.jd(0)
|
|
|
|
d2 = Date.jd(0)
|
|
|
|
dt = DateTime.jd(0)
|
|
|
|
dt2 = DateTime.jd(0)
|
|
|
|
|
|
|
|
assert_equal(d, d2)
|
|
|
|
assert_not_equal(d, 0)
|
|
|
|
|
|
|
|
assert_equal(dt, dt2)
|
|
|
|
assert_not_equal(dt, 0)
|
|
|
|
|
|
|
|
assert_equal(d, dt)
|
|
|
|
assert_equal(d2, dt2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
h = {}
|
|
|
|
h[Date.new(1999,5,23)] = 0
|
|
|
|
h[Date.new(1999,5,24)] = 1
|
|
|
|
h[Date.new(1999,5,25)] = 2
|
|
|
|
h[Date.new(1999,5,25)] = 9
|
|
|
|
assert_equal(3, h.size)
|
|
|
|
assert_equal(9, h[Date.new(1999,5,25)])
|
|
|
|
assert_equal(9, h[DateTime.new(1999,5,25)])
|
|
|
|
|
|
|
|
h = {}
|
|
|
|
h[DateTime.new(1999,5,23)] = 0
|
|
|
|
h[DateTime.new(1999,5,24)] = 1
|
|
|
|
h[DateTime.new(1999,5,25)] = 2
|
|
|
|
h[DateTime.new(1999,5,25)] = 9
|
|
|
|
assert_equal(3, h.size)
|
|
|
|
assert_equal(9, h[Date.new(1999,5,25)])
|
|
|
|
assert_equal(9, h[DateTime.new(1999,5,25)])
|
2017-09-06 23:24:08 -04:00
|
|
|
|
|
|
|
assert_instance_of(String, Date.new(1999,5,25).hash.to_s)
|
2008-09-28 06:06:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_freeze
|
|
|
|
d = Date.new
|
|
|
|
d.freeze
|
|
|
|
assert_equal(true, d.frozen?)
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
assert_instance_of(Integer, d.yday)
|
2008-09-28 06:06:13 -04:00
|
|
|
assert_instance_of(String, d.to_s)
|
|
|
|
end
|
|
|
|
|
2013-12-13 23:17:03 -05:00
|
|
|
def test_submillisecond_comparison
|
|
|
|
d1 = DateTime.new(2013, 12, 6, 0, 0, Rational(1, 10000))
|
|
|
|
d2 = DateTime.new(2013, 12, 6, 0, 0, Rational(2, 10000))
|
|
|
|
# d1 is 0.0001s earlier than d2
|
|
|
|
assert_equal(-1, d1 <=> d2)
|
|
|
|
assert_equal(0, d1 <=> d1)
|
|
|
|
assert_equal(1, d2 <=> d1)
|
|
|
|
end
|
|
|
|
|
2021-06-15 13:39:42 -04:00
|
|
|
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
|
2008-09-28 06:06:13 -04:00
|
|
|
end
|