Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Add specs to make DateTime subclass methods return subclass instances
Browse files Browse the repository at this point in the history
Similar as previous change made for Date methods.
  • Loading branch information
jeremyevans committed Oct 18, 2010
1 parent 282ebfe commit 1d71f7e
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 2 deletions.
5 changes: 5 additions & 0 deletions spec/datetime/add_month_spec.rb
Expand Up @@ -17,6 +17,11 @@
d.should == DateTime.civil(2008, 4, 30)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.>>(10).should be_kind_of(c)
end

it "should raise an error on non numeric parameters" do
lambda { DateTime.civil(2007,2,27) >> "hello" }.should raise_error(TypeError)
lambda { DateTime.civil(2007,2,27) >> DateTime.new }.should raise_error(TypeError)
Expand Down
5 changes: 5 additions & 0 deletions spec/datetime/add_spec.rb
Expand Up @@ -26,6 +26,11 @@
d.should == DateTime.civil(2007, 2, 16, 12)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.+(10).should be_kind_of(c)
end

it "should raise an error on non numeric parameters" do
lambda { DateTime.civil(2007,2,27) + :hello }.should raise_error
lambda { DateTime.civil(2007,2,27) + "hello" }.should raise_error
Expand Down
10 changes: 10 additions & 0 deletions spec/datetime/constructor_spec.rb
Expand Up @@ -139,4 +139,14 @@
DateTime.ordinal(2008, 1, 0, 0, 0, 0).should == DateTime.ordinal(2008, 1, 0, 0, 0, 0)
DateTime.ordinal(2008, 1, 1, 1, 1, 0.5, 1).should == DateTime.ordinal(2008, 1, 1, 1, 1, 0.5)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.should be_kind_of(c)
c.civil.should be_kind_of(c)
c.commercial.should be_kind_of(c)
c.ordinal.should be_kind_of(c)
c.new!.should be_kind_of(c)
c.new.should be_kind_of(c)
end
end
5 changes: 5 additions & 0 deletions spec/datetime/conversions_spec.rb
Expand Up @@ -9,6 +9,11 @@
DateTime.new(2008, 1, 1).new_offset(0.5).should == DateTime.new(2008, 1, 1)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.new_offset.should be_kind_of(c)
end

ruby_version_is "" ... "1.9" do
it "#newof should be a separate datetime with a modified offset" do
DateTime.new(2008, 1, 1).newof(0.5).to_s.should == DateTime.new(2008, 1, 1, 12, 0, 0, 0.5).to_s
Expand Down
6 changes: 6 additions & 0 deletions spec/datetime/downto_spec.rb
Expand Up @@ -36,4 +36,10 @@
count.should == 17
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.downto(c.jd - 2) do |d|
d.should be_kind_of(c)
end.should be_kind_of(c)
end
end
7 changes: 5 additions & 2 deletions spec/datetime/minus_month_spec.rb
@@ -1,7 +1,6 @@
require File.expand_path('../../spec_helper', __FILE__)

describe "DateTime#<<" do

it "should substract a number of months from a date" do
(DateTime.civil(2007, 12, 27) << 10).should == DateTime.civil(2007,2,27)
(DateTime.commercial(2007, 45, 5) << 10).should == DateTime.commercial(2007,2,2)
Expand All @@ -16,10 +15,14 @@
d.should == DateTime.civil(2008, 2, 29)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.<<(10).should be_kind_of(c)
end

it "should raise an error on non numeric parameters" do
lambda { DateTime.civil(2007,2,27) << "hello" }.should raise_error(TypeError)
lambda { DateTime.civil(2007,2,27) << DateTime.new }.should raise_error(TypeError)
lambda { DateTime.civil(2007,2,27) << Object.new }.should raise_error(TypeError)
end

end
5 changes: 5 additions & 0 deletions spec/datetime/minus_spec.rb
Expand Up @@ -16,6 +16,11 @@
(DateTime.ordinal(2008, 325, 6) - 315.25).should == DateTime.ordinal(2008, 10)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.-(10).should be_kind_of(c)
end

it "should substract a negative number of days from a DateTime" do
d = DateTime.civil(2007, 4, 19).-(-13)
d.should == DateTime.civil(2007, 5 ,2)
Expand Down
30 changes: 30 additions & 0 deletions spec/datetime/next_prev_spec.rb
Expand Up @@ -18,6 +18,11 @@
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).next_day).day_fraction.should == 0.5
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).next_day).offset.should == 0.5
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.next_day.should be_kind_of(c)
end
end

describe "DateTime#prev_day" do
Expand All @@ -37,6 +42,11 @@
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).prev_day).day_fraction.should == 0.5
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).prev_day).offset.should == 0.5
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.prev_day.should be_kind_of(c)
end
end

describe "DateTime#next_month" do
Expand All @@ -62,6 +72,11 @@
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).next_month).day_fraction.should == 0.5
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).next_month).offset.should == 0.5
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.next_month.should be_kind_of(c)
end
end

describe "DateTime#prev_month" do
Expand All @@ -87,6 +102,11 @@
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).prev_month).day_fraction.should == 0.5
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).prev_month).offset.should == 0.5
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.prev_month.should be_kind_of(c)
end
end

describe "DateTime#next_year" do
Expand All @@ -110,6 +130,11 @@
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).next_year).day_fraction.should == 0.5
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).next_year).offset.should == 0.5
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.next_year.should be_kind_of(c)
end
end

describe "DateTime#prev_year" do
Expand All @@ -133,6 +158,11 @@
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).prev_year).day_fraction.should == 0.5
(DateTime.civil(2007,2,27, 12, 0, 0, 0.5).prev_year).offset.should == 0.5
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.prev_year.should be_kind_of(c)
end
end

end
5 changes: 5 additions & 0 deletions spec/datetime/now_spec.rb
Expand Up @@ -15,4 +15,9 @@
it "should accept an optional sg value" do
DateTime.now(1).to_s.should == DateTime.now.to_s
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.now.should be_kind_of(c)
end
end
6 changes: 6 additions & 0 deletions spec/datetime/parse_spec.rb
Expand Up @@ -18,6 +18,12 @@
DateTime.parse.should == DateTime.jd
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.parse.should be_kind_of(c)
c.parse('2008-10-11').should be_kind_of(c)
end

it "can't handle a empty string" do
lambda{ DateTime.parse("") }.should raise_error(ArgumentError)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/datetime/parsing_spec.rb
Expand Up @@ -77,5 +77,16 @@
it ".xmlschema should parse an ISO8601 format" do
DateTime.xmlschema("2009-01-02T03:04:05+12:00").should == DateTime.new(2009, 1, 2, 3, 4, 5, 0.5)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.httpdate("Fri, 02 Jan 2009 00:00:00 GMT").should be_kind_of(c)
c.iso8601("2009-01-02").should be_kind_of(c)
c.jisx0301("H21.01.02").should be_kind_of(c)
c.rfc2822("Fri, 2 Jan 2009 00:00:00 +0000").should be_kind_of(c)
c.rfc822("Fri, 2 Jan 2009 00:00:00 +0000").should be_kind_of(c)
c.rfc3339("2009-01-02T00:00:00+00:00").should be_kind_of(c)
c.xmlschema("2009-01-02").should be_kind_of(c)
end
end
end
6 changes: 6 additions & 0 deletions spec/datetime/step_spec.rb
Expand Up @@ -174,4 +174,10 @@

end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.step(c.jd + 2) do |d|
d.should be_kind_of(c)
end.should be_kind_of(c)
end
end
6 changes: 6 additions & 0 deletions spec/datetime/strptime_spec.rb
Expand Up @@ -21,6 +21,12 @@
DateTime.strptime("2000-04-06T10:11:12+00:00").should == DateTime.civil(2000, 4, 6, 10, 11, 12)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.strptime.should be_kind_of(c)
c.strptime('2008-10-11', '%Y-%m-%d').should be_kind_of(c)
end

it "should be able to parse the hour in a 24 hour clock with leading zero" do
DateTime.strptime("10", '%H').should == DateTime.civil(@t.year, @t.mon, @t.day, 10, 0, 0)
DateTime.strptime("09", '%H').should == DateTime.civil(@t.year, @t.mon, @t.day, 9, 0, 0)
Expand Down
5 changes: 5 additions & 0 deletions spec/datetime/succ_spec.rb
Expand Up @@ -14,6 +14,11 @@
ds.succ.should == DateTime.ordinal(2009, 1)
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.succ.should be_kind_of(c)
end

it "should be aliased as next" do
DateTime.civil(2008, 10, 11).next.should == DateTime.civil(2008, 10, 12)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/datetime/upto_spec.rb
Expand Up @@ -36,4 +36,10 @@
count.should == 13
end

it "should keep the same class as the receiver" do
c = Class.new(DateTime)
c.jd.upto(c.jd + 2) do |d|
d.should be_kind_of(c)
end.should be_kind_of(c)
end
end

0 comments on commit 1d71f7e

Please sign in to comment.