|
require 'test/unit'
|
|
require 'date/delta'
|
|
require 'mathn'
|
|
|
|
class TestDateDelta < Test::Unit::TestCase
|
|
def setup
|
|
@fix1 =
|
|
{ :years => 0, :months => 0, :days => 1, :hours => 2,
|
|
:minutes => 3, :seconds => 4 }
|
|
@fix2 =
|
|
{ :years => 1, :months => 2, :days => 0, :hours => 0,
|
|
:minutes => 0, :seconds => 0 }
|
|
@parsestrings =
|
|
{ "2 weeks and 1 days" => Date::Delta.new(15),
|
|
"2 weeks - 1 days" => Date::Delta.new(13),
|
|
"2 weeks and - 1 days" => Date::Delta.new(13),
|
|
"(2 weeks and 1 hours) * 2" => Date::Delta.new(28,2),
|
|
"1 weeks^2" => Date::Delta.new(49),
|
|
"1 days/ 24" => Date::Delta.new(0,1),
|
|
"1 month" => Date::Delta.new(Complex::I)
|
|
}
|
|
@units =
|
|
{"year"=>12* Complex::I,
|
|
"month"=> Complex::I,
|
|
"day"=> Rational(1,1),
|
|
"week"=> Rational(7,1),
|
|
"sennight"=> Rational(7,1),
|
|
"fortnight"=> Rational(14,1),
|
|
"hour"=> Rational(1,24),
|
|
"minute"=> Rational(1,1440),
|
|
"second"=> Rational(1,86400),
|
|
"yottasecond"=> Rational(312500000000000000000,27),
|
|
"zettasecond"=> Rational(312500000000000000,27),
|
|
"exasecond"=> Rational(312500000000000,27),
|
|
"petasecond"=> Rational(312500000000,27),
|
|
"terasecond"=> Rational(312500000,27),
|
|
"gigasecond"=> Rational(312500,27),
|
|
"megasecond"=> Rational(625,54),
|
|
"kilosecond"=> Rational(5,432),
|
|
"hectosecond"=> Rational(1,864),
|
|
"decasecond"=> Rational(1,8640),
|
|
"dekasecond"=> Rational(1,8640),
|
|
"decisecond"=> Rational(1,864000),
|
|
"centisecond"=> Rational(1,8640000),
|
|
"millisecond"=> Rational(1,86400000),
|
|
"decimillisecond"=> Rational(1,864000000),
|
|
"centimillisecond"=> Rational(1,8640000000),
|
|
"microsecond"=> Rational(1,86400000000),
|
|
"nanosecond"=> Rational(1,86400000000000),
|
|
"millimicrosecond"=> Rational(1,86400000000000),
|
|
"picosecond"=> Rational(1,86400000000000000),
|
|
"micromicrosecond"=> Rational(1,86400000000000000),
|
|
"femtosecond"=> Rational(1,86400000000000000000),
|
|
"attosecond"=> Rational(1,86400000000000000000000),
|
|
"zeptosecond"=> Rational(1,86400000000000000000000000),
|
|
"yoctosecond"=> Rational(1,86400000000000000000000000000)}
|
|
|
|
end
|
|
|
|
def test_constants
|
|
assert_equal(Date::Delta::UNITS, @units)
|
|
end
|
|
|
|
|
|
def test_singleton_methods
|
|
assert_nothing_raised(NameError) do
|
|
Date::Delta.method(:diff)
|
|
Date::Delta.method(:parse)
|
|
Date::Delta.method(:delta_to_dhms)
|
|
Date::Delta.method(:dhms_to_delta)
|
|
Date::Delta::UNITS.each_key do |k|
|
|
Date::Delta.method(k + 's')
|
|
end
|
|
end
|
|
end
|
|
|
|
def test_methods
|
|
delta = Date::Delta.new(1)
|
|
assert_nothing_raised(NameError) do
|
|
delta.method(:delta)
|
|
delta.method(:dhms)
|
|
end
|
|
end
|
|
|
|
def test_delta_to_dhms
|
|
assert_raise(ArgumentError) do
|
|
Date::Delta.delta_to_dhms()
|
|
end
|
|
assert_raise(ArgumentError) do
|
|
Date::Delta.delta_to_dhms(1,1,1)
|
|
end
|
|
assert_equal(Date::Delta.delta_to_dhms(1),
|
|
[0, 0, 1, 0, 0, 0, (0/1)])
|
|
assert_equal(Date::Delta.delta_to_dhms(Complex::I),
|
|
[0, 1, 0, 0, 0, 0, (0/1)])
|
|
assert_equal(Date::Delta.delta_to_dhms(Rational(1,3000)),
|
|
[0, 0, 0, 0, 0, 28, Rational(1,108000)])
|
|
end
|
|
|
|
def test_dhms_to_delta
|
|
assert_raise(ArgumentError) do
|
|
Date::Delta.dhms_to_delta(*Array.new(6,1))
|
|
end
|
|
assert_raise(ArgumentError) do
|
|
Date::Delta.dhms_to_delta(*Array.new(8,1))
|
|
end
|
|
assert_equal(Date::Delta.dhms_to_delta(*Array.new(7,1)),
|
|
Complex(Rational(45031,43200),13))
|
|
end
|
|
|
|
def test_new
|
|
assert_equal(Date::Delta.new(1).class,Date::Delta)
|
|
d1 = Date::Delta.new(@fix1[:days],
|
|
@fix1[:hours],
|
|
@fix1[:minutes],
|
|
@fix1[:seconds])
|
|
@fix1.each do |k,v|
|
|
assert_equal(d1.send(k), v)
|
|
end
|
|
d2 = Date::Delta.new(Complex::I * ( @fix2[:months] + 12* @fix2[:years]))
|
|
@fix2.each do |k,v|
|
|
assert_equal(d2.send(k),v)
|
|
end
|
|
end
|
|
|
|
def test_parse
|
|
@parsestrings.each do |k,v|
|
|
assert_equal(Date::Delta.parse(k), v)
|
|
end
|
|
end
|
|
|
|
def test_diff
|
|
date1 = Date.new(2011)
|
|
date2 = Date.new(2011,2,11)
|
|
assert_equal Date::Delta.diff(date1,date2),
|
|
Date::Delta.new(-41)
|
|
assert_equal Date::Delta.diff(date2,date1),
|
|
Date::Delta.new(41)
|
|
end
|
|
|
|
def test_dhms
|
|
delta = Date::Delta.new(1)
|
|
assert_equal(delta.dhms, [0, 0, 1, 0, 0, 0, 0])
|
|
end
|
|
|
|
def set_and_get_with_units
|
|
@fix1.each do |k,v|
|
|
delta = Date::Delta.new
|
|
delta += Date::Delta.send(k,v)
|
|
assert(delta.send(k),v)
|
|
end
|
|
end
|
|
|
|
def test_plus
|
|
assert_equal((Date::Delta.hours(1) + Date::Delta.months(1)).hours,1)
|
|
assert_equal((Date::Delta.hours(1) + Date::Delta.months(1)).months,1)
|
|
|
|
end
|
|
|
|
def test_minus
|
|
assert_equal(- Date::Delta.hours(1), Date::Delta.hours(-1))
|
|
assert_equal(Date::Delta.new - Date::Delta.hours,
|
|
Date::Delta.hours(-1))
|
|
end
|
|
|
|
def test_mult
|
|
assert_equal(Date::Delta.hours(1)*3,Date::Delta.hours(3))
|
|
end
|
|
|
|
def test_div
|
|
assert_equal(Date::Delta.hours(3)/3, Date::Delta.hours(1))
|
|
assert_equal(Date::Delta.years(1)/12,Date::Delta.months(1))
|
|
end
|
|
|
|
def test_date_plus
|
|
amonth = Date::Delta.months(1)
|
|
assert_equal(Date.new(2011,2,15) + amonth, Date.new(2011,3,15))
|
|
assert_equal(Date.new(2011,1,31) + amonth, Date.new(2011,2,28))
|
|
assert_equal(Date.new(2011,1,31) + amonth *2,Date.new(2011,3,31))
|
|
end
|
|
|
|
def test_date_minus
|
|
amonth = Date::Delta.months(1)
|
|
assert_equal(Date.new(2011,2,11) - amonth, Date.new(2011,1,11))
|
|
end
|
|
end
|