diff options
Diffstat (limited to 'spec/ruby/library/date')
-rw-r--r-- | spec/ruby/library/date/civil_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/library/date/deconstruct_keys_spec.rb | 44 | ||||
-rw-r--r-- | spec/ruby/library/date/iso8601_spec.rb | 21 | ||||
-rw-r--r-- | spec/ruby/library/date/new_spec.rb | 1 | ||||
-rw-r--r-- | spec/ruby/library/date/parse_spec.rb | 10 | ||||
-rw-r--r-- | spec/ruby/library/date/shared/new_bang.rb | 14 | ||||
-rw-r--r-- | spec/ruby/library/date/shared/parse.rb | 4 | ||||
-rw-r--r-- | spec/ruby/library/date/shared/parse_eu.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/date/shared/parse_us.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/date/strftime_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/library/date/time/to_date_spec.rb | 42 |
11 files changed, 127 insertions, 38 deletions
diff --git a/spec/ruby/library/date/civil_spec.rb b/spec/ruby/library/date/civil_spec.rb index f3537c2f84..1c780fce56 100644 --- a/spec/ruby/library/date/civil_spec.rb +++ b/spec/ruby/library/date/civil_spec.rb @@ -2,11 +2,6 @@ require_relative '../../spec_helper' require_relative 'shared/civil' require 'date' -describe "Date#civil" do - it_behaves_like :date_civil, :civil -end - - describe "Date.civil" do - it "needs to be reviewed for spec completeness" + it_behaves_like :date_civil, :civil 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..92579e35c7 --- /dev/null +++ b/spec/ruby/library/date/deconstruct_keys_spec.rb @@ -0,0 +1,44 @@ +require_relative '../../spec_helper' +require 'date' +date_version = defined?(Date::VERSION) ? Date::VERSION : '3.1.0' + +version_is date_version, "3.3" do #ruby_version_is "3.2" do + 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 +end diff --git a/spec/ruby/library/date/iso8601_spec.rb b/spec/ruby/library/date/iso8601_spec.rb index a29652014e..af66845a6b 100644 --- a/spec/ruby/library/date/iso8601_spec.rb +++ b/spec/ruby/library/date/iso8601_spec.rb @@ -22,6 +22,18 @@ describe "Date.iso8601" do 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 @@ -32,4 +44,13 @@ describe "Date._iso8601" 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/new_spec.rb b/spec/ruby/library/date/new_spec.rb index 18120118c0..cb64cabce6 100644 --- a/spec/ruby/library/date/new_spec.rb +++ b/spec/ruby/library/date/new_spec.rb @@ -1,7 +1,6 @@ require 'date' require_relative '../../spec_helper' require_relative 'shared/civil' -require_relative 'shared/new_bang' describe "Date.new" do it_behaves_like :date_civil, :new diff --git a/spec/ruby/library/date/parse_spec.rb b/spec/ruby/library/date/parse_spec.rb index bfbe86fbac..5ef4f6e9b5 100644 --- a/spec/ruby/library/date/parse_spec.rb +++ b/spec/ruby/library/date/parse_spec.rb @@ -93,7 +93,7 @@ describe "Date#parse with '.' separator" do @sep = '.' end - it_should_behave_like "date_parse" + it_should_behave_like :date_parse end describe "Date#parse with '/' separator" do @@ -101,7 +101,7 @@ describe "Date#parse with '/' separator" do @sep = '/' end - it_should_behave_like "date_parse" + it_should_behave_like :date_parse end describe "Date#parse with ' ' separator" do @@ -109,7 +109,7 @@ describe "Date#parse with ' ' separator" do @sep = ' ' end - it_should_behave_like "date_parse" + it_should_behave_like :date_parse end describe "Date#parse with '/' separator US-style" do @@ -117,7 +117,7 @@ describe "Date#parse with '/' separator US-style" do @sep = '/' end - it_should_behave_like "date_parse_us" + it_should_behave_like :date_parse_us end describe "Date#parse with '-' separator EU-style" do @@ -125,7 +125,7 @@ describe "Date#parse with '-' separator EU-style" do @sep = '-' end - it_should_behave_like "date_parse_eu" + it_should_behave_like :date_parse_eu end describe "Date#parse(.)" do diff --git a/spec/ruby/library/date/shared/new_bang.rb b/spec/ruby/library/date/shared/new_bang.rb deleted file mode 100644 index 90f1b432f0..0000000000 --- a/spec/ruby/library/date/shared/new_bang.rb +++ /dev/null @@ -1,14 +0,0 @@ -describe :date_new_bang, shared: true do - - it "returns a new Date object set to Astronomical Julian Day 0 if no arguments passed" do - d = Date.send(@method) - d.ajd.should == 0 - end - - it "accepts astronomical julian day number, offset as a fraction of a day and returns a new Date object" do - d = Date.send(@method, 10, 0.5) - d.ajd.should == 10 - d.jd.should == 11 - end - -end diff --git a/spec/ruby/library/date/shared/parse.rb b/spec/ruby/library/date/shared/parse.rb index 1015285e04..40af908386 100644 --- a/spec/ruby/library/date/shared/parse.rb +++ b/spec/ruby/library/date/shared/parse.rb @@ -13,7 +13,7 @@ describe :date_parse, shared: true do d.day.should == 23 end - it "can parse a 'mmm DD YYYY' string into a Date object" do + 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 @@ -42,7 +42,7 @@ describe :date_parse, shared: true do d.should == Date.civil(2005, 11, 5) end - it "can parse a year, day and month name into a Date object" do + 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 diff --git a/spec/ruby/library/date/shared/parse_eu.rb b/spec/ruby/library/date/shared/parse_eu.rb index ecb15e3c0e..3819524a57 100644 --- a/spec/ruby/library/date/shared/parse_eu.rb +++ b/spec/ruby/library/date/shared/parse_eu.rb @@ -7,28 +7,28 @@ describe :date_parse_eu, shared: true do d.day.should == 1 end - it "can parse a MM-DD-YYYY string into a Date object" do + 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 MM-DD-YY string into a Date object" do + 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 MM-DD-YY string into a Date object NOT using the year digits as 20XX" do + 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 MM-DD-YY string into a Date object using the year digits as 20XX" do + 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 diff --git a/spec/ruby/library/date/shared/parse_us.rb b/spec/ruby/library/date/shared/parse_us.rb index 7be62b1af1..17e2fc96c1 100644 --- a/spec/ruby/library/date/shared/parse_us.rb +++ b/spec/ruby/library/date/shared/parse_us.rb @@ -6,28 +6,28 @@ describe :date_parse_us, shared: true do d.day.should == 1 end - it "parses a MM#{@sep}DD#{@sep}YYYY string into a Date object" do + 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 MM#{@sep}DD#{@sep}YY string into a Date object" do + 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 MM#{@sep}DD#{@sep}YY string into a Date object NOT using the year digits as 20XX" do + 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 MM#{@sep}DD#{@sep}YY string into a Date object using the year digits as 20XX" do + 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 diff --git a/spec/ruby/library/date/strftime_spec.rb b/spec/ruby/library/date/strftime_spec.rb index 8b1ebe061c..b5232a2073 100644 --- a/spec/ruby/library/date/strftime_spec.rb +++ b/spec/ruby/library/date/strftime_spec.rb @@ -1,5 +1,7 @@ +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 @@ -22,14 +24,14 @@ describe "Date#strftime" do end # %v is %e-%b-%Y for Date/DateTime - ruby_version_is ""..."3.1" do + version_is date_version, ""..."3.2" do #ruby_version_is ""..."3.1" do 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 end - ruby_version_is "3.1" do + version_is date_version, "3.2" do #ruby_version_is "3.1" do 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') 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 |