| 6 | | before do |
| 7 | | @birth = DateTime.new(1984, 12, 26) |
| 8 | | @birth_after_a_year_before_one_day = DateTime.new(1985,12,25) |
| 9 | | @birth_after_a_year = DateTime.new(1985, 12,26) |
| 10 | | @dt = DateTime.new(2007, 12, 25) |
| 11 | | @dt2 = DateTime.new(2007, 12, 26) |
| 12 | | @dd = DateTime::Duration.new(@birth, @dt) |
| 13 | | @dd2 = DateTime::Duration.new(@birth, @dt2) |
| 14 | | @dd3 = DateTime::Duration.new(@birth, @birth_after_a_year_before_one_day) |
| 15 | | @dd4 = DateTime::Duration.new(@birth, @birth_after_a_year ) |
| 16 | | @dd5 = DateTime::Duration.new(@birth_after_a_year, @birth) |
| | 6 | describe "#days" do |
| | 7 | with_fixtures [:from, :to] => :days do |
| | 8 | it 'should return :days days in duration from :from to :to' do |input, days| |
| | 9 | DateTime::Duration.new(input[:from], input[:to]).days.should == days |
| | 10 | end |
| | 11 | |
| | 12 | filters({ |
| | 13 | :from => lambda {|val| DateTime.strptime(val, '%Y-%m-%d') }, |
| | 14 | :to => lambda {|val| DateTime.strptime(val, '%Y-%m-%d') }, |
| | 15 | }) |
| | 16 | |
| | 17 | set_fixtures([ |
| | 18 | [ ['1984-12-26', '2007-12-25'] => 8399 ], |
| | 19 | [ ['1984-12-26', '2007-12-26'] => 8400 ], |
| | 20 | [ ['1984-12-26', '1985-12-25'] => 364 ], |
| | 21 | [ ['1984-12-26', '1985-12-26'] => 365 ], |
| | 22 | [ ['1985-12-26', '1984-12-26'] => -365 ], |
| | 23 | ]) |
| | 24 | end |
| 19 | | it %{ should convert duration in days properly} do |
| 20 | | @dd.days.to_i.should == 8399 |
| 21 | | @dd2.days.to_i.should == 8400 |
| 22 | | @dd3.days.to_i.should == 364 |
| 23 | | @dd4.days.to_i.should == 365 |
| 24 | | @dd5.days.to_i.should == -365 |
| | 27 | describe "#months" do |
| | 28 | with_fixtures [:from, :to] => :months do |
| | 29 | it 'should return :months months in duration from :from to :to' do |input, months| |
| | 30 | DateTime::Duration.new(input[:from], input[:to]).months.should == months |
| | 31 | end |
| | 32 | |
| | 33 | filters({ |
| | 34 | :from => lambda {|val| DateTime.strptime(val, '%Y-%m-%d') }, |
| | 35 | :to => lambda {|val| DateTime.strptime(val, '%Y-%m-%d') }, |
| | 36 | }) |
| | 37 | |
| | 38 | set_fixtures([ |
| | 39 | [ ['1984-12-26', '2007-12-25'] => 275 ], |
| | 40 | [ ['1984-12-26', '2007-12-26'] => 276 ], |
| | 41 | [ ['1984-12-26', '1985-12-25'] => 11 ], |
| | 42 | [ ['1984-12-26', '1985-12-26'] => 12 ], |
| | 43 | [ ['1985-12-26', '1984-12-26'] => -12 ], |
| | 44 | ]) |
| | 45 | end |
| 27 | | it %{ should convert duration in months properly} do |
| 28 | | @dd.months.should == 275 |
| 29 | | @dd2.months.should == 276 |
| 30 | | @dd3.months.should == 11 |
| 31 | | @dd4.months.should == 12 |
| 32 | | @dd5.months.should == -12 |
| | 48 | describe "#years" do |
| | 49 | with_fixtures [:from, :to] => :years do |
| | 50 | it 'should return :years years in duration from :from to :to' do |input, years| |
| | 51 | DateTime::Duration.new(input[:from], input[:to]).years.should == years |
| | 52 | end |
| | 53 | |
| | 54 | filters({ |
| | 55 | :from => lambda {|val| DateTime.strptime(val, '%Y-%m-%d') }, |
| | 56 | :to => lambda {|val| DateTime.strptime(val, '%Y-%m-%d') }, |
| | 57 | }) |
| | 58 | |
| | 59 | set_fixtures([ |
| | 60 | [ ['1984-12-26', '2007-12-25'] => 22 ], |
| | 61 | [ ['1984-12-26', '2007-12-26'] => 23 ], |
| | 62 | [ ['1984-12-26', '1985-12-25'] => 0 ], |
| | 63 | [ ['1984-12-26', '1985-12-26'] => 1 ], |
| | 64 | [ ['1985-12-26', '1984-12-26'] => -1 ], |
| | 65 | ]) |
| | 66 | end |
| 43 | | it %{ should make from data} do |
| 44 | | DateTime::Duration.new2(@birth, :years => 22, :months => 11, :days => 30).should == @dd |
| 45 | | DateTime::Duration.new2(@birth, :years => 23, :days => -1).should == @dd |
| 46 | | DateTime::Duration.new2(@birth, :years => 23).should == @dd2 |
| 47 | | DateTime::Duration.new2(@birth, :years => 1, :days => -1).should == @dd3 |
| 48 | | DateTime::Duration.new2(@birth, :years => 1).should == @dd4 |
| 49 | | DateTime::Duration.new2(@birth_after_a_year, :years => -1) |
| | 79 | filters({ |
| | 80 | :from => lambda {|val| DateTime.strptime(val, '%Y-%m-%d') }, |
| | 81 | :duration => lambda {|val| |
| | 82 | DateTime::Duration.new( |
| | 83 | *val.map {|date_str| DateTime.strptime(date_str, '%Y-%m-%d') } |
| | 84 | ) |
| | 85 | } |
| | 86 | }) |
| | 87 | |
| | 88 | desc_filters({ |
| | 89 | :years => :to_s, |
| | 90 | :months => :to_s, |
| | 91 | :days => :to_s, |
| | 92 | :duration => lambda {|val| "(#{val.join(', ')})" }, |
| | 93 | }) |
| | 94 | |
| | 95 | set_fixtures([ |
| | 96 | [ ['1984-12-26', 22, 11, 30] => ['1984-12-26', '2007-12-25'] ], |
| | 97 | [ ['1984-12-26', 23, nil, -1] => ['1984-12-26', '2007-12-25'] ], |
| | 98 | [ ['1984-12-26', 23, nil, nil] => ['1984-12-26', '2007-12-26'] ], |
| | 99 | [ ['1984-12-26', 1, nil, -1] => ['1984-12-26', '1985-12-25'] ], |
| | 100 | [ ['1984-12-26', 1, nil, nil] => ['1984-12-26', '1985-12-26'] ], |
| | 101 | [ ['1985-12-26', -1, nil, nil] => ['1985-12-26', '1984-12-26'] ], |
| | 102 | ]) |
| | 103 | end |