diff options
Diffstat (limited to 'spec/ruby/library/date/shared')
| -rw-r--r-- | spec/ruby/library/date/shared/civil.rb | 57 | ||||
| -rw-r--r-- | spec/ruby/library/date/shared/parse.rb | 54 | ||||
| -rw-r--r-- | spec/ruby/library/date/shared/parse_eu.rb | 37 | ||||
| -rw-r--r-- | spec/ruby/library/date/shared/parse_us.rb | 36 |
4 files changed, 184 insertions, 0 deletions
diff --git a/spec/ruby/library/date/shared/civil.rb b/spec/ruby/library/date/shared/civil.rb new file mode 100644 index 0000000000..4499cdf8e9 --- /dev/null +++ b/spec/ruby/library/date/shared/civil.rb @@ -0,0 +1,57 @@ +describe :date_civil, shared: true do + it "creates a Date for -4712 by default" do + # the #chomp calls are necessary because of RSpec + d = Date.send(@method) + d.year.should == -4712 + d.month.should == 1 + d.day.should == 1 + d.should.julian? + d.jd.should == 0 + end + + it "creates a date with arguments" do + d = Date.send(@method, 2000, 3, 5) + d.year.should == 2000 + d.month.should == 3 + d.day.should == 5 + d.should_not.julian? + d.jd.should == 2451609 + + # Should also work with years far in the past and future + + d = Date.send(@method, -9000, 7, 5) + d.year.should == -9000 + d.month.should == 7 + d.day.should == 5 + d.should.julian? + d.jd.should == -1566006 + + d = Date.send(@method, 9000, 10, 14) + d.year.should == 9000 + d.month.should == 10 + d.day.should == 14 + d.should_not.julian? + d.jd.should == 5008529 + + end + + it "doesn't create dates for invalid arguments" do + -> { Date.send(@method, 2000, 13, 31) }.should.raise(ArgumentError) + -> { Date.send(@method, 2000, 12, 32) }.should.raise(ArgumentError) + -> { Date.send(@method, 2000, 2, 30) }.should.raise(ArgumentError) + -> { Date.send(@method, 1900, 2, 29) }.should.raise(ArgumentError) + -> { Date.send(@method, 2000, 2, 29) }.should_not.raise(ArgumentError) + + -> { Date.send(@method, 1582, 10, 14) }.should.raise(ArgumentError) + -> { Date.send(@method, 1582, 10, 15) }.should_not.raise(ArgumentError) + + end + + it "creates a Date for different calendar reform dates" do + d1 = Date.send(@method, 1582, 10, 4) + d1.succ.day.should == 15 + + d2 = Date.send(@method, 1582, 10, 4, Date::ENGLAND) + d2.succ.day.should == 5 + end +end diff --git a/spec/ruby/library/date/shared/parse.rb b/spec/ruby/library/date/shared/parse.rb new file mode 100644 index 0000000000..40af908386 --- /dev/null +++ b/spec/ruby/library/date/shared/parse.rb @@ -0,0 +1,54 @@ +describe :date_parse, shared: true do + it "can parse a mmm-YYYY string into a Date object" do + d = Date.parse("feb#{@sep}2008") + d.year.should == 2008 + d.month.should == 2 + d.day.should == 1 + end + + it "can parse a 'DD mmm YYYY' string into a Date object" do + d = Date.parse("23#{@sep}feb#{@sep}2008") + d.year.should == 2008 + d.month.should == 2 + d.day.should == 23 + end + + it "can parse a 'DD mmm YYYY' string into a Date object" do + d = Date.parse("23#{@sep}feb#{@sep}2008") + d.year.should == 2008 + d.month.should == 2 + d.day.should == 23 + end + + it "can parse a 'YYYY mmm DD' string into a Date object" do + d = Date.parse("2008#{@sep}feb#{@sep}23") + d.year.should == 2008 + d.month.should == 2 + d.day.should == 23 + end + + it "can parse a month name and day into a Date object" do + d = Date.parse("november#{@sep}5th") + d.should == Date.civil(Date.today.year, 11, 5) + end + + it "can parse a month name, day and year into a Date object" do + d = Date.parse("november#{@sep}5th#{@sep}2005") + d.should == Date.civil(2005, 11, 5) + end + + it "can parse a year, month name and day into a Date object" do + d = Date.parse("2005#{@sep}november#{@sep}5th") + d.should == Date.civil(2005, 11, 5) + end + + it "can parse a day, month name and year into a Date object" do + d = Date.parse("5th#{@sep}november#{@sep}2005") + d.should == Date.civil(2005, 11, 5) + end + + it "can handle negative year numbers" do + d = Date.parse("5th#{@sep}november#{@sep}-2005") + d.should == Date.civil(-2005, 11, 5) + end +end diff --git a/spec/ruby/library/date/shared/parse_eu.rb b/spec/ruby/library/date/shared/parse_eu.rb new file mode 100644 index 0000000000..3819524a57 --- /dev/null +++ b/spec/ruby/library/date/shared/parse_eu.rb @@ -0,0 +1,37 @@ +describe :date_parse_eu, shared: true do + # The - separator let's it work like European format, so it as a different spec + it "can parse a YYYY-MM-DD string into a Date object" do + d = Date.parse("2007#{@sep}10#{@sep}01") + d.year.should == 2007 + d.month.should == 10 + d.day.should == 1 + end + + it "can parse a DD-MM-YYYY string into a Date object" do + d = Date.parse("10#{@sep}01#{@sep}2007") + d.year.should == 2007 + d.month.should == 1 + d.day.should == 10 + end + + it "can parse a YY-MM-DD string into a Date object" do + d = Date.parse("10#{@sep}01#{@sep}07") + d.year.should == 2010 + d.month.should == 1 + d.day.should == 7 + end + + it "can parse a YY-MM-DD string into a Date object NOT using the year digits as 20XX" do + d = Date.parse("10#{@sep}01#{@sep}07", false) + d.year.should == 10 + d.month.should == 1 + d.day.should == 7 + end + + it "can parse a YY-MM-DD string into a Date object using the year digits as 20XX" do + d = Date.parse("10#{@sep}01#{@sep}07", true) + d.year.should == 2010 + d.month.should == 1 + d.day.should == 7 + end +end diff --git a/spec/ruby/library/date/shared/parse_us.rb b/spec/ruby/library/date/shared/parse_us.rb new file mode 100644 index 0000000000..17e2fc96c1 --- /dev/null +++ b/spec/ruby/library/date/shared/parse_us.rb @@ -0,0 +1,36 @@ +describe :date_parse_us, shared: true do + it "parses a YYYY#{@sep}MM#{@sep}DD string into a Date object" do + d = Date.parse("2007#{@sep}10#{@sep}01") + d.year.should == 2007 + d.month.should == 10 + d.day.should == 1 + end + + it "parses a DD#{@sep}MM#{@sep}YYYY string into a Date object" do + d = Date.parse("10#{@sep}01#{@sep}2007") + d.year.should == 2007 + d.month.should == 1 + d.day.should == 10 + end + + it "parses a YY#{@sep}MM#{@sep}DD string into a Date object" do + d = Date.parse("10#{@sep}01#{@sep}07") + d.year.should == 2010 + d.month.should == 1 + d.day.should == 7 + end + + it "parses a YY#{@sep}MM#{@sep}DD string into a Date object NOT using the year digits as 20XX" do + d = Date.parse("10#{@sep}01#{@sep}07", false) + d.year.should == 10 + d.month.should == 1 + d.day.should == 7 + end + + it "parses a YY#{@sep}MM#{@sep}DD string into a Date object using the year digits as 20XX" do + d = Date.parse("10#{@sep}01#{@sep}07", true) + d.year.should == 2010 + d.month.should == 1 + d.day.should == 7 + end +end |
