Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 3 commits into from

2 participants

Gibheer Brian Shirai
Gibheer
Collaborator

There was a small glitch with Times between 23:59:59 and 00:00, so that rbx was missing a second every day.

Brian Shirai brixen merged commit fcf3b3d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
9 lib/date.rb
View
@@ -698,12 +698,9 @@ def self.valid_time? (h, min, s)
h += 24 if h < 0
min += 60 if min < 0
s += 60 if s < 0
- return unless ((0..23) === h &&
- (0..59) === min &&
- (0..59) === s) ||
- (24 == h &&
- 0 == min &&
- 0 == s)
+ return if s < 0 || s >= 60
+ return if min < 0 || min >= 60
+ return if h < 0 || h > 24 || (24 == h && (0 != min || 0 != s))
time_to_day_fraction(h, min, s)
end
52 spec/ruby/library/datetime/hour_spec.rb
View
@@ -1,6 +1,56 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require 'mspec/helpers/datetime'
require 'date'
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
52 spec/ruby/library/datetime/min_spec.rb
View
@@ -1,6 +1,56 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require 'mspec/helpers/datetime'
require 'date'
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
46 spec/ruby/library/datetime/minute_spec.rb
View
@@ -1,6 +1,48 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require 'mspec/helpers/datetime'
require 'date'
-describe "DateTime#minute" do
- it "needs to be reviewed for spec completeness"
+describe "DateTime#min" do
+ 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
49 spec/ruby/library/datetime/new_spec.rb
View
@@ -1,6 +1,53 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require 'mspec/helpers/hash'
require 'date'
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
54 spec/ruby/library/datetime/sec_spec.rb
View
@@ -1,6 +1,58 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require 'mspec/helpers/datetime'
require 'date'
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
45 spec/ruby/library/datetime/second_spec.rb
View
@@ -1,6 +1,49 @@
require File.expand_path('../../../spec_helper', __FILE__)
+require 'mspec/helpers/datetime'
require 'date'
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
Something went wrong with that request. Please try again.