1require 'test/unit' 2require 'date' 3 4class TestDateAttr < Test::Unit::TestCase 5 6 def test__attr 7 date = Date.new(1965, 5, 23) 8 datetime = DateTime.new(1965, 5, 23, 22, 31, 59) 9 10 [date, datetime].each_with_index do |d, i| 11=begin 12 if i == 0 13 assert_match(/\#<Date\d?: 1965-05-23 \(4877807\/2,0,2299161\)>/, 14 d.inspect) 15 else 16 assert_match(/\#<DateTime\d?: 1965-05-23T22:31:59\+00:00 \(210721343519\/86400,0,2299161\)>/, 17 d.inspect) 18 end 19=end 20 21 if i == 0 22 assert_equal('1965-05-23', d.to_s) 23 else 24 assert_equal('1965-05-23T22:31:59+00:00', d.to_s) 25 end 26 27 assert_equal('', d.inspect.gsub!(/./,'')) 28 assert_equal('', d.to_s.gsub!(/./,'')) 29 30 assert_equal(2438904, d.jd) 31 32 if i == 0 33 assert_equal(0, d.day_fraction) 34 else 35 assert_equal(22.to_r/24 + 31.to_r/1440 + 59.to_r/86400, d.day_fraction) 36 end 37 38 assert_equal(38903, d.mjd) 39 assert_equal(139744, d.ld) 40 41 assert_equal(1965, d.year) 42 assert_equal(143, d.yday) 43 assert_equal(5, d.mon) 44 assert_equal(d.mon, d.month) 45 assert_equal(23, d.mday) 46 assert_equal(d.mday, d.day) 47 48 if i == 0 49 assert_equal(false, d.respond_to?(:hour)) 50 assert_equal(false, d.respond_to?(:min)) 51 assert_equal(false, d.respond_to?(:sec)) 52 assert_equal(false, d.respond_to?(:sec_fraction)) 53 assert_equal(false, d.respond_to?(:zone)) 54 assert_equal(false, d.respond_to?(:offset)) 55 else 56 assert_equal(22, d.hour) 57 assert_equal(31, d.min) 58 assert_equal(59, d.sec) 59 assert_equal(0, d.sec_fraction) 60 assert_equal('+00:00', d.zone) 61 assert_equal(0, d.offset) 62 end 63 64 assert_equal(1965, d.cwyear) 65 assert_equal(20, d.cweek) 66 assert_equal(7, d.cwday) 67 68 assert_equal(0, d.wday) 69 assert_equal(false, d.leap?) 70 assert_equal(false, d.julian?) 71 assert_equal(true, d.gregorian?) 72 73 assert_equal(Date::ITALY, d.start) 74 assert_equal(d.start, d.start) 75 end 76 77 d = DateTime.new(1965, 5, 23, 22, 31, 59) + 1.to_r/(86400*2) 78 assert_equal(1.to_r/2, d.sec_fraction) 79 end 80 81 def test__wday_predicate 82 d = Date.new(2005, 10, 23) 83 assert_equal(true, d.sunday?) 84 assert_equal(false, d.monday?) 85 assert_equal(false, d.tuesday?) 86 assert_equal(false, d.wednesday?) 87 assert_equal(false, d.thursday?) 88 assert_equal(false, d.friday?) 89 assert_equal(false, d.saturday?) 90 91 d = Date.new(2005, 10, 30) 92 14.times do |i| 93 assert((d + i).__send__(%w(sunday? monday? tuesday? wednesday? 94 thursday? friday? saturday?)[i % 7])) 95 end 96 end 97 98 def test_nth_kday 99 skip unless Date.new.respond_to?(:nth_kday?, true) 100 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 1,0)) 101 assert_equal(true, Date.new(2001,1,14).__send__(:nth_kday?, 2,0)) 102 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 3,0)) 103 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 4,0)) 104 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, 5,0)) 105 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -1,0)) 106 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -2,0)) 107 assert_equal(true, Date.new(2001,1,14).__send__(:nth_kday?, -3,0)) 108 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -4,0)) 109 assert_equal(false, Date.new(2001,1,14).__send__(:nth_kday?, -5,0)) 110 end 111 112end 113