diff options
Diffstat (limited to 'spec/ruby/library/date')
123 files changed, 2144 insertions, 0 deletions
diff --git a/spec/ruby/library/date/accessor_spec.rb b/spec/ruby/library/date/accessor_spec.rb new file mode 100644 index 0000000000..74ed0e9c21 --- /dev/null +++ b/spec/ruby/library/date/accessor_spec.rb @@ -0,0 +1,91 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#ajd" do + it "determines the Astronomical Julian day" do + Date.civil(2007, 1, 17).ajd.should == 4908235.to_r / 2 + end +end + +describe "Date#amjd" do + it "determines the Astronomical Modified Julian day" do + Date.civil(2007, 1, 17).amjd.should == 54117 + end +end + +describe "Date#day_fraction" do + it "determines the day fraction" do + Date.civil(2007, 1, 17).day_fraction.should == 0 + end +end + +describe "Date#mjd" do + it "determines the Modified Julian day" do + Date.civil(2007, 1, 17).mjd.should == 54117 + end +end + +describe "Date#ld" do + it "determines the Modified Julian day" do + Date.civil(2007, 1, 17).ld.should == 154958 + end +end + +describe "Date#year" do + it "determines the year" do + Date.civil(2007, 1, 17).year.should == 2007 + end +end + +describe "Date#yday" do + it "determines the day of the year" do + Date.civil(2007, 1, 17).yday.should == 17 + Date.civil(2008, 10, 28).yday.should == 302 + end +end + +describe "Date#mon" do + it "determines the month" do + Date.civil(2007, 1, 17).mon.should == 1 + Date.civil(2008, 10, 28).mon.should == 10 + end +end + +describe "Date#mday" do + it "determines the day of the month" do + Date.civil(2007, 1, 17).mday.should == 17 + Date.civil(2008, 10, 28).mday.should == 28 + end +end + +describe "Date#wday" do + it "determines the week day" do + Date.civil(2007, 1, 17).wday.should == 3 + Date.civil(2008, 10, 26).wday.should == 0 + end +end + +describe "Date#cwyear" do + it "determines the commercial year" do + Date.civil(2007, 1, 17).cwyear.should == 2007 + Date.civil(2008, 10, 28).cwyear.should == 2008 + Date.civil(2007, 12, 31).cwyear.should == 2008 + Date.civil(2010, 1, 1).cwyear.should == 2009 + end +end + +describe "Date#cweek" do + it "determines the commercial week" do + Date.civil(2007, 1, 17).cweek.should == 3 + Date.civil(2008, 10, 28).cweek.should == 44 + Date.civil(2007, 12, 31).cweek.should == 1 + Date.civil(2010, 1, 1).cweek.should == 53 + end +end + +describe "Date#cwday" do + it "determines the commercial week day" do + Date.civil(2007, 1, 17).cwday.should == 3 + Date.civil(2008, 10, 26).cwday.should == 7 + end +end diff --git a/spec/ruby/library/date/add_month_spec.rb b/spec/ruby/library/date/add_month_spec.rb new file mode 100644 index 0000000000..40833f6487 --- /dev/null +++ b/spec/ruby/library/date/add_month_spec.rb @@ -0,0 +1,38 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#>>" do + it "adds the number of months to a Date" do + d = Date.civil(2007,2,27) >> 10 + d.should == Date.civil(2007, 12, 27) + end + + it "sets the day to the last day of a month if the day doesn't exist" do + d = Date.civil(2008,3,31) >> 1 + d.should == Date.civil(2008, 4, 30) + end + + it "returns the day of the reform if date falls within calendar reform" do + calendar_reform_italy = Date.new(1582, 10, 4) + d1 = Date.new(1582, 9, 9) >> 1 + d2 = Date.new(1582, 9, 10) >> 1 + d1.should == calendar_reform_italy + d2.should == calendar_reform_italy + end + + it "raise a TypeError when passed a Symbol" do + -> { Date.civil(2007,2,27) >> :hello }.should raise_error(TypeError) + end + + it "raise a TypeError when passed a String" do + -> { Date.civil(2007,2,27) >> "hello" }.should raise_error(TypeError) + end + + it "raise a TypeError when passed a Date" do + -> { Date.civil(2007,2,27) >> Date.new }.should raise_error(TypeError) + end + + it "raise a TypeError when passed an Object" do + -> { Date.civil(2007,2,27) >> Object.new }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/library/date/add_spec.rb b/spec/ruby/library/date/add_spec.rb new file mode 100644 index 0000000000..2b9cc62023 --- /dev/null +++ b/spec/ruby/library/date/add_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#+" do + it "adds the number of days to a Date" do + d = Date.civil(2007,2,27) + 10 + d.should == Date.civil(2007, 3, 9) + end + + it "adds a negative number of days to a Date" do + d = Date.civil(2007,2,27).+(-10) + d.should == Date.civil(2007, 2, 17) + end + + it "raises a TypeError when passed a Symbol" do + -> { Date.civil(2007,2,27) + :hello }.should raise_error(TypeError) + end + + it "raises a TypeError when passed a String" do + -> { Date.civil(2007,2,27) + "hello" }.should raise_error(TypeError) + end + + it "raises a TypeError when passed a Date" do + -> { Date.civil(2007,2,27) + Date.new }.should raise_error(TypeError) + end + + it "raises a TypeError when passed an Object" do + -> { Date.civil(2007,2,27) + Object.new }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/library/date/ajd_spec.rb b/spec/ruby/library/date/ajd_spec.rb new file mode 100644 index 0000000000..10f1302354 --- /dev/null +++ b/spec/ruby/library/date/ajd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#ajd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/ajd_to_amjd_spec.rb b/spec/ruby/library/date/ajd_to_amjd_spec.rb new file mode 100644 index 0000000000..948f4c2236 --- /dev/null +++ b/spec/ruby/library/date/ajd_to_amjd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.ajd_to_amjd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/ajd_to_jd_spec.rb b/spec/ruby/library/date/ajd_to_jd_spec.rb new file mode 100644 index 0000000000..e55ce9f4f2 --- /dev/null +++ b/spec/ruby/library/date/ajd_to_jd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.ajd_to_jd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/amjd_spec.rb b/spec/ruby/library/date/amjd_spec.rb new file mode 100644 index 0000000000..ad7bc14965 --- /dev/null +++ b/spec/ruby/library/date/amjd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#amjd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/amjd_to_ajd_spec.rb b/spec/ruby/library/date/amjd_to_ajd_spec.rb new file mode 100644 index 0000000000..66c26a16a8 --- /dev/null +++ b/spec/ruby/library/date/amjd_to_ajd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.amjd_to_ajd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/append_spec.rb b/spec/ruby/library/date/append_spec.rb new file mode 100644 index 0000000000..4305a00321 --- /dev/null +++ b/spec/ruby/library/date/append_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#<<" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/asctime_spec.rb b/spec/ruby/library/date/asctime_spec.rb new file mode 100644 index 0000000000..67d158cc01 --- /dev/null +++ b/spec/ruby/library/date/asctime_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#asctime" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/boat_spec.rb b/spec/ruby/library/date/boat_spec.rb new file mode 100644 index 0000000000..e4f1b797fc --- /dev/null +++ b/spec/ruby/library/date/boat_spec.rb @@ -0,0 +1,24 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#<=>" do + it "returns 0 when two dates are equal" do + (Date.civil(2000, 04, 06) <=> Date.civil(2000, 04, 06)).should == 0 + end + + it "returns -1 when self is less than another date" do + (Date.civil(2000, 04, 05) <=> Date.civil(2000, 04, 06)).should == -1 + end + + it "returns -1 when self is less than a Numeric" do + (Date.civil(2000, 04, 05) <=> Date.civil(2000, 04, 06).jd).should == -1 + end + + it "returns 1 when self is greater than another date" do + (Date.civil(2001, 04, 05) <=> Date.civil(2000, 04, 06)).should == 1 + end + + it "returns 1 when self is greater than a Numeric" do + (Date.civil(2001, 04, 05) <=> Date.civil(2000, 04, 06).jd).should == 1 + end +end diff --git a/spec/ruby/library/date/case_compare_spec.rb b/spec/ruby/library/date/case_compare_spec.rb new file mode 100644 index 0000000000..87d522ee6a --- /dev/null +++ b/spec/ruby/library/date/case_compare_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#===" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/civil_spec.rb b/spec/ruby/library/date/civil_spec.rb new file mode 100644 index 0000000000..1c780fce56 --- /dev/null +++ b/spec/ruby/library/date/civil_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/civil' +require 'date' + +describe "Date.civil" do + it_behaves_like :date_civil, :civil +end diff --git a/spec/ruby/library/date/commercial_spec.rb b/spec/ruby/library/date/commercial_spec.rb new file mode 100644 index 0000000000..d7fc34d74a --- /dev/null +++ b/spec/ruby/library/date/commercial_spec.rb @@ -0,0 +1,17 @@ +require 'date' +require_relative '../../spec_helper' +require_relative 'shared/commercial' + +describe "Date#commercial" do + + it_behaves_like :date_commercial, :commercial + +end + +# reference: +# October 1582 (the Gregorian calendar, Civil Date) +# S M Tu W Th F S +# 1 2 3 4 15 16 +# 17 18 19 20 21 22 23 +# 24 25 26 27 28 29 30 +# 31 diff --git a/spec/ruby/library/date/commercial_to_jd_spec.rb b/spec/ruby/library/date/commercial_to_jd_spec.rb new file mode 100644 index 0000000000..9b77f1229f --- /dev/null +++ b/spec/ruby/library/date/commercial_to_jd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.commercial_to_jd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/comparison_spec.rb b/spec/ruby/library/date/comparison_spec.rb new file mode 100644 index 0000000000..1a94b9dcd2 --- /dev/null +++ b/spec/ruby/library/date/comparison_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#<=>" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/constants_spec.rb b/spec/ruby/library/date/constants_spec.rb new file mode 100644 index 0000000000..1d18dd1b0c --- /dev/null +++ b/spec/ruby/library/date/constants_spec.rb @@ -0,0 +1,48 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date constants" do + + it "defines JULIAN" do + (Date::JULIAN <=> Date::Infinity.new).should == 0 + end + + it "defines GREGORIAN" do + (Date::GREGORIAN <=> -Date::Infinity.new).should == 0 + end + + it "defines ITALY" do + Date::ITALY.should == 2299161 # 1582-10-15 + end + + it "defines ENGLAND" do + Date::ENGLAND.should == 2361222 # 1752-09-14 + end + + it "defines MONTHNAMES" do + Date::MONTHNAMES.should == [nil] + %w(January February March April May June July + August September October November December) + end + + it "defines DAYNAMES" do + Date::DAYNAMES.should == %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday) + end + + it "defines ABBR_MONTHNAMES" do + Date::ABBR_DAYNAMES.should == %w(Sun Mon Tue Wed Thu Fri Sat) + end + + it "freezes MONTHNAMES, DAYNAMES, ABBR_MONTHNAMES, ABBR_DAYSNAMES" do + [Date::MONTHNAMES, Date::DAYNAMES, Date::ABBR_MONTHNAMES, Date::ABBR_DAYNAMES].each do |ary| + -> { + ary << "Unknown" + }.should raise_error(FrozenError, /frozen/) + ary.compact.each do |name| + -> { + name << "modified" + }.should raise_error(FrozenError, /frozen/) + end + end + end + +end diff --git a/spec/ruby/library/date/conversions_spec.rb b/spec/ruby/library/date/conversions_spec.rb new file mode 100644 index 0000000000..a9a320b0fc --- /dev/null +++ b/spec/ruby/library/date/conversions_spec.rb @@ -0,0 +1,43 @@ +require 'date' +require_relative '../../spec_helper' + + +describe "Date#new_start" do + it "converts a date object into another with a new calendar reform" do + Date.civil(1582, 10, 14, Date::ENGLAND).new_start.should == Date.civil(1582, 10, 24) + Date.civil(1582, 10, 4, Date::ENGLAND).new_start.should == Date.civil(1582, 10, 4) + Date.civil(1582, 10, 15).new_start(Date::ENGLAND).should == Date.civil(1582, 10, 5, Date::ENGLAND) + Date.civil(1752, 9, 14).new_start(Date::ENGLAND).should == Date.civil(1752, 9, 14, Date::ENGLAND) + Date.civil(1752, 9, 13).new_start(Date::ENGLAND).should == Date.civil(1752, 9, 2, Date::ENGLAND) + end +end + +describe "Date#italy" do + it "converts a date object into another with the Italian calendar reform" do + Date.civil(1582, 10, 14, Date::ENGLAND).italy.should == Date.civil(1582, 10, 24) + Date.civil(1582, 10, 4, Date::ENGLAND).italy.should == Date.civil(1582, 10, 4) + end +end + +describe "Date#england" do + it "converts a date object into another with the English calendar reform" do + Date.civil(1582, 10, 15).england.should == Date.civil(1582, 10, 5, Date::ENGLAND) + Date.civil(1752, 9, 14).england.should == Date.civil(1752, 9, 14, Date::ENGLAND) + Date.civil(1752, 9, 13).england.should == Date.civil(1752, 9, 2, Date::ENGLAND) + end +end + +describe "Date#julian" do + it "converts a date object into another with the Julian calendar" do + Date.civil(1582, 10, 15).julian.should == Date.civil(1582, 10, 5, Date::JULIAN) + Date.civil(1752, 9, 14).julian.should == Date.civil(1752, 9, 3, Date::JULIAN) + Date.civil(1752, 9, 13).julian.should == Date.civil(1752, 9, 2, Date::JULIAN) + end +end + +describe "Date#gregorian" do + it "converts a date object into another with the Gregorian calendar" do + Date.civil(1582, 10, 4).gregorian.should == Date.civil(1582, 10, 14, Date::GREGORIAN) + Date.civil(1752, 9, 14).gregorian.should == Date.civil(1752, 9, 14, Date::GREGORIAN) + end +end diff --git a/spec/ruby/library/date/ctime_spec.rb b/spec/ruby/library/date/ctime_spec.rb new file mode 100644 index 0000000000..3faa7c6380 --- /dev/null +++ b/spec/ruby/library/date/ctime_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#ctime" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/cwday_spec.rb b/spec/ruby/library/date/cwday_spec.rb new file mode 100644 index 0000000000..c5a39f277f --- /dev/null +++ b/spec/ruby/library/date/cwday_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#cwday" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/cweek_spec.rb b/spec/ruby/library/date/cweek_spec.rb new file mode 100644 index 0000000000..6f7aab3922 --- /dev/null +++ b/spec/ruby/library/date/cweek_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#cweek" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/cwyear_spec.rb b/spec/ruby/library/date/cwyear_spec.rb new file mode 100644 index 0000000000..a85ee29920 --- /dev/null +++ b/spec/ruby/library/date/cwyear_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#cwyear" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/day_fraction_spec.rb b/spec/ruby/library/date/day_fraction_spec.rb new file mode 100644 index 0000000000..12b873773f --- /dev/null +++ b/spec/ruby/library/date/day_fraction_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#day_fraction" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/day_fraction_to_time_spec.rb b/spec/ruby/library/date/day_fraction_to_time_spec.rb new file mode 100644 index 0000000000..d4741d65ec --- /dev/null +++ b/spec/ruby/library/date/day_fraction_to_time_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.day_fraction_to_time" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/day_spec.rb b/spec/ruby/library/date/day_spec.rb new file mode 100644 index 0000000000..bc727c4717 --- /dev/null +++ b/spec/ruby/library/date/day_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#day" do + it "returns the day" do + d = Date.new(2000, 7, 1).day + d.should == 1 + end +end diff --git a/spec/ruby/library/date/deconstruct_keys_spec.rb b/spec/ruby/library/date/deconstruct_keys_spec.rb new file mode 100644 index 0000000000..b9dd6b8816 --- /dev/null +++ b/spec/ruby/library/date/deconstruct_keys_spec.rb @@ -0,0 +1,42 @@ +require_relative '../../spec_helper' +require 'date' +date_version = defined?(Date::VERSION) ? Date::VERSION : '3.1.0' + +describe "Date#deconstruct_keys" do + it "returns whole hash for nil as an argument" do + d = Date.new(2022, 10, 5) + d.deconstruct_keys(nil).should == { year: 2022, month: 10, day: 5, yday: 278, wday: 3 } + end + + it "returns only specified keys" do + d = Date.new(2022, 10, 5) + d.deconstruct_keys([:year, :month]).should == { year: 2022, month: 10 } + end + + it "requires one argument" do + -> { + Date.new(2022, 10, 5).deconstruct_keys + }.should raise_error(ArgumentError) + end + + it "it raises error when argument is neither nil nor array" do + d = Date.new(2022, 10, 5) + + -> { d.deconstruct_keys(1) }.should raise_error(TypeError, "wrong argument type Integer (expected Array or nil)") + -> { d.deconstruct_keys("asd") }.should raise_error(TypeError, "wrong argument type String (expected Array or nil)") + -> { d.deconstruct_keys(:x) }.should raise_error(TypeError, "wrong argument type Symbol (expected Array or nil)") + -> { d.deconstruct_keys({}) }.should raise_error(TypeError, "wrong argument type Hash (expected Array or nil)") + end + + it "returns {} when passed []" do + Date.new(2022, 10, 5).deconstruct_keys([]).should == {} + end + + it "ignores non-Symbol keys" do + Date.new(2022, 10, 5).deconstruct_keys(['year', []]).should == {} + end + + it "ignores not existing Symbol keys" do + Date.new(2022, 10, 5).deconstruct_keys([:year, :a]).should == { year: 2022 } + end +end diff --git a/spec/ruby/library/date/downto_spec.rb b/spec/ruby/library/date/downto_spec.rb new file mode 100644 index 0000000000..84c641ee14 --- /dev/null +++ b/spec/ruby/library/date/downto_spec.rb @@ -0,0 +1,18 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#downto" do + + it "creates earlier dates when passed a negative step" do + ds = Date.civil(2000, 4, 14) + de = Date.civil(2000, 3, 29) + count = 0 + ds.step(de, -1) do |d| + d.should <= ds + d.should >= de + count += 1 + end + count.should == 17 + end + +end diff --git a/spec/ruby/library/date/england_spec.rb b/spec/ruby/library/date/england_spec.rb new file mode 100644 index 0000000000..2e30530c5a --- /dev/null +++ b/spec/ruby/library/date/england_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#england" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/eql_spec.rb b/spec/ruby/library/date/eql_spec.rb new file mode 100644 index 0000000000..a1819cae3a --- /dev/null +++ b/spec/ruby/library/date/eql_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#eql?" do + it "returns true if self is equal to another date" do + Date.civil(2007, 10, 11).eql?(Date.civil(2007, 10, 11)).should be_true + end + + it "returns false if self is not equal to another date" do + Date.civil(2007, 10, 11).eql?(Date.civil(2007, 10, 12)).should be_false + end +end diff --git a/spec/ruby/library/date/format/bag/method_missing_spec.rb b/spec/ruby/library/date/format/bag/method_missing_spec.rb new file mode 100644 index 0000000000..03e4fbcd30 --- /dev/null +++ b/spec/ruby/library/date/format/bag/method_missing_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../../spec_helper' +require 'date' + +describe "Date::Format::Bag#method_missing" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/format/bag/to_hash_spec.rb b/spec/ruby/library/date/format/bag/to_hash_spec.rb new file mode 100644 index 0000000000..76734624b9 --- /dev/null +++ b/spec/ruby/library/date/format/bag/to_hash_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../../spec_helper' +require 'date' + +describe "Date::Format::Bag#to_hash" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/friday_spec.rb b/spec/ruby/library/date/friday_spec.rb new file mode 100644 index 0000000000..3dc040fabe --- /dev/null +++ b/spec/ruby/library/date/friday_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#friday?" do + it "should be friday" do + Date.new(2000, 1, 7).friday?.should be_true + end + + it "should not be friday" do + Date.new(2000, 1, 8).friday?.should be_false + end +end diff --git a/spec/ruby/library/date/gregorian_leap_spec.rb b/spec/ruby/library/date/gregorian_leap_spec.rb new file mode 100644 index 0000000000..c3d25cf90f --- /dev/null +++ b/spec/ruby/library/date/gregorian_leap_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#gregorian_leap?" do + it "returns true if a year is a leap year in the Gregorian calendar" do + Date.gregorian_leap?(2000).should be_true + Date.gregorian_leap?(2004).should be_true + end + + it "returns false if a year is not a leap year in the Gregorian calendar" do + Date.gregorian_leap?(1900).should be_false + Date.gregorian_leap?(1999).should be_false + Date.gregorian_leap?(2002).should be_false + end +end diff --git a/spec/ruby/library/date/gregorian_spec.rb b/spec/ruby/library/date/gregorian_spec.rb new file mode 100644 index 0000000000..ea7ece2ade --- /dev/null +++ b/spec/ruby/library/date/gregorian_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#gregorian?" do + + it "marks a day before the calendar reform as Julian" do + Date.civil(1007, 2, 27).gregorian?.should be_false + Date.civil(1907, 2, 27, Date.civil(1930, 1, 1).jd).gregorian?.should be_false + end + + it "marks a day after the calendar reform as Julian" do + Date.civil(2007, 2, 27).should.gregorian? + Date.civil(1607, 2, 27, Date.civil(1582, 1, 1).jd).gregorian?.should be_true + end + +end diff --git a/spec/ruby/library/date/hash_spec.rb b/spec/ruby/library/date/hash_spec.rb new file mode 100644 index 0000000000..4fb452d486 --- /dev/null +++ b/spec/ruby/library/date/hash_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#hash" do + it "returns the same value for equal dates" do + Date.civil(2004, 7, 12).hash.should == Date.civil(2004, 7, 12).hash + end +end diff --git a/spec/ruby/library/date/infinity/abs_spec.rb b/spec/ruby/library/date/infinity/abs_spec.rb new file mode 100644 index 0000000000..c08189155d --- /dev/null +++ b/spec/ruby/library/date/infinity/abs_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#abs" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/coerce_spec.rb b/spec/ruby/library/date/infinity/coerce_spec.rb new file mode 100644 index 0000000000..75e5ebeab7 --- /dev/null +++ b/spec/ruby/library/date/infinity/coerce_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#coerce" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/comparison_spec.rb b/spec/ruby/library/date/infinity/comparison_spec.rb new file mode 100644 index 0000000000..a9b9d124fd --- /dev/null +++ b/spec/ruby/library/date/infinity/comparison_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#<=>" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/d_spec.rb b/spec/ruby/library/date/infinity/d_spec.rb new file mode 100644 index 0000000000..a5bd2427c7 --- /dev/null +++ b/spec/ruby/library/date/infinity/d_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#d" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/finite_spec.rb b/spec/ruby/library/date/infinity/finite_spec.rb new file mode 100644 index 0000000000..8971c2213e --- /dev/null +++ b/spec/ruby/library/date/infinity/finite_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#finite?" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/infinite_spec.rb b/spec/ruby/library/date/infinity/infinite_spec.rb new file mode 100644 index 0000000000..848f538672 --- /dev/null +++ b/spec/ruby/library/date/infinity/infinite_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#infinite?" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/nan_spec.rb b/spec/ruby/library/date/infinity/nan_spec.rb new file mode 100644 index 0000000000..b0f5d8ac7a --- /dev/null +++ b/spec/ruby/library/date/infinity/nan_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#nan?" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/uminus_spec.rb b/spec/ruby/library/date/infinity/uminus_spec.rb new file mode 100644 index 0000000000..1b1f568103 --- /dev/null +++ b/spec/ruby/library/date/infinity/uminus_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#-@" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/uplus_spec.rb b/spec/ruby/library/date/infinity/uplus_spec.rb new file mode 100644 index 0000000000..6a3b2d8442 --- /dev/null +++ b/spec/ruby/library/date/infinity/uplus_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#+@" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity/zero_spec.rb b/spec/ruby/library/date/infinity/zero_spec.rb new file mode 100644 index 0000000000..7df5518785 --- /dev/null +++ b/spec/ruby/library/date/infinity/zero_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../../spec_helper' +require 'date' + +describe "Date::Infinity#zero?" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/infinity_spec.rb b/spec/ruby/library/date/infinity_spec.rb new file mode 100644 index 0000000000..721fd76066 --- /dev/null +++ b/spec/ruby/library/date/infinity_spec.rb @@ -0,0 +1,67 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date::Infinity" do + + it "should be able to check whether Infinity is zero" do + i = Date::Infinity.new + i.should_not.zero? + end + + it "should be able to check whether Infinity is finite" do + i1 = Date::Infinity.new + i1.should_not.finite? + i2 = Date::Infinity.new(-1) + i2.should_not.finite? + i3 = Date::Infinity.new(0) + i3.should_not.finite? + end + + it "should be able to check whether Infinity is infinite" do + i1 = Date::Infinity.new + i1.infinite?.should == 1 + i2 = Date::Infinity.new(-1) + i2.infinite?.should == -1 + i3 = Date::Infinity.new(0) + i3.infinite?.should == nil + end + + it "should be able to check whether Infinity is not a number" do + i1 = Date::Infinity.new + i1.should_not.nan? + i2 = Date::Infinity.new(-1) + i2.should_not.nan? + i3 = Date::Infinity.new(0) + i3.should.nan? + end + + it "should be able to compare Infinity objects" do + i1 = Date::Infinity.new + i2 = Date::Infinity.new(-1) + i3 = Date::Infinity.new(0) + i4 = Date::Infinity.new + (i4 <=> i1).should == 0 + (i3 <=> i1).should == -1 + (i2 <=> i1).should == -1 + (i3 <=> i2).should == 1 + end + + it "should be able to return plus Infinity for abs" do + i1 = Date::Infinity.new + i2 = Date::Infinity.new(-1) + i3 = Date::Infinity.new(0) + (i2.abs <=> i1).should == 0 + (i3.abs <=> i1).should == 0 + end + + it "should be able to use -@ and +@ for Date::Infinity" do + (Date::Infinity.new <=> +Date::Infinity.new).should == 0 + (Date::Infinity.new(-1) <=> -Date::Infinity.new).should == 0 + end + + it "should be able to coerce a Date::Infinity object" do + Date::Infinity.new.coerce(1).should == [-1, 1] + Date::Infinity.new(0).coerce(2).should == [0, 0] + Date::Infinity.new(-1).coerce(1.5).should == [1, -1] + end +end diff --git a/spec/ruby/library/date/inspect_spec.rb b/spec/ruby/library/date/inspect_spec.rb new file mode 100644 index 0000000000..81c2cc8003 --- /dev/null +++ b/spec/ruby/library/date/inspect_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#inspect" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/iso8601_spec.rb b/spec/ruby/library/date/iso8601_spec.rb new file mode 100644 index 0000000000..af66845a6b --- /dev/null +++ b/spec/ruby/library/date/iso8601_spec.rb @@ -0,0 +1,56 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.iso8601" do + it "parses YYYY-MM-DD into a Date object" do + d = Date.iso8601("2018-01-01") + d.should == Date.civil(2018, 1, 1) + end + + it "parses YYYYMMDD into a Date object" do + d = Date.iso8601("20180715") + d.should == Date.civil(2018, 7, 15) + end + + it "parses a negative Date" do + d = Date.iso8601("-4712-01-01") + d.should == Date.civil(-4712, 1, 1) + end + + it "parses a StringSubclass into a Date object" do + d = Date.iso8601(Class.new(String).new("-4712-01-01")) + d.should == Date.civil(-4712, 1, 1) + end + + it "raises a Date::Error if the argument is a invalid Date" do + -> { + Date.iso8601('invalid') + }.should raise_error(Date::Error, "invalid date") + end + + it "raises a Date::Error when passed a nil" do + -> { + Date.iso8601(nil) + }.should raise_error(Date::Error, "invalid date") + end + + it "raises a TypeError when passed an Object" do + -> { Date.iso8601(Object.new) }.should raise_error(TypeError) + end +end + +describe "Date._iso8601" do + it "returns an empty hash if the argument is a invalid Date" do + h = Date._iso8601('invalid') + h.should == {} + end + + it "returns an empty hash if the argument is nil" do + h = Date._iso8601(nil) + h.should == {} + end + + it "raises a TypeError when passed an Object" do + -> { Date._iso8601(Object.new) }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/library/date/italy_spec.rb b/spec/ruby/library/date/italy_spec.rb new file mode 100644 index 0000000000..9369b05180 --- /dev/null +++ b/spec/ruby/library/date/italy_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#italy" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/jd_spec.rb b/spec/ruby/library/date/jd_spec.rb new file mode 100644 index 0000000000..336b783e8d --- /dev/null +++ b/spec/ruby/library/date/jd_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require_relative 'shared/jd' +require 'date' + +describe "Date#jd" do + + it "determines the Julian day for a Date object" do + Date.civil(2008, 1, 16).jd.should == 2454482 + end + +end + +describe "Date.jd" do + it_behaves_like :date_jd, :jd +end diff --git a/spec/ruby/library/date/jd_to_ajd_spec.rb b/spec/ruby/library/date/jd_to_ajd_spec.rb new file mode 100644 index 0000000000..f946c46b8a --- /dev/null +++ b/spec/ruby/library/date/jd_to_ajd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.jd_to_ajd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/jd_to_civil_spec.rb b/spec/ruby/library/date/jd_to_civil_spec.rb new file mode 100644 index 0000000000..13b6e47ee2 --- /dev/null +++ b/spec/ruby/library/date/jd_to_civil_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.jd_to_civil" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/jd_to_commercial_spec.rb b/spec/ruby/library/date/jd_to_commercial_spec.rb new file mode 100644 index 0000000000..2256b74f2a --- /dev/null +++ b/spec/ruby/library/date/jd_to_commercial_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.jd_to_commercial" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/jd_to_ld_spec.rb b/spec/ruby/library/date/jd_to_ld_spec.rb new file mode 100644 index 0000000000..5954014f85 --- /dev/null +++ b/spec/ruby/library/date/jd_to_ld_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.jd_to_ld" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/jd_to_mjd_spec.rb b/spec/ruby/library/date/jd_to_mjd_spec.rb new file mode 100644 index 0000000000..24eb84e171 --- /dev/null +++ b/spec/ruby/library/date/jd_to_mjd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.jd_to_mjd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/jd_to_ordinal_spec.rb b/spec/ruby/library/date/jd_to_ordinal_spec.rb new file mode 100644 index 0000000000..c7c1704948 --- /dev/null +++ b/spec/ruby/library/date/jd_to_ordinal_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.jd_to_ordinal" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/jd_to_wday_spec.rb b/spec/ruby/library/date/jd_to_wday_spec.rb new file mode 100644 index 0000000000..27e00b2044 --- /dev/null +++ b/spec/ruby/library/date/jd_to_wday_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.jd_to_wday" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/julian_leap_spec.rb b/spec/ruby/library/date/julian_leap_spec.rb new file mode 100644 index 0000000000..2ef2d65d81 --- /dev/null +++ b/spec/ruby/library/date/julian_leap_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.julian_leap?" do + it "determines whether a year is a leap year in the Julian calendar" do + Date.julian_leap?(1900).should be_true + Date.julian_leap?(2000).should be_true + Date.julian_leap?(2004).should be_true + end + + it "determines whether a year is not a leap year in the Julian calendar" do + Date.julian_leap?(1999).should be_false + Date.julian_leap?(2002).should be_false + end +end diff --git a/spec/ruby/library/date/julian_spec.rb b/spec/ruby/library/date/julian_spec.rb new file mode 100644 index 0000000000..db2629d1e7 --- /dev/null +++ b/spec/ruby/library/date/julian_spec.rb @@ -0,0 +1,16 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#julian?" do + + it "marks a day before the calendar reform as Julian" do + Date.civil(1007, 2, 27).should.julian? + Date.civil(1907, 2, 27, Date.civil(1930, 1, 1).jd).julian?.should be_true + end + + it "marks a day after the calendar reform as Julian" do + Date.civil(2007, 2, 27).should_not.julian? + Date.civil(1607, 2, 27, Date.civil(1582, 1, 1).jd).julian?.should be_false + end + +end diff --git a/spec/ruby/library/date/ld_spec.rb b/spec/ruby/library/date/ld_spec.rb new file mode 100644 index 0000000000..73a47d2382 --- /dev/null +++ b/spec/ruby/library/date/ld_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#ld" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/ld_to_jd_spec.rb b/spec/ruby/library/date/ld_to_jd_spec.rb new file mode 100644 index 0000000000..37abe01449 --- /dev/null +++ b/spec/ruby/library/date/ld_to_jd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.ld_to_jd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/leap_spec.rb b/spec/ruby/library/date/leap_spec.rb new file mode 100644 index 0000000000..674b191c9f --- /dev/null +++ b/spec/ruby/library/date/leap_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#leap?" do + it "needs to be reviewed for spec completeness" +end + +describe "Date.leap?" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/mday_spec.rb b/spec/ruby/library/date/mday_spec.rb new file mode 100644 index 0000000000..53f6f98169 --- /dev/null +++ b/spec/ruby/library/date/mday_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#mday" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/minus_month_spec.rb b/spec/ruby/library/date/minus_month_spec.rb new file mode 100644 index 0000000000..470c4d8a76 --- /dev/null +++ b/spec/ruby/library/date/minus_month_spec.rb @@ -0,0 +1,23 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#<<" do + + it "subtracts a number of months from a date" do + d = Date.civil(2007,2,27) << 10 + d.should == Date.civil(2006, 4, 27) + end + + it "returns the last day of a month if the day doesn't exist" do + d = Date.civil(2008,3,31) << 1 + d.should == Date.civil(2008, 2, 29) + end + + it "raises an error on non numeric parameters" do + -> { Date.civil(2007,2,27) << :hello }.should raise_error(TypeError) + -> { Date.civil(2007,2,27) << "hello" }.should raise_error(TypeError) + -> { Date.civil(2007,2,27) << Date.new }.should raise_error(TypeError) + -> { Date.civil(2007,2,27) << Object.new }.should raise_error(TypeError) + end + +end diff --git a/spec/ruby/library/date/minus_spec.rb b/spec/ruby/library/date/minus_spec.rb new file mode 100644 index 0000000000..5a2a29e04a --- /dev/null +++ b/spec/ruby/library/date/minus_spec.rb @@ -0,0 +1,30 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#-" do + + it "subtracts a number of days from a Date" do + d = Date.civil(2007, 5 ,2) - 13 + d.should == Date.civil(2007, 4, 19) + end + + it "subtracts a negative number of days from a Date" do + d = Date.civil(2007, 4, 19).-(-13) + d.should == Date.civil(2007, 5 ,2) + end + + it "computes the difference between two dates" do + (Date.civil(2007,2,27) - Date.civil(2007,2,27)).should == 0 + (Date.civil(2007,2,27) - Date.civil(2007,2,26)).should == 1 + (Date.civil(2006,2,27) - Date.civil(2007,2,27)).should == -365 + (Date.civil(2008,2,27) - Date.civil(2007,2,27)).should == 365 + + end + + it "raises an error for non Numeric arguments" do + -> { Date.civil(2007,2,27) - :hello }.should raise_error(TypeError) + -> { Date.civil(2007,2,27) - "hello" }.should raise_error(TypeError) + -> { Date.civil(2007,2,27) - Object.new }.should raise_error(TypeError) + end + +end diff --git a/spec/ruby/library/date/mjd_spec.rb b/spec/ruby/library/date/mjd_spec.rb new file mode 100644 index 0000000000..6f03af346b --- /dev/null +++ b/spec/ruby/library/date/mjd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#mjd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/mjd_to_jd_spec.rb b/spec/ruby/library/date/mjd_to_jd_spec.rb new file mode 100644 index 0000000000..2009261103 --- /dev/null +++ b/spec/ruby/library/date/mjd_to_jd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.mjd_to_jd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/mon_spec.rb b/spec/ruby/library/date/mon_spec.rb new file mode 100644 index 0000000000..616d72cf88 --- /dev/null +++ b/spec/ruby/library/date/mon_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/month' +require 'date' + +describe "Date#mon" do + it_behaves_like :date_month, :mon +end diff --git a/spec/ruby/library/date/monday_spec.rb b/spec/ruby/library/date/monday_spec.rb new file mode 100644 index 0000000000..14a117b73c --- /dev/null +++ b/spec/ruby/library/date/monday_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#monday?" do + it "should be monday" do + Date.new(2000, 1, 3).monday?.should be_true + end +end diff --git a/spec/ruby/library/date/month_spec.rb b/spec/ruby/library/date/month_spec.rb new file mode 100644 index 0000000000..f493ec8119 --- /dev/null +++ b/spec/ruby/library/date/month_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/month' +require 'date' + +describe "Date#month" do + it_behaves_like :date_month, :month +end diff --git a/spec/ruby/library/date/new_spec.rb b/spec/ruby/library/date/new_spec.rb new file mode 100644 index 0000000000..cb64cabce6 --- /dev/null +++ b/spec/ruby/library/date/new_spec.rb @@ -0,0 +1,7 @@ +require 'date' +require_relative '../../spec_helper' +require_relative 'shared/civil' + +describe "Date.new" do + it_behaves_like :date_civil, :new +end diff --git a/spec/ruby/library/date/new_start_spec.rb b/spec/ruby/library/date/new_start_spec.rb new file mode 100644 index 0000000000..aef78f2320 --- /dev/null +++ b/spec/ruby/library/date/new_start_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#new_start" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/next_day_spec.rb b/spec/ruby/library/date/next_day_spec.rb new file mode 100644 index 0000000000..3b066630e7 --- /dev/null +++ b/spec/ruby/library/date/next_day_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#next_day" do + it "returns the next day" do + d = Date.new(2000, 1, 4).next_day + d.should == Date.new(2000, 1, 5) + end + + it "returns three days later across months" do + d = Date.new(2000, 1, 30).next_day(3) + d.should == Date.new(2000, 2, 2) + end +end diff --git a/spec/ruby/library/date/next_month_spec.rb b/spec/ruby/library/date/next_month_spec.rb new file mode 100644 index 0000000000..6ee664433f --- /dev/null +++ b/spec/ruby/library/date/next_month_spec.rb @@ -0,0 +1,29 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#next_month" do + it "returns the next month" do + d = Date.new(2000, 7, 1).next_month + d.should == Date.new(2000, 8, 1) + end + + it "returns three months later" do + d = Date.new(2000, 7, 1).next_month(3) + d.should == Date.new(2000, 10, 1) + end + + it "returns three months later across years" do + d = Date.new(2000, 12, 1).next_month(3) + d.should == Date.new(2001, 3, 1) + end + + it "returns last day of month two months later" do + d = Date.new(2000, 1, 31).next_month(2) + d.should == Date.new(2000, 3, 31) + end + + it "returns last day of next month when same day does not exist" do + d = Date.new(2001, 1, 30).next_month + d.should == Date.new(2001, 2, 28) + end +end diff --git a/spec/ruby/library/date/next_spec.rb b/spec/ruby/library/date/next_spec.rb new file mode 100644 index 0000000000..8063d6a2e4 --- /dev/null +++ b/spec/ruby/library/date/next_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#next" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/next_year_spec.rb b/spec/ruby/library/date/next_year_spec.rb new file mode 100644 index 0000000000..dda9a44008 --- /dev/null +++ b/spec/ruby/library/date/next_year_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#next_year" do + it "returns the day of the reform if date falls within calendar reform" do + calendar_reform_italy = Date.new(1582, 10, 4) + d1 = Date.new(1581, 10, 9).next_year + d2 = Date.new(1581, 10, 10).next_year + d1.should == calendar_reform_italy + d2.should == calendar_reform_italy + end +end diff --git a/spec/ruby/library/date/ordinal_spec.rb b/spec/ruby/library/date/ordinal_spec.rb new file mode 100644 index 0000000000..ec490fd49c --- /dev/null +++ b/spec/ruby/library/date/ordinal_spec.rb @@ -0,0 +1,7 @@ +require 'date' +require_relative '../../spec_helper' +require_relative 'shared/ordinal' + +describe "Date.ordinal" do + it_behaves_like :date_ordinal, :ordinal +end diff --git a/spec/ruby/library/date/ordinal_to_jd_spec.rb b/spec/ruby/library/date/ordinal_to_jd_spec.rb new file mode 100644 index 0000000000..44f4b3321e --- /dev/null +++ b/spec/ruby/library/date/ordinal_to_jd_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.ordinal_to_jd" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/parse_spec.rb b/spec/ruby/library/date/parse_spec.rb new file mode 100644 index 0000000000..5ef4f6e9b5 --- /dev/null +++ b/spec/ruby/library/date/parse_spec.rb @@ -0,0 +1,159 @@ +require_relative '../../spec_helper' +require_relative 'shared/parse' +require_relative 'shared/parse_us' +require_relative 'shared/parse_eu' +require 'date' + +describe "Date#parse" do + # The space separator is also different, doesn't work for only numbers + it "parses a day name into a Date object" do + d = Date.parse("friday") + d.should == Date.commercial(d.cwyear, d.cweek, 5) + end + + it "parses a month name into a Date object" do + d = Date.parse("october") + d.should == Date.civil(Date.today.year, 10) + end + + it "parses a month day into a Date object" do + d = Date.parse("5th") + d.should == Date.civil(Date.today.year, Date.today.month, 5) + end + + # Specs using numbers + it "throws an argument error for a single digit" do + ->{ Date.parse("1") }.should raise_error(ArgumentError) + end + + it "parses DD as month day number" do + d = Date.parse("10") + d.should == Date.civil(Date.today.year, Date.today.month, 10) + end + + it "parses DDD as year day number" do + d = Date.parse("100") + if Date.gregorian_leap?(Date.today.year) + d.should == Date.civil(Date.today.year, 4, 9) + else + d.should == Date.civil(Date.today.year, 4, 10) + end + end + + it "parses MMDD as month and day" do + d = Date.parse("1108") + d.should == Date.civil(Date.today.year, 11, 8) + end + + it "parses YYDDD as year and day number in 1969--2068" do + d = Date.parse("10100") + d.should == Date.civil(2010, 4, 10) + end + + it "parses YYMMDD as year, month and day in 1969--2068" do + d = Date.parse("201023") + d.should == Date.civil(2020, 10, 23) + end + + it "parses YYYYDDD as year and day number" do + d = Date.parse("1910100") + d.should == Date.civil(1910, 4, 10) + end + + it "parses YYYYMMDD as year, month and day number" do + d = Date.parse("19101101") + d.should == Date.civil(1910, 11, 1) + end + + it "raises a TypeError trying to parse non-String-like object" do + -> { Date.parse(1) }.should raise_error(TypeError) + -> { Date.parse([]) }.should raise_error(TypeError) + end + + it "coerces using to_str" do + c = Class.new do + attr_accessor :string + def to_str + @string + end + end + o = c.new + o.string = "19101101" + + d = Date.parse(o) + d.should == Date.civil(1910, 11, 1) + + # parse should not modify string value + o.to_str.should == "19101101" + end +end + +describe "Date#parse with '.' separator" do + before :all do + @sep = '.' + end + + it_should_behave_like :date_parse +end + +describe "Date#parse with '/' separator" do + before :all do + @sep = '/' + end + + it_should_behave_like :date_parse +end + +describe "Date#parse with ' ' separator" do + before :all do + @sep = ' ' + end + + it_should_behave_like :date_parse +end + +describe "Date#parse with '/' separator US-style" do + before :all do + @sep = '/' + end + + it_should_behave_like :date_parse_us +end + +describe "Date#parse with '-' separator EU-style" do + before :all do + @sep = '-' + end + + it_should_behave_like :date_parse_eu +end + +describe "Date#parse(.)" do + it "parses YYYY.MM.DD into a Date object" do + d = Date.parse("2007.10.01") + d.year.should == 2007 + d.month.should == 10 + d.day.should == 1 + end + + it "parses DD.MM.YYYY into a Date object" do + d = Date.parse("10.01.2007") + d.year.should == 2007 + d.month.should == 1 + d.day.should == 10 + end + + it "parses YY.MM.DD into a Date object using the year 20YY" do + d = Date.parse("10.01.07") + d.year.should == 2010 + d.month.should == 1 + d.day.should == 7 + end + + it "parses YY.MM.DD using the year digits as 20YY when given true as additional argument" do + d = Date.parse("10.01.07", true) + d.year.should == 2010 + d.month.should == 1 + d.day.should == 7 + end +end diff --git a/spec/ruby/library/date/plus_spec.rb b/spec/ruby/library/date/plus_spec.rb new file mode 100644 index 0000000000..0cb99fd4ca --- /dev/null +++ b/spec/ruby/library/date/plus_spec.rb @@ -0,0 +1,20 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#+" do + before :all do + @date = Date.civil(2000, 1, 1) + end + + it "returns a new Date object that is n days later than the current one" do + (@date + 31).should == Date.civil(2000, 2, 1) + end + + it "accepts a negative argument and returns a new Date that is earlier than the current one" do + (@date + -1).should == Date.civil(1999, 12, 31) + end + + it "raises TypeError if argument is not Numeric" do + -> { Date.today + Date.today }.should raise_error(TypeError) + end +end diff --git a/spec/ruby/library/date/prev_day_spec.rb b/spec/ruby/library/date/prev_day_spec.rb new file mode 100644 index 0000000000..cce24da875 --- /dev/null +++ b/spec/ruby/library/date/prev_day_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#prev_day" do + it "returns previous day" do + d = Date.new(2000, 7, 2).prev_day + d.should == Date.new(2000, 7, 1) + end + + it "returns three days ago across months" do + d = Date.new(2000, 7, 2).prev_day(3) + d.should == Date.new(2000, 6, 29) + end +end diff --git a/spec/ruby/library/date/prev_month_spec.rb b/spec/ruby/library/date/prev_month_spec.rb new file mode 100644 index 0000000000..3d0d1d437d --- /dev/null +++ b/spec/ruby/library/date/prev_month_spec.rb @@ -0,0 +1,29 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#prev_month" do + it "returns previous month" do + d = Date.new(2000, 9, 1).prev_month + d.should == Date.new(2000, 8, 1) + end + + it "returns three months ago" do + d = Date.new(2000, 10, 1).prev_month(3) + d.should == Date.new(2000, 7, 1) + end + + it "returns three months ago across years" do + d = Date.new(2000, 1, 1).prev_month(3) + d.should == Date.new(1999, 10, 1) + end + + it "returns last day of month two months ago" do + d = Date.new(2000, 3, 31).prev_month(2) + d.should == Date.new(2000, 1, 31) + end + + it "returns last day of previous month when same day does not exist" do + d = Date.new(2001, 3, 30).prev_month + d.should == Date.new(2001, 2, 28) + end +end diff --git a/spec/ruby/library/date/prev_year_spec.rb b/spec/ruby/library/date/prev_year_spec.rb new file mode 100644 index 0000000000..ba06dd198b --- /dev/null +++ b/spec/ruby/library/date/prev_year_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#prev_year" do + it "returns the day of the reform if date falls within calendar reform" do + calendar_reform_italy = Date.new(1582, 10, 4) + d1 = Date.new(1583, 10, 9).prev_year + d2 = Date.new(1583, 10, 10).prev_year + d1.should == calendar_reform_italy + d2.should == calendar_reform_italy + end +end diff --git a/spec/ruby/library/date/relationship_spec.rb b/spec/ruby/library/date/relationship_spec.rb new file mode 100644 index 0000000000..979516e164 --- /dev/null +++ b/spec/ruby/library/date/relationship_spec.rb @@ -0,0 +1,20 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#===" do + + it "returns 0 when comparing two equal dates" do + (Date.civil(2000, 04, 06) <=> Date.civil(2000, 04, 06)).should == 0 + end + + it "computes the difference between two dates" do + (Date.civil(2000, 04, 05) <=> Date.civil(2000, 04, 06)).should == -1 + (Date.civil(2001, 04, 05) <=> Date.civil(2000, 04, 06)).should == 1 + end + + it "compares to another numeric" do + (Date.civil(2000, 04, 05) <=> Date.civil(2000, 04, 06).jd).should == -1 + (Date.civil(2001, 04, 05) <=> Date.civil(2000, 04, 06).jd).should == 1 + end + +end diff --git a/spec/ruby/library/date/rfc3339_spec.rb b/spec/ruby/library/date/rfc3339_spec.rb new file mode 100644 index 0000000000..a8711d47b2 --- /dev/null +++ b/spec/ruby/library/date/rfc3339_spec.rb @@ -0,0 +1,13 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.rfc3339" do + it "needs to be reviewed for spec completeness" +end + +describe "Date._rfc3339" do + it "returns an empty hash if the argument is a invalid Date" do + h = Date._rfc3339('invalid') + h.should == {} + end +end diff --git a/spec/ruby/library/date/right_shift_spec.rb b/spec/ruby/library/date/right_shift_spec.rb new file mode 100644 index 0000000000..bd7de0e3d5 --- /dev/null +++ b/spec/ruby/library/date/right_shift_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#>>" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/saturday_spec.rb b/spec/ruby/library/date/saturday_spec.rb new file mode 100644 index 0000000000..1527b71d00 --- /dev/null +++ b/spec/ruby/library/date/saturday_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#saturday?" do + it "should be saturday" do + Date.new(2000, 1, 1).saturday?.should be_true + end +end diff --git a/spec/ruby/library/date/shared/civil.rb b/spec/ruby/library/date/shared/civil.rb new file mode 100644 index 0000000000..bbed4a8866 --- /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_error(ArgumentError) + -> { Date.send(@method, 2000, 12, 32) }.should raise_error(ArgumentError) + -> { Date.send(@method, 2000, 2, 30) }.should raise_error(ArgumentError) + -> { Date.send(@method, 1900, 2, 29) }.should raise_error(ArgumentError) + -> { Date.send(@method, 2000, 2, 29) }.should_not raise_error(ArgumentError) + + -> { Date.send(@method, 1582, 10, 14) }.should raise_error(ArgumentError) + -> { Date.send(@method, 1582, 10, 15) }.should_not raise_error(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/commercial.rb b/spec/ruby/library/date/shared/commercial.rb new file mode 100644 index 0000000000..39c9af47b6 --- /dev/null +++ b/spec/ruby/library/date/shared/commercial.rb @@ -0,0 +1,39 @@ +describe :date_commercial, shared: true do + it "creates a Date for Julian Day Number day 0 by default" do + d = Date.send(@method) + d.year.should == -4712 + d.month.should == 1 + d.day.should == 1 + end + + it "creates a Date for the monday in the year and week given" do + d = Date.send(@method, 2000, 1) + d.year.should == 2000 + d.month.should == 1 + d.day.should == 3 + d.cwday.should == 1 + end + + it "creates a Date for the correct day given the year, week and day number" do + d = Date.send(@method, 2004, 1, 1) + d.year.should == 2003 + d.month.should == 12 + d.day.should == 29 + d.cwday.should == 1 + d.cweek.should == 1 + d.cwyear.should == 2004 + end + + it "creates only Date objects for valid weeks" do + -> { Date.send(@method, 2004, 53, 1) }.should_not raise_error(ArgumentError) + -> { Date.send(@method, 2004, 53, 0) }.should raise_error(ArgumentError) + -> { Date.send(@method, 2004, 53, 8) }.should raise_error(ArgumentError) + -> { Date.send(@method, 2004, 54, 1) }.should raise_error(ArgumentError) + -> { Date.send(@method, 2004, 0, 1) }.should raise_error(ArgumentError) + + -> { Date.send(@method, 2003, 52, 1) }.should_not raise_error(ArgumentError) + -> { Date.send(@method, 2003, 53, 1) }.should raise_error(ArgumentError) + -> { Date.send(@method, 2003, 52, 0) }.should raise_error(ArgumentError) + -> { Date.send(@method, 2003, 52, 8) }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/library/date/shared/jd.rb b/spec/ruby/library/date/shared/jd.rb new file mode 100644 index 0000000000..511557b4f7 --- /dev/null +++ b/spec/ruby/library/date/shared/jd.rb @@ -0,0 +1,14 @@ +describe :date_jd, shared: true do + it "constructs a Date object if passed a Julian day" do + Date.send(@method, 2454482).should == Date.civil(2008, 1, 16) + end + + it "returns a Date object representing Julian day 0 (-4712-01-01) if no arguments passed" do + Date.send(@method).should == Date.civil(-4712, 1, 1) + end + + it "constructs a Date object if passed a negative number" do + Date.send(@method, -1).should == Date.civil(-4713, 12, 31) + end + +end diff --git a/spec/ruby/library/date/shared/month.rb b/spec/ruby/library/date/shared/month.rb new file mode 100644 index 0000000000..5fcb2cbeb0 --- /dev/null +++ b/spec/ruby/library/date/shared/month.rb @@ -0,0 +1,6 @@ +describe :date_month, shared: true do + it "returns the month" do + m = Date.new(2000, 7, 1).send(@method) + m.should == 7 + end +end diff --git a/spec/ruby/library/date/shared/ordinal.rb b/spec/ruby/library/date/shared/ordinal.rb new file mode 100644 index 0000000000..4b182d5a25 --- /dev/null +++ b/spec/ruby/library/date/shared/ordinal.rb @@ -0,0 +1,22 @@ +# reference: +# October 1582 (the Gregorian calendar, Civil Date) +# S M Tu W Th F S +# 1 2 3 4 15 16 +# 17 18 19 20 21 22 23 +# 24 25 26 27 28 29 30 +# 31 + +describe :date_ordinal, shared: true do + it "constructs a Date object from an ordinal date" do + # October 1582 (the Gregorian calendar, Ordinal Date) + # S M Tu W Th F S + # 274 275 276 277 278 279 + # 280 281 282 283 284 285 286 + # 287 288 289 290 291 292 293 + # 294 + Date.send(@method, 1582, 274).should == Date.civil(1582, 10, 1) + Date.send(@method, 1582, 277).should == Date.civil(1582, 10, 4) + Date.send(@method, 1582, 278).should == Date.civil(1582, 10, 15) + Date.send(@method, 1582, 287, Date::ENGLAND).should == Date.civil(1582, 10, 14, Date::ENGLAND) + 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 diff --git a/spec/ruby/library/date/shared/valid_civil.rb b/spec/ruby/library/date/shared/valid_civil.rb new file mode 100644 index 0000000000..545c207bbe --- /dev/null +++ b/spec/ruby/library/date/shared/valid_civil.rb @@ -0,0 +1,36 @@ +describe :date_valid_civil?, shared: true do + + # reference: + # October 1582 (the Gregorian calendar, Civil Date) + # S M Tu W Th F S + # 1 2 3 4 15 16 + # 17 18 19 20 21 22 23 + # 24 25 26 27 28 29 30 + # 31 + + it "returns true if it is a valid civil date" do + Date.send(@method, 1582, 10, 15).should be_true + Date.send(@method, 1582, 10, 14, Date::ENGLAND).should be_true + end + + it "returns false if it is not a valid civil date" do + Date.send(@method, 1582, 10, 14).should == false + end + + it "handles negative months and days" do + # October 1582 (the Gregorian calendar, Civil Date) + # S M Tu W Th F S + # -21 -20 -19 -18 -17 -16 + # -15 -14 -13 -12 -11 -10 -9 + # -8 -7 -6 -5 -4 -3 -2 + # -1 + Date.send(@method, 1582, -3, -22).should be_false + Date.send(@method, 1582, -3, -21).should be_true + Date.send(@method, 1582, -3, -18).should be_true + Date.send(@method, 1582, -3, -17).should be_true + + Date.send(@method, 2007, -11, -10).should be_true + Date.send(@method, 2008, -11, -10).should be_true + end + +end diff --git a/spec/ruby/library/date/shared/valid_commercial.rb b/spec/ruby/library/date/shared/valid_commercial.rb new file mode 100644 index 0000000000..117dfe1d3d --- /dev/null +++ b/spec/ruby/library/date/shared/valid_commercial.rb @@ -0,0 +1,34 @@ +describe :date_valid_commercial?, shared: true do + + it "returns true if it is a valid commercial date" do + # October 1582 (the Gregorian calendar, Commercial Date) + # M Tu W Th F Sa Su + # 39: 1 2 3 4 5 6 7 + # 40: 1 2 3 4 5 6 7 + # 41: 1 2 3 4 5 6 7 + Date.send(@method, 1582, 39, 4).should be_true + Date.send(@method, 1582, 39, 5).should be_true + Date.send(@method, 1582, 41, 4).should be_true + Date.send(@method, 1582, 41, 5).should be_true + Date.send(@method, 1582, 41, 4, Date::ENGLAND).should be_true + Date.send(@method, 1752, 37, 4, Date::ENGLAND).should be_true + end + + it "returns false it is not a valid commercial date" do + Date.send(@method, 1999, 53, 1).should be_false + end + + it "handles negative week and day numbers" do + # October 1582 (the Gregorian calendar, Commercial Date) + # M Tu W Th F Sa Su + # -12: -7 -6 -5 -4 -3 -2 -1 + # -11: -7 -6 -5 -4 -3 -2 -1 + # -10: -7 -6 -5 -4 -3 -2 -1 + Date.send(@method, 1582, -12, -4).should be_true + Date.send(@method, 1582, -12, -3).should be_true + Date.send(@method, 2007, -44, -2).should be_true + Date.send(@method, 2008, -44, -2).should be_true + Date.send(@method, 1999, -53, -1).should be_false + end + +end diff --git a/spec/ruby/library/date/shared/valid_jd.rb b/spec/ruby/library/date/shared/valid_jd.rb new file mode 100644 index 0000000000..e474dfb450 --- /dev/null +++ b/spec/ruby/library/date/shared/valid_jd.rb @@ -0,0 +1,20 @@ +describe :date_valid_jd?, shared: true do + it "returns true if passed a number value" do + Date.send(@method, -100).should be_true + Date.send(@method, 100.0).should be_true + Date.send(@method, 2**100).should be_true + Date.send(@method, Rational(1,2)).should be_true + end + + it "returns false if passed nil" do + Date.send(@method, nil).should be_false + end + + it "returns false if passed symbol" do + Date.send(@method, :number).should be_false + end + + it "returns false if passed false" do + Date.send(@method, false).should be_false + end +end diff --git a/spec/ruby/library/date/shared/valid_ordinal.rb b/spec/ruby/library/date/shared/valid_ordinal.rb new file mode 100644 index 0000000000..1ed961be23 --- /dev/null +++ b/spec/ruby/library/date/shared/valid_ordinal.rb @@ -0,0 +1,26 @@ +describe :date_valid_ordinal?, shared: true do + it "determines if the date is a valid ordinal date" do + # October 1582 (the Gregorian calendar, Ordinal Date) + # S M Tu W Th F S + # 274 275 276 277 278 279 + # 280 281 282 283 284 285 286 + # 287 288 289 290 291 292 293 + # 294 + Date.send(@method, 1582, 277).should == true + Date.send(@method, 1582, 278).should == true + Date.send(@method, 1582, 287).should == true + Date.send(@method, 1582, 288).should == true + end + + it "handles negative day numbers" do + # October 1582 (the Gregorian calendar, Ordinal Date) + # S M Tu W Th F S + # -82 -81 -80 -79 -78 -77 + # -76 -75 -74 -73 -72 -71 -70 + # -69 -68 -67 -66 -65 -64 -63 + # -62 + Date.send(@method, 1582, -79).should == true + Date.send(@method, 1582, -78).should == true + Date.send(@method, 2007, -100).should == true + end +end diff --git a/spec/ruby/library/date/start_spec.rb b/spec/ruby/library/date/start_spec.rb new file mode 100644 index 0000000000..8ba272a7f3 --- /dev/null +++ b/spec/ruby/library/date/start_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#start" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/step_spec.rb b/spec/ruby/library/date/step_spec.rb new file mode 100644 index 0000000000..6bbd671840 --- /dev/null +++ b/spec/ruby/library/date/step_spec.rb @@ -0,0 +1,56 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#step" do + + it "steps forward in time" do + ds = Date.civil(2008, 10, 11) + de = Date.civil(2008, 9, 29) + count = 0 + de.step(ds) do |d| + d.should <= ds + d.should >= de + count += 1 + end + count.should == 13 + + count = 0 + de.step(ds, 5) do |d| + d.should <= ds + d.should >= de + count += 1 + end + count.should == 3 + + count = 0 + ds.step(de) do |d|; count += 1; end + count.should == 0 + + end + + it "steps backward in time" do + ds = Date.civil(2000, 4, 14) + de = Date.civil(2000, 3, 29) + count = 0 + ds.step(de, -1) do |d| + d.should <= ds + d.should >= de + count += 1 + end + count.should == 17 + + count = 0 + ds.step(de, -5) do |d| + d.should <= ds + d.should >= de + count += 1 + end + count.should == 4 + + count = 0 + de.step(ds, -1) do |d|; count += 1; end + count.should == 0 + + end + +end diff --git a/spec/ruby/library/date/strftime_spec.rb b/spec/ruby/library/date/strftime_spec.rb new file mode 100644 index 0000000000..1b93a8d1b2 --- /dev/null +++ b/spec/ruby/library/date/strftime_spec.rb @@ -0,0 +1,41 @@ +require_relative "../../spec_helper" +require 'date' +require_relative '../../shared/time/strftime_for_date' +date_version = defined?(Date::VERSION) ? Date::VERSION : '3.1.0' + +describe "Date#strftime" do + before :all do + @new_date = -> y, m, d { Date.civil(y,m,d) } + + @date = Date.civil(2000, 4, 9) + end + + it_behaves_like :strftime_date, :strftime + + # Differences with Time + it "should be able to print the date with no argument" do + @date.strftime.should == "2000-04-09" + @date.strftime.should == @date.to_s + end + + # %Z is %:z for Date/DateTime + it "should be able to show the timezone with a : separator" do + @date.strftime("%Z").should == "+00:00" + end + + it "should be able to show the commercial week" do + @date.strftime("%v").should == " 9-APR-2000" + @date.strftime("%v").should != @date.strftime('%e-%b-%Y') + end + + # additional conversion specifiers only in Date/DateTime + it 'shows the number of milliseconds since epoch' do + DateTime.new(1970, 1, 1).strftime('%Q').should == "0" + @date.strftime("%Q").should == "955238400000" + end + + it "should be able to show a full notation" do + @date.strftime("%+").should == "Sun Apr 9 00:00:00 +00:00 2000" + @date.strftime("%+").should == @date.strftime('%a %b %e %H:%M:%S %Z %Y') + end +end diff --git a/spec/ruby/library/date/strptime_spec.rb b/spec/ruby/library/date/strptime_spec.rb new file mode 100644 index 0000000000..c90721751e --- /dev/null +++ b/spec/ruby/library/date/strptime_spec.rb @@ -0,0 +1,149 @@ +require 'date' +require_relative '../../spec_helper' + +describe "Date#strptime" do + + it "returns January 1, 4713 BCE when given no arguments" do + Date.strptime.should == Date.civil(-4712, 1, 1) + end + + it "uses the default format when not given a date format" do + Date.strptime("2000-04-06").should == Date.civil(2000, 4, 6) + Date.civil(2000, 4, 6).strftime.should == Date.civil(2000, 4, 6).to_s + end + + it "parses a full day name" do + d = Date.today + expected_date = Date.commercial(d.cwyear, d.cweek, 4) + # strptime assumed week that start on sunday, not monday + expected_date += 7 if d.cwday == 7 + Date.strptime("Thursday", "%A").should == expected_date + end + + it "parses a short day name" do + d = Date.today + expected_date = Date.commercial(d.cwyear, d.cweek, 4) + # strptime assumed week that start on sunday, not monday + expected_date += 7 if d.cwday == 7 + Date.strptime("Thu", "%a").should == expected_date + end + + it "parses a full month name" do + d = Date.today + Date.strptime("April", "%B").should == Date.civil(d.year, 4, 1) + end + + it "parses a short month name" do + d = Date.today + Date.strptime("Apr", "%b").should == Date.civil(d.year, 4, 1) + Date.strptime("Apr", "%h").should == Date.civil(d.year, 4, 1) + end + + it "parses a century" do + Date.strptime("06 20", "%y %C").should == Date.civil(2006, 1, 1) + end + + it "parses a month day with leading zeroes" do + d = Date.today + Date.strptime("06", "%d").should == Date.civil(d.year, d.month, 6) + end + + it "parses a month day with leading spaces" do + d = Date.today + Date.strptime(" 6", "%e").should == Date.civil(d.year, d.month, 6) + end + + it "parses a commercial year with leading zeroes" do + Date.strptime("2000", "%G").should == Date.civil(2000, 1, 3) + Date.strptime("2002", "%G").should == Date.civil(2001, 12, 31) + end + + it "parses a commercial year with only two digits" do + Date.strptime("68", "%g").should == Date.civil(2068, 1, 2) + Date.strptime("69", "%g").should == Date.civil(1968, 12, 30) + end + + it "parses a year day with leading zeroes" do + d = Date.today + if Date.gregorian_leap?(Date.today.year) + Date.strptime("097", "%j").should == Date.civil(d.year, 4, 6) + else + Date.strptime("097", "%j").should == Date.civil(d.year, 4, 7) + end + end + + it "parses a month with leading zeroes" do + d = Date.today + Date.strptime("04", "%m").should == Date.civil(d.year, 4, 1) + end + + it "parses a week number for a week starting on Sunday" do + Date.strptime("2010/1", "%Y/%U").should == Date.civil(2010, 1, 3) + end + + # See http://redmine.ruby-lang.org/repositories/diff/ruby-19?rev=24500 + it "parses a week number for a week starting on Monday" do + Date.strptime("2010/1", "%Y/%W").should == Date.civil(2010, 1, 4) + end + + it "parses a commercial week day" do + Date.strptime("2008 1", "%G %u").should == Date.civil(2007, 12, 31) + end + + it "parses a commercial week" do + d = Date.commercial(Date.today.cwyear,1,1) + Date.strptime("1", "%V").should == d + Date.strptime("15", "%V").should == Date.commercial(d.cwyear, 15, 1) + end + + it "parses a week day" do + Date.strptime("2007 4", "%Y %w").should == Date.civil(2007, 1, 4) + end + + it "parses a year in YYYY format" do + Date.strptime("2007", "%Y").should == Date.civil(2007, 1, 1) + end + + it "parses a year in YY format" do + Date.strptime("00", "%y").should == Date.civil(2000, 1, 1) + end + + ############################ + # Specs that combine stuff # + ############################ + + it "parses a full date" do + Date.strptime("Thu Apr 6 00:00:00 2000", "%c").should == Date.civil(2000, 4, 6) + Date.strptime("Thu Apr 6 00:00:00 2000", "%a %b %e %H:%M:%S %Y").should == Date.civil(2000, 4, 6) + end + + it "parses a date with slashes" do + Date.strptime("04/06/00", "%D").should == Date.civil(2000, 4, 6) + Date.strptime("04/06/00", "%m/%d/%y").should == Date.civil(2000, 4, 6) + end + + it "parses a date given as YYYY-MM-DD" do + Date.strptime("2000-04-06", "%F").should == Date.civil(2000, 4, 6) + Date.strptime("2000-04-06", "%Y-%m-%d").should == Date.civil(2000, 4, 6) + end + + it "parses a commercial week" do + Date.strptime(" 9-Apr-2000", "%v").should == Date.civil(2000, 4, 9) + Date.strptime(" 9-Apr-2000", "%e-%b-%Y").should == Date.civil(2000, 4, 9) + end + + it "parses a date given MM/DD/YY" do + Date.strptime("04/06/00", "%x").should == Date.civil(2000, 4, 6) + Date.strptime("04/06/00", "%m/%d/%y").should == Date.civil(2000, 4, 6) + end + + it "parses a date given in full notation" do + Date.strptime("Sun Apr 9 00:00:00 +00:00 2000", "%+").should == Date.civil(2000, 4, 9) + Date.strptime("Sun Apr 9 00:00:00 +00:00 2000", "%a %b %e %H:%M:%S %Z %Y").should == Date.civil(2000, 4, 9) + end + +end + +describe "Date.strptime" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/succ_spec.rb b/spec/ruby/library/date/succ_spec.rb new file mode 100644 index 0000000000..c4a902aa63 --- /dev/null +++ b/spec/ruby/library/date/succ_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#succ" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/sunday_spec.rb b/spec/ruby/library/date/sunday_spec.rb new file mode 100644 index 0000000000..c3a817fa86 --- /dev/null +++ b/spec/ruby/library/date/sunday_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#sunday?" do + it "should be sunday" do + Date.new(2000, 1, 2).sunday?.should be_true + end +end diff --git a/spec/ruby/library/date/thursday_spec.rb b/spec/ruby/library/date/thursday_spec.rb new file mode 100644 index 0000000000..74b5f40365 --- /dev/null +++ b/spec/ruby/library/date/thursday_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#thursday?" do + it "should be thursday" do + Date.new(2000, 1, 6).thursday?.should be_true + end +end diff --git a/spec/ruby/library/date/time/to_date_spec.rb b/spec/ruby/library/date/time/to_date_spec.rb new file mode 100644 index 0000000000..f9132da289 --- /dev/null +++ b/spec/ruby/library/date/time/to_date_spec.rb @@ -0,0 +1,42 @@ + +require_relative '../../../spec_helper' +require 'time' + +describe "Time#to_date" do + it "yields accurate julian date for ambiguous pre-Gregorian reform value" do + Time.utc(1582, 10, 4).to_date.jd.should == Date::ITALY - 11 # 2299150j + end + + it "yields accurate julian date for Julian-Gregorian gap value" do + Time.utc(1582, 10, 14).to_date.jd.should == Date::ITALY - 1 # 2299160j + end + + it "yields accurate julian date for post-Gregorian reform value" do + Time.utc(1582, 10, 15).to_date.jd.should == Date::ITALY # 2299161j + end + + it "yields same julian day regardless of UTC time value" do + Time.utc(1582, 10, 15, 00, 00, 00).to_date.jd.should == Date::ITALY + Time.utc(1582, 10, 15, 23, 59, 59).to_date.jd.should == Date::ITALY + end + + it "yields same julian day regardless of local time or zone" do + + with_timezone("Pacific/Pago_Pago", -11) do + Time.local(1582, 10, 15, 00, 00, 00).to_date.jd.should == Date::ITALY + Time.local(1582, 10, 15, 23, 59, 59).to_date.jd.should == Date::ITALY + end + + with_timezone("Asia/Kamchatka", +12) do + Time.local(1582, 10, 15, 00, 00, 00).to_date.jd.should == Date::ITALY + Time.local(1582, 10, 15, 23, 59, 59).to_date.jd.should == Date::ITALY + end + + end + + it "yields date with default Calendar reform day" do + Time.utc(1582, 10, 4).to_date.start.should == Date::ITALY + Time.utc(1582, 10, 14).to_date.start.should == Date::ITALY + Time.utc(1582, 10, 15).to_date.start.should == Date::ITALY + end +end diff --git a/spec/ruby/library/date/time_to_day_fraction_spec.rb b/spec/ruby/library/date/time_to_day_fraction_spec.rb new file mode 100644 index 0000000000..e59980e036 --- /dev/null +++ b/spec/ruby/library/date/time_to_day_fraction_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.time_to_day_fraction" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/to_s_spec.rb b/spec/ruby/library/date/to_s_spec.rb new file mode 100644 index 0000000000..fe7cb19a46 --- /dev/null +++ b/spec/ruby/library/date/to_s_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#to_s" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/today_spec.rb b/spec/ruby/library/date/today_spec.rb new file mode 100644 index 0000000000..7c6ebc9cb4 --- /dev/null +++ b/spec/ruby/library/date/today_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.today" do + it "returns a Date object" do + Date.today.should be_kind_of Date + end + + it "sets Date object to the current date" do + today = Date.today + now = Time.now + (now - today.to_time).should be_close(0.0, 24 * 60 * 60) + end +end diff --git a/spec/ruby/library/date/tuesday_spec.rb b/spec/ruby/library/date/tuesday_spec.rb new file mode 100644 index 0000000000..052837b54e --- /dev/null +++ b/spec/ruby/library/date/tuesday_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#tuesday?" do + it "should be tuesday" do + Date.new(2000, 1, 4).tuesday?.should be_true + end +end diff --git a/spec/ruby/library/date/upto_spec.rb b/spec/ruby/library/date/upto_spec.rb new file mode 100644 index 0000000000..8745be85b3 --- /dev/null +++ b/spec/ruby/library/date/upto_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#upto" do + it "returns future dates for the default step value" do + ds = Date.civil(2008, 10, 11) + de = Date.civil(2008, 9, 29) + count = 0 + de.upto(ds) do |d| + d.should <= ds + d.should >= de + count += 1 + end + count.should == 13 + end +end diff --git a/spec/ruby/library/date/valid_civil_spec.rb b/spec/ruby/library/date/valid_civil_spec.rb new file mode 100644 index 0000000000..00f2c57205 --- /dev/null +++ b/spec/ruby/library/date/valid_civil_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require_relative 'shared/valid_civil' +require 'date' + +describe "Date#valid_civil?" do + + it_behaves_like :date_valid_civil?, :valid_civil? + +end diff --git a/spec/ruby/library/date/valid_commercial_spec.rb b/spec/ruby/library/date/valid_commercial_spec.rb new file mode 100644 index 0000000000..7e96782b6b --- /dev/null +++ b/spec/ruby/library/date/valid_commercial_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require_relative 'shared/valid_commercial' +require 'date' + +describe "Date#valid_commercial?" do + + it_behaves_like :date_valid_commercial?, :valid_commercial? +end diff --git a/spec/ruby/library/date/valid_date_spec.rb b/spec/ruby/library/date/valid_date_spec.rb new file mode 100644 index 0000000000..f12a71d966 --- /dev/null +++ b/spec/ruby/library/date/valid_date_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/valid_civil' +require 'date' + +describe "Date#valid_date?" do + it_behaves_like :date_valid_civil?, :valid_date? +end diff --git a/spec/ruby/library/date/valid_jd_spec.rb b/spec/ruby/library/date/valid_jd_spec.rb new file mode 100644 index 0000000000..aecaaabcf4 --- /dev/null +++ b/spec/ruby/library/date/valid_jd_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require_relative 'shared/valid_jd' +require 'date' + +describe "Date.valid_jd?" do + + it_behaves_like :date_valid_jd?, :valid_jd? + +end diff --git a/spec/ruby/library/date/valid_ordinal_spec.rb b/spec/ruby/library/date/valid_ordinal_spec.rb new file mode 100644 index 0000000000..58d548c704 --- /dev/null +++ b/spec/ruby/library/date/valid_ordinal_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require_relative 'shared/valid_ordinal' +require 'date' + +describe "Date.valid_ordinal?" do + + it_behaves_like :date_valid_ordinal?, :valid_ordinal? + +end diff --git a/spec/ruby/library/date/valid_time_spec.rb b/spec/ruby/library/date/valid_time_spec.rb new file mode 100644 index 0000000000..87c239bedb --- /dev/null +++ b/spec/ruby/library/date/valid_time_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.valid_time?" do + it "needs to be reviewed for spec completeness" +end diff --git a/spec/ruby/library/date/wday_spec.rb b/spec/ruby/library/date/wday_spec.rb new file mode 100644 index 0000000000..303905ed35 --- /dev/null +++ b/spec/ruby/library/date/wday_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#wday" do + it "returns the week day as a number starting with Sunday as 0" do + w = Date.new(2000, 1, 1).wday + w.should == 6 + end +end diff --git a/spec/ruby/library/date/wednesday_spec.rb b/spec/ruby/library/date/wednesday_spec.rb new file mode 100644 index 0000000000..e80ec23dd2 --- /dev/null +++ b/spec/ruby/library/date/wednesday_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#wednesday?" do + it "should be wednesday" do + Date.new(2000, 1, 5).wednesday?.should be_true + end +end diff --git a/spec/ruby/library/date/yday_spec.rb b/spec/ruby/library/date/yday_spec.rb new file mode 100644 index 0000000000..7dd42e52a5 --- /dev/null +++ b/spec/ruby/library/date/yday_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative '../../shared/time/yday' +require 'date' + +describe "Date#yday" do + it_behaves_like :time_yday, -> year, month, day { Date.new(year, month, day).yday } +end diff --git a/spec/ruby/library/date/year_spec.rb b/spec/ruby/library/date/year_spec.rb new file mode 100644 index 0000000000..90d14e5a39 --- /dev/null +++ b/spec/ruby/library/date/year_spec.rb @@ -0,0 +1,9 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date#year" do + it "returns the year" do + y = Date.new(2000, 7, 1).year + y.should == 2000 + end +end diff --git a/spec/ruby/library/date/zone_to_diff_spec.rb b/spec/ruby/library/date/zone_to_diff_spec.rb new file mode 100644 index 0000000000..354daaaee4 --- /dev/null +++ b/spec/ruby/library/date/zone_to_diff_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require 'date' + +describe "Date.zone_to_diff" do + it "needs to be reviewed for spec completeness" +end |
