Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new tests for DateTime with a fix for Times between 23h and 24h #878

Merged
merged 3 commits into from
May 15, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/date.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -698,12 +698,9 @@ def self.valid_time? (h, min, s)
h += 24 if h < 0 h += 24 if h < 0
min += 60 if min < 0 min += 60 if min < 0
s += 60 if s < 0 s += 60 if s < 0
return unless ((0..23) === h && return if s < 0 || s >= 60
(0..59) === min && return if min < 0 || min >= 60
(0..59) === s) || return if h < 0 || h > 24 || (24 == h && (0 != min || 0 != s))
(24 == h &&
0 == min &&
0 == s)
time_to_day_fraction(h, min, s) time_to_day_fraction(h, min, s)
end end


Expand Down
52 changes: 51 additions & 1 deletion spec/ruby/library/datetime/hour_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,56 @@
require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../../../spec_helper', __FILE__)
require 'mspec/helpers/datetime'
require 'date' require 'date'


describe "DateTime#hour" do describe "DateTime#hour" do
it "needs to be reviewed for spec completeness" it "returns 0 if no argument is passed" do
DateTime.new.hour.should == 0
end

it "returns the hour given as argument" do
new_datetime(:hour => 5).hour.should == 5
end

it "adds 24 to negative hours" do
new_datetime(:hour => -10).hour.should == 14
end

it "returns the absolute value of a Rational" do
new_datetime(:hour => 1 + Rational(1,2)).hour.should == 1
end

ruby_version_is "" .. "1.9" do
it "raises an error for Float" do
lambda { new_datetime :hour => 1.5 }.should raise_error(NoMethodError)
end
end

ruby_version_is "1.9" do
it "returns the absolute value of a Float" do
new_datetime(:hour => 1.5).hour.should == 1
end
end

it "returns a fraction of a day" do
new_datetime(:day => 1 + Rational(1,2)).hour.should == 12
end

it "raises an error, when the hour is smaller than -24" do
lambda { new_datetime(:hour => -25) }.should raise_error(ArgumentError)
end

it "raises an error, when the hour is larger than 24" do
lambda { new_datetime(:hour => 25) }.should raise_error(ArgumentError)
end

it "raises an error for hour fractions smaller than -24" do
lambda { new_datetime(:hour => -24 - Rational(1,2)) }.should(
raise_error(ArgumentError))
end

it "adds 1 to day, when 24 hours given" do
d = new_datetime :day => 1, :hour => 24
d.hour.should == 0
d.day.should == 2
end
end end
52 changes: 51 additions & 1 deletion spec/ruby/library/datetime/min_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,56 @@
require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../../../spec_helper', __FILE__)
require 'mspec/helpers/datetime'
require 'date' require 'date'


describe "DateTime#min" do describe "DateTime#min" do
it "needs to be reviewed for spec completeness" it "returns 0 if no argument is passed" do
DateTime.new.min.should == 0
end

it "returns the minute passed as argument" do
new_datetime(:minute => 5).min.should == 5
end

it "adds 60 to negative minutes" do
new_datetime(:minute => -20).min.should == 40
end

it "returns the absolute value of a Rational" do
new_datetime(:minute => 5 + Rational(1,2)).min.should == 5
end

ruby_version_is "" .. "1.9" do
it "raises an error for Float" do
lambda { new_datetime :minute => 5.5 }.should raise_error(NoMethodError)
end
end

ruby_version_is "1.9" do
it "returns the absolute value of a Float" do
new_datetime(:minute => 5.5).min.should == 5
end
end

it "returns a fraction of an hour" do
new_datetime(:hour => 2 + Rational(1,2)).min.should == 30
end

it "raises an error, when the minute is smaller than -60" do
lambda { new_datetime(:minute => -61) }.should raise_error(ArgumentError)
end

it "raises an error, when the minute is greater or equal than 60" do
lambda { new_datetime(:minute => 60) }.should raise_error(ArgumentError)
end

it "raises an error for minute fractions smaller than -60" do
lambda { new_datetime(:minute => -60 - Rational(1,2))}.should(
raise_error(ArgumentError))
end

ruby_version_is "1.8.7" do
it "takes a minute fraction near 60" do
new_datetime(:minute => 59 + Rational(1,2)).min.should == 59
end
end
end end
46 changes: 44 additions & 2 deletions spec/ruby/library/datetime/minute_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,48 @@
require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../../../spec_helper', __FILE__)
require 'mspec/helpers/datetime'
require 'date' require 'date'


describe "DateTime#minute" do describe "DateTime#min" do
it "needs to be reviewed for spec completeness" ruby_version_is "1.9" do
it "returns 0 if no argument is passed" do
DateTime.new.minute.should == 0
end

it "returns the minute passed as argument" do
new_datetime(:minute => 5).minute.should == 5
end

it "adds 60 to negative minutes" do
new_datetime(:minute => -20).minute.should == 40
end

it "returns the absolute value of a Rational" do
new_datetime(:minute => 5 + Rational(1,2)).minute.should == 5
end

it "returns the absolute value of a Float" do
new_datetime(:minute => 5.5).minute.should == 5
end

it "returns a fraction of an hour" do
new_datetime(:hour => 2 + Rational(1,2)).minute.should == 30
end

it "raises an error, when the minute is smaller than -60" do
lambda { new_datetime(:minute => -61) }.should raise_error(ArgumentError)
end

it "raises an error, when the minute is greater or equal than 60" do
lambda { new_datetime(:minute => 60) }.should raise_error(ArgumentError)
end

it "raises an error for minute fractions smaller than -60" do
lambda { new_datetime(:minute => -60 - Rational(1,2))}.should(
raise_error(ArgumentError))
end

it "takes a minute fraction near 60" do
new_datetime(:minute => 59 + Rational(1,2)).minute.should == 59
end
end
end end
49 changes: 48 additions & 1 deletion spec/ruby/library/datetime/new_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,53 @@
require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../../../spec_helper', __FILE__)
require 'mspec/helpers/hash'
require 'date' require 'date'


describe "DateTime.new" do describe "DateTime.new" do
it "needs to be reviewed for spec completeness" it "sets all values to default if passed no arguments" do
d = DateTime.new
d.year.should == -4712
d.month.should == 1
d.day.should == 1
d.hour.should == 0
d.min.should == 0
d.sec.should == 0
d.sec_fraction.should == 0
d.offset.should == 0
end

it "takes the first argument as year" do
DateTime.new(2011).year.should == 2011
end

it "takes the second argument as month" do
DateTime.new(2011, 2).month.should == 2
end

it "takes the third argument as day" do
DateTime.new(2011, 2, 3).day.should == 3
end

it "takes the forth argument as hour" do
DateTime.new(2011, 2, 3, 4).hour.should == 4
end

it "takes the fifth argument as minute" do
DateTime.new(1, 2, 3, 4, 5).min.should == 5
end

it "takes the sixth argument as second" do
DateTime.new(1, 2, 3, 4, 5, 6).sec.should == 6
end

it "takes the seventh argument as an offset" do
DateTime.new(1, 2, 3, 4, 5, 6, 7).offset.should == 7
end

it "takes the eigth argument as the date of calendar reform" do
DateTime.new(1, 2, 3, 4, 5, 6, 7, Date::ITALY).start().should == Date::ITALY
end

it "raises an error on invalid arguments" do
lambda { new_datetime(:minute => 999) }.should raise_error(ArgumentError)
end
end end
54 changes: 53 additions & 1 deletion spec/ruby/library/datetime/sec_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,58 @@
require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../../../spec_helper', __FILE__)
require 'mspec/helpers/datetime'
require 'date' require 'date'


describe "DateTime#sec" do describe "DateTime#sec" do
it "needs to be reviewed for spec completeness" it "returns 0 seconds if passed no arguments" do
d = DateTime.new
d.sec.should == 0
end

it "returns the seconds passed in the arguments" do
new_datetime(:second => 5).sec.should == 5
end

it "adds 60 to negative values" do
new_datetime(:second => -20).sec.should == 40
end

it "returns the absolute value of a Rational" do
new_datetime(:second => 5 + Rational(1,2)).sec.should == 5
end

ruby_version_is "" .. "1.9" do
it "raises an error when second is given as a float" do
# Float has no method to_r
lambda { new_datetime(:second => 5.5) }.should raise_error(NoMethodError)
end
end

ruby_version_is "1.9" do
it "returns the absolute value of a float" do
new_datetime(:second => 5.5).sec.should == 5
end
end

it "displays the fraction of a minute" do
new_datetime(:minute => 5 + Rational(1,2)).sec.should == 30
end

it "raises an error, when the second is smaller than -60" do
lambda { new_datetime(:second => -61) }.should raise_error(ArgumentError)
end

it "raises an error, when the second is greater or equal than 60" do
lambda { new_datetime(:second => 60) }.should raise_error(ArgumentError)
end

it "raises an error for second fractions smaller than -60" do
lambda { new_datetime(:second => -60 - Rational(1,2))}.should(
raise_error(ArgumentError))
end

ruby_version_is "1.8.7" do
it "takes a second fraction near 60" do
new_datetime(:second => 59 + Rational(1,2)).sec.should == 59
end
end
end end
45 changes: 44 additions & 1 deletion spec/ruby/library/datetime/second_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,49 @@
require File.expand_path('../../../spec_helper', __FILE__) require File.expand_path('../../../spec_helper', __FILE__)
require 'mspec/helpers/datetime'
require 'date' require 'date'


describe "DateTime#second" do describe "DateTime#second" do
it "needs to be reviewed for spec completeness" ruby_version_is "1.9" do
it "returns 0 seconds if passed no arguments" do
d = DateTime.new
d.second.should == 0
end

it "returns the seconds passed in the arguments" do
new_datetime(:second => 5).second.should == 5
end

it "adds 60 to negative values" do
new_datetime(:second => -20).second.should == 40
end

it "returns the absolute value of a Rational" do
new_datetime(:second => 5 + Rational(1,2)).second.should == 5
end

it "returns the absolute value of a float" do
new_datetime(:second => 5.5).second.should == 5
end

it "displays the fraction of a minute" do
new_datetime(:minute => 5 + Rational(1,2)).second.should == 30
end

it "raises an error, when the second is smaller than -60" do
lambda { new_datetime(:second => -61) }.should raise_error(ArgumentError)
end

it "raises an error, when the second is greater or equal than 60" do
lambda { new_datetime(:second => 60) }.should raise_error(ArgumentError)
end

it "raises an error for second fractions smaller than -60" do
lambda { new_datetime(:second => -60 - Rational(1,2))}.should(
raise_error(ArgumentError))
end

it "takes a second fraction near 60" do
new_datetime(:second => 59 + Rational(1,2)).second.should == 59
end
end
end end