diff options
Diffstat (limited to 'test/date')
-rw-r--r-- | test/date/test_date.rb | 29 | ||||
-rw-r--r-- | test/date/test_date_conv.rb | 17 | ||||
-rw-r--r-- | test/date/test_date_parse.rb | 42 | ||||
-rw-r--r-- | test/date/test_date_ractor.rb | 2 | ||||
-rw-r--r-- | test/date/test_date_strptime.rb | 9 |
5 files changed, 78 insertions, 21 deletions
diff --git a/test/date/test_date.rb b/test/date/test_date.rb index 6e99bc562c..3f9c893efa 100644 --- a/test/date/test_date.rb +++ b/test/date/test_date.rb @@ -175,4 +175,33 @@ class TestDate < Test::Unit::TestCase assert_equal(-1, -Float::INFINITY <=> Date::Infinity.new) assert_equal(-1, -Date::Infinity.new <=> Float::INFINITY) end + + def test_deconstruct_keys + d = Date.new(1999,5,23) + assert_equal({year: 1999, month: 5, day: 23, wday: 0, yday: 143}, d.deconstruct_keys(nil)) + assert_equal({year: 1999}, d.deconstruct_keys([:year, :century])) + assert_equal( + {year: 1999, month: 5, day: 23, wday: 0, yday: 143}, + d.deconstruct_keys([:year, :month, :day, :wday, :yday]) + ) + + dt = DateTime.new(1999, 5, 23, 4, 20, Rational(1, 10000)) + + assert_equal( + {year: 1999, month: 5, day: 23, wday: 0, yday: 143, + hour: 4, min: 20, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"}, + dt.deconstruct_keys(nil) + ) + + assert_equal({year: 1999}, dt.deconstruct_keys([:year, :century])) + + assert_equal( + {year: 1999, month: 5, day: 23, wday: 0, yday: 143, + hour: 4, min: 20, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"}, + dt.deconstruct_keys([:year, :month, :day, :wday, :yday, :hour, :min, :sec, :sec_fraction, :zone]) + ) + + dtz = DateTime.parse('3rd Feb 2001 04:05:06+03:30') + assert_equal({zone: '+03:30'}, dtz.deconstruct_keys([:zone])) + end end diff --git a/test/date/test_date_conv.rb b/test/date/test_date_conv.rb index d41ff45d85..ed478b41bb 100644 --- a/test/date/test_date_conv.rb +++ b/test/date/test_date_conv.rb @@ -77,6 +77,11 @@ class TestDateConv < Test::Unit::TestCase assert_equal([2004, 9, 19, 1, 2, 3, 456789], [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec]) + d = DateTime.new(1582, 10, 3, 1, 2, 3, 0) + 456789.to_r/86400000000 + t = d.to_time.utc + assert_equal([1582, 10, 13, 1, 2, 3, 456789], + [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec]) + if Time.allocate.respond_to?(:nsec) d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000 t = d.to_time.utc @@ -100,6 +105,10 @@ class TestDateConv < Test::Unit::TestCase t = Time.utc(2004, 9, 19, 1, 2, 3, 456789) d = t.to_date assert_equal([2004, 9, 19, 0], [d.year, d.mon, d.mday, d.day_fraction]) + + t = Time.utc(1582, 10, 13, 1, 2, 3, 456789) + d = t.to_date # using ITALY + assert_equal([1582, 10, 3, 0], [d.year, d.mon, d.mday, d.day_fraction]) end def test_to_date__from_date @@ -136,6 +145,14 @@ class TestDateConv < Test::Unit::TestCase [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction, d.offset]) + t = Time.utc(1582, 10, 13, 1, 2, 3, 456789) + d = t.to_datetime # using ITALY + assert_equal([1582, 10, 3, 1, 2, 3, + 456789.to_r/1000000, + 0], + [d.year, d.mon, d.mday, d.hour, d.min, d.sec, + d.sec_fraction, d.offset]) + t = Time.now d = t.to_datetime require 'time' diff --git a/test/date/test_date_parse.rb b/test/date/test_date_parse.rb index 34a672b069..16362e3bff 100644 --- a/test/date/test_date_parse.rb +++ b/test/date/test_date_parse.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'test/unit' require 'date' -require 'timeout' +require 'envutil' class TestDateParse < Test::Unit::TestCase @@ -41,6 +41,7 @@ class TestDateParse < Test::Unit::TestCase [['Sat Aug 28 02:29:34 Mountain Daylight Time 2000',false],[2000,8,28,2,29,34,'Mountain Daylight Time',-6*3600,6], __LINE__], [['Sat Aug 28 02:29:34 Mexico Standard Time 2000',false],[2000,8,28,2,29,34,'Mexico Standard Time',-6*3600,6], __LINE__], [['Sat Aug 28 02:29:34 E. Australia Standard Time 2000',false],[2000,8,28,2,29,34,'E. Australia Standard Time',10*3600,6], __LINE__], + [['Sat Aug 28 02:29:34 W. Central Africa Standard Time 2000',false],[2000,8,28,2,29,34,'W. Central Africa Standard Time',1*3600,6], __LINE__], # part of iso 8601 [['1999-05-23 23:55:21',false],[1999,5,23,23,55,21,nil,nil,nil], __LINE__], @@ -132,6 +133,7 @@ class TestDateParse < Test::Unit::TestCase [['19990523235521.123[-9]',false],[1999,5,23,23,55,21,'-9',-(9*3600),nil], __LINE__], [['19990523235521.123[+9]',false],[1999,5,23,23,55,21,'+9',+(9*3600),nil], __LINE__], [['19990523235521.123[9]',false],[1999,5,23,23,55,21,'9',+(9*3600),nil], __LINE__], + [['19990523235521.123[9 ]',false],[1999,5,23,23,55,21,'9 ',+(9*3600),nil], __LINE__], [['19990523235521.123[-9.50]',false],[1999,5,23,23,55,21,'-9.50',-(9*3600+30*60),nil], __LINE__], [['19990523235521.123[+9.50]',false],[1999,5,23,23,55,21,'+9.50',+(9*3600+30*60),nil], __LINE__], [['19990523235521.123[-5:EST]',false],[1999,5,23,23,55,21,'EST',-5*3600,nil], __LINE__], @@ -140,6 +142,7 @@ class TestDateParse < Test::Unit::TestCase [['235521.123',false],[nil,nil,nil,23,55,21,nil,nil,nil], __LINE__], [['235521.123[-9]',false],[nil,nil,nil,23,55,21,'-9',-9*3600,nil], __LINE__], [['235521.123[+9]',false],[nil,nil,nil,23,55,21,'+9',+9*3600,nil], __LINE__], + [['235521.123[-9 ]',false],[nil,nil,nil,23,55,21,'-9 ',-9*3600,nil], __LINE__], [['235521.123[-5:EST]',false],[nil,nil,nil,23,55,21,'EST',-5*3600,nil], __LINE__], [['235521.123[+9:JST]',false],[nil,nil,nil,23,55,21,'JST',+9*3600,nil], __LINE__], @@ -585,6 +588,18 @@ class TestDateParse < Test::Unit::TestCase assert_equal(5025, h[:offset]) end + def test__parse_too_long_year + str = "Jan 1" + "0" * 100_000 + h = EnvUtil.timeout(3) {Date._parse(str, limit: 100_010)} + assert_equal(100_000, Math.log10(h[:year])) + assert_equal(1, h[:mon]) + + str = "Jan - 1" + "0" * 100_000 + h = EnvUtil.timeout(3) {Date._parse(str, limit: 100_010)} + assert_equal(1, h[:mon]) + assert_not_include(h, :year) + end + require 'time' def test_parse__time @@ -852,9 +867,7 @@ class TestDateParse < Test::Unit::TestCase h = Date._iso8601(nil) assert_equal({}, h) - h = Date._iso8601('01-02-03T04:05:06Z'.to_sym) - assert_equal([2001, 2, 3, 4, 5, 6, 0], - h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + assert_raise(TypeError) {Date._iso8601('01-02-03T04:05:06Z'.to_sym)} end def test__rfc3339 @@ -874,9 +887,7 @@ class TestDateParse < Test::Unit::TestCase h = Date._rfc3339(nil) assert_equal({}, h) - h = Date._rfc3339('2001-02-03T04:05:06Z'.to_sym) - assert_equal([2001, 2, 3, 4, 5, 6, 0], - h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + assert_raise(TypeError) {Date._rfc3339('2001-02-03T04:05:06Z'.to_sym)} end def test__xmlschema @@ -963,9 +974,7 @@ class TestDateParse < Test::Unit::TestCase h = Date._xmlschema(nil) assert_equal({}, h) - h = Date._xmlschema('2001-02-03'.to_sym) - assert_equal([2001, 2, 3, nil, nil, nil, nil], - h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + assert_raise(TypeError) {Date._xmlschema('2001-02-03'.to_sym)} end def test__rfc2822 @@ -1002,9 +1011,7 @@ class TestDateParse < Test::Unit::TestCase h = Date._rfc2822(nil) assert_equal({}, h) - h = Date._rfc2822('Sat, 3 Feb 2001 04:05:06 UT'.to_sym) - assert_equal([2001, 2, 3, 4, 5, 6, 0], - h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + assert_raise(TypeError) {Date._rfc2822('Sat, 3 Feb 2001 04:05:06 UT'.to_sym)} end def test__httpdate @@ -1029,9 +1036,7 @@ class TestDateParse < Test::Unit::TestCase h = Date._httpdate(nil) assert_equal({}, h) - h = Date._httpdate('Sat, 03 Feb 2001 04:05:06 GMT'.to_sym) - assert_equal([2001, 2, 3, 4, 5, 6, 0], - h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + assert_raise(TypeError) {Date._httpdate('Sat, 03 Feb 2001 04:05:06 GMT'.to_sym)} end def test__jisx0301 @@ -1112,9 +1117,7 @@ class TestDateParse < Test::Unit::TestCase h = Date._jisx0301(nil) assert_equal({}, h) - h = Date._jisx0301('H13.02.03T04:05:06.07+0100'.to_sym) - assert_equal([2001, 2, 3, 4, 5, 6, 3600], - h.values_at(:year, :mon, :mday, :hour, :min, :sec, :offset)) + assert_raise(TypeError) {Date._jisx0301('H13.02.03T04:05:06.07+0100'.to_sym)} end def test_iso8601 @@ -1297,6 +1300,5 @@ class TestDateParse < Test::Unit::TestCase assert_raise(ArgumentError) { DateTime.jisx0301("1" * 1000) } assert_raise(ArgumentError) { Date._parse("Jan " + "9" * 1000000) } - assert_raise(Timeout::Error) { Timeout.timeout(1) { Date._parse("Jan " + "9" * 1000000, limit: nil) } } end end diff --git a/test/date/test_date_ractor.rb b/test/date/test_date_ractor.rb index 7b0c3f4911..7ec953d87a 100644 --- a/test/date/test_date_ractor.rb +++ b/test/date/test_date_ractor.rb @@ -2,7 +2,7 @@ require 'test/unit' require 'date' -class TestDateParse < Test::Unit::TestCase +class TestDateParseRactor < Test::Unit::TestCase def code(klass = Date, share: false) <<~RUBY.gsub('Date', klass.name) share = #{share} diff --git a/test/date/test_date_strptime.rb b/test/date/test_date_strptime.rb index fc42ebf7cd..4efe1a47d0 100644 --- a/test/date/test_date_strptime.rb +++ b/test/date/test_date_strptime.rb @@ -180,6 +180,10 @@ class TestDateStrptime < Test::Unit::TestCase [['fri1feb034pm+5', '%a%d%b%y%H%p%Z'], [2003,2,1,16,nil,nil,'+5',5*3600,5]], [['E. Australia Standard Time', '%Z'], [nil,nil,nil,nil,nil,nil,'E. Australia Standard Time',10*3600,nil], __LINE__], + + # out of range + [['+0.9999999999999999999999', '%Z'], [nil,nil,nil,nil,nil,nil,'+0.9999999999999999999999',+1*3600,nil], __LINE__], + [['+9999999999999999999999.0', '%Z'], [nil,nil,nil,nil,nil,nil,'+9999999999999999999999.0',nil,nil], __LINE__], ].each do |x, y| h = Date._strptime(*x) a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday) @@ -292,6 +296,11 @@ class TestDateStrptime < Test::Unit::TestCase assert_not_nil(Date._strptime('Januari', '%B')) assert_nil(Date._strptime('Sundai,', '%A,')) assert_nil(Date._strptime('Januari,', '%B,')) + + assert_nil(Date._strptime('+24:00', '%Z')[:offset]) + assert_nil(Date._strptime('+23:60', '%Z')[:offset]) + assert_nil(Date._strptime('+23:00:60', '%Z')[:offset]) + assert_nil(Date._strptime('+23:00:60', '%Z')[:offset]) end def test_strptime |