diff options
Diffstat (limited to 'spec/ruby/core/time/shared')
-rw-r--r-- | spec/ruby/core/time/shared/asctime.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/day.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/getgm.rb | 9 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/gm.rb | 22 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/gmt_offset.rb | 53 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/gmtime.rb | 33 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/inspect.rb | 23 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/isdst.rb | 8 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/local.rb | 45 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/month.rb | 15 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/now.rb | 8 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/time_params.rb | 260 | ||||
-rw-r--r-- | spec/ruby/core/time/shared/to_i.rb | 9 |
13 files changed, 506 insertions, 0 deletions
diff --git a/spec/ruby/core/time/shared/asctime.rb b/spec/ruby/core/time/shared/asctime.rb new file mode 100644 index 0000000000..d096666863 --- /dev/null +++ b/spec/ruby/core/time/shared/asctime.rb @@ -0,0 +1,6 @@ +describe :time_asctime, shared: true do + it "returns a canonical string representation of time" do + t = Time.now + t.send(@method).should == t.strftime("%a %b %e %H:%M:%S %Y") + end +end diff --git a/spec/ruby/core/time/shared/day.rb b/spec/ruby/core/time/shared/day.rb new file mode 100644 index 0000000000..472dc959c1 --- /dev/null +++ b/spec/ruby/core/time/shared/day.rb @@ -0,0 +1,15 @@ +describe :time_day, shared: true do + it "returns the day of the month (1..n) for a local Time" do + with_timezone("CET", 1) do + Time.local(1970, 1, 1).send(@method).should == 1 + end + end + + it "returns the day of the month for a UTC Time" do + Time.utc(1970, 1, 1).send(@method).should == 1 + end + + it "returns the day of the month for a Time with a fixed offset" do + Time.new(2012, 1, 1, 0, 0, 0, -3600).send(@method).should == 1 + end +end diff --git a/spec/ruby/core/time/shared/getgm.rb b/spec/ruby/core/time/shared/getgm.rb new file mode 100644 index 0000000000..3576365772 --- /dev/null +++ b/spec/ruby/core/time/shared/getgm.rb @@ -0,0 +1,9 @@ +describe :time_getgm, shared: true do + it "returns a new time which is the utc representation of time" do + # Testing with America/Regina here because it doesn't have DST. + with_timezone("CST", -6) do + t = Time.local(2007, 1, 9, 6, 0, 0) + t.send(@method).should == Time.gm(2007, 1, 9, 12, 0, 0) + end + end +end diff --git a/spec/ruby/core/time/shared/gm.rb b/spec/ruby/core/time/shared/gm.rb new file mode 100644 index 0000000000..805e35766c --- /dev/null +++ b/spec/ruby/core/time/shared/gm.rb @@ -0,0 +1,22 @@ +describe :time_gm, shared: true do + it "creates a time based on given values, interpreted as UTC (GMT)" do + Time.send(@method, 2000,"jan",1,20,15,1).inspect.should == "2000-01-01 20:15:01 UTC" + end + + it "creates a time based on given C-style gmtime arguments, interpreted as UTC (GMT)" do + time = Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored) + time.inspect.should == "2000-01-01 20:15:01 UTC" + end + + it "interprets pre-Gregorian reform dates using Gregorian proleptic calendar" do + Time.send(@method, 1582, 10, 4, 12).to_i.should == -12220200000 # 2299150j + end + + it "interprets Julian-Gregorian gap dates using Gregorian proleptic calendar" do + Time.send(@method, 1582, 10, 14, 12).to_i.should == -12219336000 # 2299160j + end + + it "interprets post-Gregorian reform dates using Gregorian calendar" do + Time.send(@method, 1582, 10, 15, 12).to_i.should == -12219249600 # 2299161j + end +end diff --git a/spec/ruby/core/time/shared/gmt_offset.rb b/spec/ruby/core/time/shared/gmt_offset.rb new file mode 100644 index 0000000000..cb842be2f3 --- /dev/null +++ b/spec/ruby/core/time/shared/gmt_offset.rb @@ -0,0 +1,53 @@ +describe :time_gmt_offset, shared: true do + it "returns the offset in seconds between the timezone of time and UTC" do + with_timezone("AST", 3) do + Time.new.send(@method).should == 10800 + end + end + + platform_is_not :windows do + it "returns the correct offset for US Eastern time zone around daylight savings time change" do + # "2010-03-14 01:59:59 -0500" + 1 ==> "2010-03-14 03:00:00 -0400" + with_timezone("EST5EDT") do + t = Time.local(2010,3,14,1,59,59) + t.send(@method).should == -5*60*60 + (t + 1).send(@method).should == -4*60*60 + end + end + + it "returns the correct offset for Hawaii around daylight savings time change" do + # "2010-03-14 01:59:59 -1000" + 1 ==> "2010-03-14 02:00:00 -1000" + with_timezone("Pacific/Honolulu") do + t = Time.local(2010,3,14,1,59,59) + t.send(@method).should == -10*60*60 + (t + 1).send(@method).should == -10*60*60 + end + end + + it "returns the correct offset for New Zealand around daylight savings time change" do + # "2010-04-04 02:59:59 +1300" + 1 ==> "2010-04-04 02:00:00 +1200" + with_timezone("Pacific/Auckland") do + t = Time.local(2010,4,4,1,59,59) + (60 * 60) + t.send(@method).should == 13*60*60 + (t + 1).send(@method).should == 12*60*60 + end + end + end + + it "returns offset as Rational" do + Time.new(2010,4,4,1,59,59,7245).send(@method).should == 7245 + Time.new(2010,4,4,1,59,59,7245.5).send(@method).should == Rational(14491,2) + end + + context 'given positive offset' do + it 'returns a positive offset' do + Time.new(2013,3,17,nil,nil,nil,"+03:00").send(@method).should == 10800 + end + end + + context 'given negative offset' do + it 'returns a negative offset' do + Time.new(2013,3,17,nil,nil,nil,"-03:00").send(@method).should == -10800 + end + end +end diff --git a/spec/ruby/core/time/shared/gmtime.rb b/spec/ruby/core/time/shared/gmtime.rb new file mode 100644 index 0000000000..e684a1fd95 --- /dev/null +++ b/spec/ruby/core/time/shared/gmtime.rb @@ -0,0 +1,33 @@ +describe :time_gmtime, shared: true do + it "converts self to UTC, modifying the receiver" do + # Testing with America/Regina here because it doesn't have DST. + with_timezone("CST", -6) do + t = Time.local(2007, 1, 9, 6, 0, 0) + t.send(@method) + t.should == Time.gm(2007, 1, 9, 12, 0, 0) + end + end + + it "returns self" do + with_timezone("CST", -6) do + t = Time.local(2007, 1, 9, 12, 0, 0) + t.send(@method).should equal(t) + end + end + + describe "on a frozen time" do + it "does not raise an error if already in UTC" do + time = Time.gm(2007, 1, 9, 12, 0, 0) + time.freeze + time.send(@method).should equal(time) + end + + it "raises a RuntimeError if the time is not UTC" do + with_timezone("CST", -6) do + time = Time.now + time.freeze + lambda { time.send(@method) }.should raise_error(RuntimeError) + end + end + end +end diff --git a/spec/ruby/core/time/shared/inspect.rb b/spec/ruby/core/time/shared/inspect.rb new file mode 100644 index 0000000000..c707382a6e --- /dev/null +++ b/spec/ruby/core/time/shared/inspect.rb @@ -0,0 +1,23 @@ +# -*- encoding: us-ascii -*- + +describe :inspect, shared: true do + it "formats the local time following the pattern 'yyyy-MM-dd HH:mm:ss Z'" do + with_timezone("PST", +1) do + Time.local(2000, 1, 1, 20, 15, 1).send(@method).should == "2000-01-01 20:15:01 +0100" + end + end + + it "formats the UTC time following the pattern 'yyyy-MM-dd HH:mm:ss UTC'" do + Time.utc(2000, 1, 1, 20, 15, 1).send(@method).should == "2000-01-01 20:15:01 UTC" + end + + it "formats the fixed offset time following the pattern 'yyyy-MM-dd HH:mm:ss +/-HHMM'" do + Time.new(2000, 1, 1, 20, 15, 01, 3600).send(@method).should == "2000-01-01 20:15:01 +0100" + end + + with_feature :encoding do + it "returns a US-ASCII encoded string" do + Time.now.send(@method).encoding.should equal(Encoding::US_ASCII) + end + end +end diff --git a/spec/ruby/core/time/shared/isdst.rb b/spec/ruby/core/time/shared/isdst.rb new file mode 100644 index 0000000000..bc6d139230 --- /dev/null +++ b/spec/ruby/core/time/shared/isdst.rb @@ -0,0 +1,8 @@ +describe :time_isdst, shared: true do + it "dst? returns whether time is during daylight saving time" do + with_timezone("America/Los_Angeles") do + Time.local(2007, 9, 9, 0, 0, 0).send(@method).should == true + Time.local(2007, 1, 9, 0, 0, 0).send(@method).should == false + end + end +end diff --git a/spec/ruby/core/time/shared/local.rb b/spec/ruby/core/time/shared/local.rb new file mode 100644 index 0000000000..43f331c4c1 --- /dev/null +++ b/spec/ruby/core/time/shared/local.rb @@ -0,0 +1,45 @@ +describe :time_local, shared: true do + it "creates a time based on given values, interpreted in the local time zone" do + with_timezone("PST", -8) do + Time.send(@method, 2000, "jan", 1, 20, 15, 1).to_a.should == + [1, 15, 20, 1, 1, 2000, 6, 1, false, "PST"] + end + end + + platform_is_not :windows do + describe "timezone changes" do + it "correctly adjusts the timezone change to 'CEST' on 'Europe/Amsterdam'" do + with_timezone("Europe/Amsterdam") do + Time.send(@method, 1940, 5, 16).to_a.should == + [0, 40, 1, 16, 5, 1940, 4, 137, true, "CEST"] + end + end + end + end +end + +describe :time_local_10_arg, shared: true do + it "creates a time based on given C-style gmtime arguments, interpreted in the local time zone" do + with_timezone("PST", -8) do + Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored).to_a.should == + [1, 15, 20, 1, 1, 2000, 6, 1, false, "PST"] + end + end + + platform_is_not :windows do + it "creates the correct time just before dst change" do + with_timezone("America/New_York") do + time = Time.send(@method, 0, 30, 1, 30, 10, 2005, 0, 0, true, ENV['TZ']) + time.utc_offset.should == -4 * 3600 + end + end + + it "creates the correct time just after dst change" do + with_timezone("America/New_York") do + time = Time.send(@method, 0, 30, 1, 30, 10, 2005, 0, 0, false, ENV['TZ']) + time.utc_offset.should == -5 * 3600 + end + end + end + +end diff --git a/spec/ruby/core/time/shared/month.rb b/spec/ruby/core/time/shared/month.rb new file mode 100644 index 0000000000..31ca679557 --- /dev/null +++ b/spec/ruby/core/time/shared/month.rb @@ -0,0 +1,15 @@ +describe :time_month, shared: true do + it "returns the month of the year for a local Time" do + with_timezone("CET", 1) do + Time.local(1970, 1).send(@method).should == 1 + end + end + + it "returns the month of the year for a UTC Time" do + Time.utc(1970, 1).send(@method).should == 1 + end + + it "returns the four digit year for a Time with a fixed offset" do + Time.new(2012, 1, 1, 0, 0, 0, -3600).send(@method).should == 1 + end +end diff --git a/spec/ruby/core/time/shared/now.rb b/spec/ruby/core/time/shared/now.rb new file mode 100644 index 0000000000..f570aeedd2 --- /dev/null +++ b/spec/ruby/core/time/shared/now.rb @@ -0,0 +1,8 @@ +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :time_now, shared: true do + it "creates a subclass instance if called on a subclass" do + TimeSpecs::SubTime.send(@method).should be_an_instance_of(TimeSpecs::SubTime) + TimeSpecs::MethodHolder.send(@method).should be_an_instance_of(Time) + end +end diff --git a/spec/ruby/core/time/shared/time_params.rb b/spec/ruby/core/time/shared/time_params.rb new file mode 100644 index 0000000000..87b52d9f8d --- /dev/null +++ b/spec/ruby/core/time/shared/time_params.rb @@ -0,0 +1,260 @@ +describe :time_params, shared: true do + it "accepts 1 argument (year)" do + Time.send(@method, 2000).should == + Time.send(@method, 2000, 1, 1, 0, 0, 0) + end + + it "accepts 2 arguments (year, month)" do + Time.send(@method, 2000, 2).should == + Time.send(@method, 2000, 2, 1, 0, 0, 0) + end + + it "accepts 3 arguments (year, month, day)" do + Time.send(@method, 2000, 2, 3).should == + Time.send(@method, 2000, 2, 3, 0, 0, 0) + end + + it "accepts 4 arguments (year, month, day, hour)" do + Time.send(@method, 2000, 2, 3, 4).should == + Time.send(@method, 2000, 2, 3, 4, 0, 0) + end + + it "accepts 5 arguments (year, month, day, hour, minute)" do + Time.send(@method, 2000, 2, 3, 4, 5).should == + Time.send(@method, 2000, 2, 3, 4, 5, 0) + end + + it "accepts a too big day of the month by going to the next month" do + Time.send(@method, 1999, 2, 31).should == + Time.send(@method, 1999, 3, 3) + end + + it "raises a TypeError if the year is nil" do + lambda { Time.send(@method, nil) }.should raise_error(TypeError) + end + + it "accepts nil month, day, hour, minute, and second" do + Time.send(@method, 2000, nil, nil, nil, nil, nil).should == + Time.send(@method, 2000) + end + + it "handles a String year" do + Time.send(@method, "2000").should == + Time.send(@method, 2000) + end + + it "coerces the year with #to_int" do + m = mock(:int) + m.should_receive(:to_int).and_return(1) + Time.send(@method, m).should == Time.send(@method, 1) + end + + it "handles a String month given as a numeral" do + Time.send(@method, 2000, "12").should == + Time.send(@method, 2000, 12) + end + + it "handles a String month given as a short month name" do + Time.send(@method, 2000, "dec").should == + Time.send(@method, 2000, 12) + end + + it "coerces the month with #to_str" do + (obj = mock('12')).should_receive(:to_str).and_return("12") + Time.send(@method, 2008, obj).should == + Time.send(@method, 2008, 12) + end + + it "coerces the month with #to_int" do + m = mock(:int) + m.should_receive(:to_int).and_return(1) + Time.send(@method, 2008, m).should == Time.send(@method, 2008, 1) + end + + it "handles a String day" do + Time.send(@method, 2000, 12, "15").should == + Time.send(@method, 2000, 12, 15) + end + + it "coerces the day with #to_int" do + m = mock(:int) + m.should_receive(:to_int).and_return(1) + Time.send(@method, 2008, 1, m).should == Time.send(@method, 2008, 1, 1) + end + + it "handles a String hour" do + Time.send(@method, 2000, 12, 1, "5").should == + Time.send(@method, 2000, 12, 1, 5) + end + + it "coerces the hour with #to_int" do + m = mock(:int) + m.should_receive(:to_int).and_return(1) + Time.send(@method, 2008, 1, 1, m).should == Time.send(@method, 2008, 1, 1, 1) + end + + it "handles a String minute" do + Time.send(@method, 2000, 12, 1, 1, "8").should == + Time.send(@method, 2000, 12, 1, 1, 8) + end + + it "coerces the minute with #to_int" do + m = mock(:int) + m.should_receive(:to_int).and_return(1) + Time.send(@method, 2008, 1, 1, 0, m).should == Time.send(@method, 2008, 1, 1, 0, 1) + end + + it "handles a String second" do + Time.send(@method, 2000, 12, 1, 1, 1, "8").should == + Time.send(@method, 2000, 12, 1, 1, 1, 8) + end + + it "coerces the second with #to_int" do + m = mock(:int) + m.should_receive(:to_int).and_return(1) + Time.send(@method, 2008, 1, 1, 0, 0, m).should == Time.send(@method, 2008, 1, 1, 0, 0, 1) + end + + it "interprets all numerals as base 10" do + Time.send(@method, "2000", "08", "08", "08", "08", "08").should == + Time.send(@method, 2000, 8, 8, 8, 8, 8) + Time.send(@method, "2000", "09", "09", "09", "09", "09").should == + Time.send(@method, 2000, 9, 9, 9, 9, 9) + end + + it "handles fractional seconds as a Float" do + t = Time.send(@method, 2000, 1, 1, 20, 15, 1.75) + t.sec.should == 1 + t.usec.should == 750000 + end + + it "handles fractional seconds as a Rational" do + t = Time.send(@method, 2000, 1, 1, 20, 15, Rational(99, 10)) + t.sec.should == 9 + t.usec.should == 900000 + end + + it "handles years from 0 as such" do + 0.upto(2100) do |year| + t = Time.send(@method, year) + t.year.should == year + end + end + + it "accepts various year ranges" do + Time.send(@method, 1801, 12, 31, 23, 59, 59).wday.should == 4 + Time.send(@method, 3000, 12, 31, 23, 59, 59).wday.should == 3 + end + + it "raises an ArgumentError for out of range month" do + lambda { + Time.send(@method, 2008, 13, 31, 23, 59, 59) + }.should raise_error(ArgumentError) + end + + it "raises an ArgumentError for out of range day" do + lambda { + Time.send(@method, 2008, 12, 32, 23, 59, 59) + }.should raise_error(ArgumentError) + end + + it "raises an ArgumentError for out of range hour" do + lambda { + Time.send(@method, 2008, 12, 31, 25, 59, 59) + }.should raise_error(ArgumentError) + end + + it "raises an ArgumentError for out of range minute" do + lambda { + Time.send(@method, 2008, 12, 31, 23, 61, 59) + }.should raise_error(ArgumentError) + end + + it "raises an ArgumentError for out of range second" do + lambda { + Time.send(@method, 2008, 12, 31, 23, 59, 61) + }.should raise_error(ArgumentError) + end + + it "raises ArgumentError when given 9 arguments" do + lambda { Time.send(@method, *[0]*9) }.should raise_error(ArgumentError) + end + + it "raises ArgumentError when given 11 arguments" do + lambda { Time.send(@method, *[0]*11) }.should raise_error(ArgumentError) + end + + it "returns subclass instances" do + c = Class.new(Time) + c.send(@method, 2008, "12").should be_an_instance_of(c) + end +end + +describe :time_params_10_arg, shared: true do + it "handles string arguments" do + Time.send(@method, "1", "15", "20", "1", "1", "2000", :ignored, :ignored, + :ignored, :ignored).should == + Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored) + end + + it "handles float arguments" do + Time.send(@method, 1.0, 15.0, 20.0, 1.0, 1.0, 2000.0, :ignored, :ignored, + :ignored, :ignored).should == + Time.send(@method, 1, 15, 20, 1, 1, 2000, :ignored, :ignored, :ignored, :ignored) + end + + it "raises an ArgumentError for out of range values" do + lambda { + Time.send(@method, 61, 59, 23, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored) + }.should raise_error(ArgumentError) # sec + + lambda { + Time.send(@method, 59, 61, 23, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored) + }.should raise_error(ArgumentError) # min + + lambda { + Time.send(@method, 59, 59, 25, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored) + }.should raise_error(ArgumentError) # hour + + lambda { + Time.send(@method, 59, 59, 23, 32, 12, 2008, :ignored, :ignored, :ignored, :ignored) + }.should raise_error(ArgumentError) # day + + lambda { + Time.send(@method, 59, 59, 23, 31, 13, 2008, :ignored, :ignored, :ignored, :ignored) + }.should raise_error(ArgumentError) # month + end +end + +describe :time_params_microseconds, shared: true do + it "handles microseconds" do + t = Time.send(@method, 2000, 1, 1, 20, 15, 1, 123) + t.usec.should == 123 + end + + it "handles fractional microseconds as a Float" do + t = Time.send(@method, 2000, 1, 1, 20, 15, 1, 1.75) + t.usec.should == 1 + t.nsec.should == 1750 + end + + it "handles fractional microseconds as a Rational" do + t = Time.send(@method, 2000, 1, 1, 20, 15, 1, Rational(99, 10)) + t.usec.should == 9 + t.nsec.should == 9900 + end + + it "ignores fractional seconds if a passed whole number of microseconds" do + t = Time.send(@method, 2000, 1, 1, 20, 15, 1.75, 2) + t.sec.should == 1 + t.usec.should == 2 + t.nsec.should == 2000 + end + + it "ignores fractional seconds if a passed fractional number of microseconds" do + t = Time.send(@method, 2000, 1, 1, 20, 15, 1.75, Rational(99, 10)) + t.sec.should == 1 + t.usec.should == 9 + t.nsec.should == 9900 + end +end diff --git a/spec/ruby/core/time/shared/to_i.rb b/spec/ruby/core/time/shared/to_i.rb new file mode 100644 index 0000000000..03497c700b --- /dev/null +++ b/spec/ruby/core/time/shared/to_i.rb @@ -0,0 +1,9 @@ +describe :time_to_i, shared: true do + it "returns the value of time as an integer number of seconds since epoch" do + Time.at(0).send(@method).should == 0 + end + + it "doesn't return an actual number of seconds in time" do + Time.at(65.5).send(@method).should == 65 + end +end |