summaryrefslogtreecommitdiff
path: root/spec/ruby/library/datetime
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/datetime')
-rw-r--r--spec/ruby/library/datetime/deconstruct_keys_spec.rb44
-rw-r--r--spec/ruby/library/datetime/hour_spec.rb10
-rw-r--r--spec/ruby/library/datetime/new_spec.rb2
-rw-r--r--spec/ruby/library/datetime/now_spec.rb2
-rw-r--r--spec/ruby/library/datetime/parse_spec.rb12
-rw-r--r--spec/ruby/library/datetime/rfc2822_spec.rb4
-rw-r--r--spec/ruby/library/datetime/shared/min.rb10
-rw-r--r--spec/ruby/library/datetime/shared/sec.rb6
-rw-r--r--spec/ruby/library/datetime/strftime_spec.rb7
-rw-r--r--spec/ruby/library/datetime/time/to_datetime_spec.rb40
-rw-r--r--spec/ruby/library/datetime/to_date_spec.rb2
-rw-r--r--spec/ruby/library/datetime/to_s_spec.rb2
-rw-r--r--spec/ruby/library/datetime/to_time_spec.rb18
-rw-r--r--spec/ruby/library/datetime/yday_spec.rb7
14 files changed, 137 insertions, 29 deletions
diff --git a/spec/ruby/library/datetime/deconstruct_keys_spec.rb b/spec/ruby/library/datetime/deconstruct_keys_spec.rb
new file mode 100644
index 0000000000..07a7bda881
--- /dev/null
+++ b/spec/ruby/library/datetime/deconstruct_keys_spec.rb
@@ -0,0 +1,44 @@
+require_relative '../../spec_helper'
+require 'date'
+date_version = defined?(Date::VERSION) ? Date::VERSION : '3.1.0'
+
+describe "DateTime#deconstruct_keys" do
+ it "returns whole hash for nil as an argument" do
+ d = DateTime.new(2022, 10, 5, 13, 30)
+ res = { year: 2022, month: 10, day: 5, yday: 278, wday: 3, hour: 13,
+ min: 30, sec: 0, sec_fraction: (0/1), zone: "+00:00" }
+ d.deconstruct_keys(nil).should == res
+ end
+
+ it "returns only specified keys" do
+ d = DateTime.new(2022, 10, 5, 13, 39)
+ d.deconstruct_keys([:zone, :hour]).should == { zone: "+00:00", hour: 13 }
+ end
+
+ it "requires one argument" do
+ -> {
+ DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys
+ }.should.raise(ArgumentError)
+ end
+
+ it "it raises error when argument is neither nil nor array" do
+ d = DateTime.new(2022, 10, 5, 13, 30)
+
+ -> { d.deconstruct_keys(1) }.should.raise(TypeError, "wrong argument type Integer (expected Array or nil)")
+ -> { d.deconstruct_keys("asd") }.should.raise(TypeError, "wrong argument type String (expected Array or nil)")
+ -> { d.deconstruct_keys(:x) }.should.raise(TypeError, "wrong argument type Symbol (expected Array or nil)")
+ -> { d.deconstruct_keys({}) }.should.raise(TypeError, "wrong argument type Hash (expected Array or nil)")
+ end
+
+ it "returns {} when passed []" do
+ DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys([]).should == {}
+ end
+
+ it "ignores non-Symbol keys" do
+ DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys(['year', []]).should == {}
+ end
+
+ it "ignores not existing Symbol keys" do
+ DateTime.new(2022, 10, 5, 13, 30).deconstruct_keys([:year, :a]).should == { year: 2022 }
+ end
+end
diff --git a/spec/ruby/library/datetime/hour_spec.rb b/spec/ruby/library/datetime/hour_spec.rb
index 8efd5f92f0..383a85fe60 100644
--- a/spec/ruby/library/datetime/hour_spec.rb
+++ b/spec/ruby/library/datetime/hour_spec.rb
@@ -15,23 +15,23 @@ describe "DateTime#hour" do
end
it "raises an error for Rational" do
- -> { new_datetime(hour: 1 + Rational(1,2)) }.should raise_error(ArgumentError)
+ -> { new_datetime(hour: 1 + Rational(1,2)) }.should.raise(ArgumentError)
end
it "raises an error for Float" do
- -> { new_datetime(hour: 1.5).hour }.should raise_error(ArgumentError)
+ -> { new_datetime(hour: 1.5).hour }.should.raise(ArgumentError)
end
it "raises an error for Rational" do
- -> { new_datetime(day: 1 + Rational(1,2)) }.should raise_error(ArgumentError)
+ -> { new_datetime(day: 1 + Rational(1,2)) }.should.raise(ArgumentError)
end
it "raises an error, when the hour is smaller than -24" do
- -> { new_datetime(hour: -25) }.should raise_error(ArgumentError)
+ -> { new_datetime(hour: -25) }.should.raise(ArgumentError)
end
it "raises an error, when the hour is larger than 24" do
- -> { new_datetime(hour: 25) }.should raise_error(ArgumentError)
+ -> { new_datetime(hour: 25) }.should.raise(ArgumentError)
end
it "raises an error for hour fractions smaller than -24" do
diff --git a/spec/ruby/library/datetime/new_spec.rb b/spec/ruby/library/datetime/new_spec.rb
index 6a4dced384..2b3c3f156c 100644
--- a/spec/ruby/library/datetime/new_spec.rb
+++ b/spec/ruby/library/datetime/new_spec.rb
@@ -47,6 +47,6 @@ describe "DateTime.new" do
end
it "raises an error on invalid arguments" do
- -> { new_datetime(minute: 999) }.should raise_error(ArgumentError)
+ -> { new_datetime(minute: 999) }.should.raise(ArgumentError)
end
end
diff --git a/spec/ruby/library/datetime/now_spec.rb b/spec/ruby/library/datetime/now_spec.rb
index 9f22153c15..5c0411c2be 100644
--- a/spec/ruby/library/datetime/now_spec.rb
+++ b/spec/ruby/library/datetime/now_spec.rb
@@ -3,7 +3,7 @@ require 'date'
describe "DateTime.now" do
it "creates an instance of DateTime" do
- DateTime.now.should be_an_instance_of(DateTime)
+ DateTime.now.should.instance_of?(DateTime)
end
it "sets the current date" do
diff --git a/spec/ruby/library/datetime/parse_spec.rb b/spec/ruby/library/datetime/parse_spec.rb
index e9bf4e2ed1..0a965273a0 100644
--- a/spec/ruby/library/datetime/parse_spec.rb
+++ b/spec/ruby/library/datetime/parse_spec.rb
@@ -20,7 +20,7 @@ describe "DateTime.parse" do
# Specs using numbers
it "throws an argument error for a single digit" do
- ->{ DateTime.parse("1") }.should raise_error(ArgumentError)
+ ->{ DateTime.parse("1") }.should.raise(ArgumentError)
end
it "parses DD as month day number" do
@@ -54,23 +54,23 @@ describe "DateTime.parse" do
end
it "throws an argument error for invalid month values" do
- ->{DateTime.parse("2012-13-08T15:43:59")}.should raise_error(ArgumentError)
+ ->{DateTime.parse("2012-13-08T15:43:59")}.should.raise(ArgumentError)
end
it "throws an argument error for invalid day values" do
- ->{DateTime.parse("2012-12-32T15:43:59")}.should raise_error(ArgumentError)
+ ->{DateTime.parse("2012-12-32T15:43:59")}.should.raise(ArgumentError)
end
it "throws an argument error for invalid hour values" do
- ->{DateTime.parse("2012-12-31T25:43:59")}.should raise_error(ArgumentError)
+ ->{DateTime.parse("2012-12-31T25:43:59")}.should.raise(ArgumentError)
end
it "throws an argument error for invalid minute values" do
- ->{DateTime.parse("2012-12-31T25:43:59")}.should raise_error(ArgumentError)
+ ->{DateTime.parse("2012-12-31T25:43:59")}.should.raise(ArgumentError)
end
it "throws an argument error for invalid second values" do
- ->{DateTime.parse("2012-11-08T15:43:61")}.should raise_error(ArgumentError)
+ ->{DateTime.parse("2012-11-08T15:43:61")}.should.raise(ArgumentError)
end
end
diff --git a/spec/ruby/library/datetime/rfc2822_spec.rb b/spec/ruby/library/datetime/rfc2822_spec.rb
index 70bfca60b4..11b79a1e84 100644
--- a/spec/ruby/library/datetime/rfc2822_spec.rb
+++ b/spec/ruby/library/datetime/rfc2822_spec.rb
@@ -3,4 +3,8 @@ require 'date'
describe "DateTime.rfc2822" do
it "needs to be reviewed for spec completeness"
+
+ it "raises DateError if passed nil" do
+ -> { DateTime.rfc2822(nil) }.should.raise(Date::Error, "invalid date")
+ end
end
diff --git a/spec/ruby/library/datetime/shared/min.rb b/spec/ruby/library/datetime/shared/min.rb
index a35b839281..04e5f3457a 100644
--- a/spec/ruby/library/datetime/shared/min.rb
+++ b/spec/ruby/library/datetime/shared/min.rb
@@ -14,23 +14,23 @@ describe :datetime_min, shared: true do
end
it "raises an error for Rational" do
- -> { new_datetime minute: 5 + Rational(1,2) }.should raise_error(ArgumentError)
+ -> { new_datetime minute: 5 + Rational(1,2) }.should.raise(ArgumentError)
end
it "raises an error for Float" do
- -> { new_datetime minute: 5.5 }.should raise_error(ArgumentError)
+ -> { new_datetime minute: 5.5 }.should.raise(ArgumentError)
end
it "raises an error for Rational" do
- -> { new_datetime(hour: 2 + Rational(1,2)) }.should raise_error(ArgumentError)
+ -> { new_datetime(hour: 2 + Rational(1,2)) }.should.raise(ArgumentError)
end
it "raises an error, when the minute is smaller than -60" do
- -> { new_datetime(minute: -61) }.should raise_error(ArgumentError)
+ -> { new_datetime(minute: -61) }.should.raise(ArgumentError)
end
it "raises an error, when the minute is greater or equal than 60" do
- -> { new_datetime(minute: 60) }.should raise_error(ArgumentError)
+ -> { new_datetime(minute: 60) }.should.raise(ArgumentError)
end
it "raises an error for minute fractions smaller than -60" do
diff --git a/spec/ruby/library/datetime/shared/sec.rb b/spec/ruby/library/datetime/shared/sec.rb
index 60009213aa..5af5db4fb2 100644
--- a/spec/ruby/library/datetime/shared/sec.rb
+++ b/spec/ruby/library/datetime/shared/sec.rb
@@ -23,15 +23,15 @@ describe :datetime_sec, shared: true do
end
it "raises an error when minute is given as a rational" do
- -> { new_datetime(minute: 5 + Rational(1,2)) }.should raise_error(ArgumentError)
+ -> { new_datetime(minute: 5 + Rational(1,2)) }.should.raise(ArgumentError)
end
it "raises an error, when the second is smaller than -60" do
- -> { new_datetime(second: -61) }.should raise_error(ArgumentError)
+ -> { new_datetime(second: -61) }.should.raise(ArgumentError)
end
it "raises an error, when the second is greater or equal than 60" do
- -> { new_datetime(second: 60) }.should raise_error(ArgumentError)
+ -> { new_datetime(second: 60) }.should.raise(ArgumentError)
end
it "raises an error for second fractions smaller than -60" do
diff --git a/spec/ruby/library/datetime/strftime_spec.rb b/spec/ruby/library/datetime/strftime_spec.rb
index 1c925f92eb..a07cc9c1aa 100644
--- a/spec/ruby/library/datetime/strftime_spec.rb
+++ b/spec/ruby/library/datetime/strftime_spec.rb
@@ -1,6 +1,8 @@
+require_relative '../../spec_helper'
require 'date'
require_relative '../../shared/time/strftime_for_date'
require_relative '../../shared/time/strftime_for_time'
+date_version = defined?(Date::VERSION) ? Date::VERSION : '3.1.0'
describe "DateTime#strftime" do
before :all do
@@ -31,10 +33,9 @@ describe "DateTime#strftime" do
@time.strftime("%Z").should == "+00:00"
end
- # %v is %e-%b-%Y for Date/DateTime
it "should be able to show the commercial week" do
- @time.strftime("%v").should == " 3-Feb-2001"
- @time.strftime("%v").should == @time.strftime('%e-%b-%Y')
+ @time.strftime("%v").should == " 3-FEB-2001"
+ @time.strftime("%v").should != @time.strftime('%e-%b-%Y')
end
# additional conversion specifiers only in Date/DateTime
diff --git a/spec/ruby/library/datetime/time/to_datetime_spec.rb b/spec/ruby/library/datetime/time/to_datetime_spec.rb
new file mode 100644
index 0000000000..5589725238
--- /dev/null
+++ b/spec/ruby/library/datetime/time/to_datetime_spec.rb
@@ -0,0 +1,40 @@
+require_relative '../../../spec_helper'
+require 'time'
+require 'date'
+date_version = defined?(Date::VERSION) ? Date::VERSION : '3.1.0'
+
+describe "Time#to_datetime" do
+ it "returns a DateTime representing the same instant" do
+ time = Time.utc(2012, 12, 31, 23, 58, 59)
+ datetime = time.to_datetime
+ datetime.year.should == 2012
+ datetime.month.should == 12
+ datetime.day.should == 31
+ datetime.hour.should == 23
+ datetime.min.should == 58
+ datetime.sec.should == 59
+ end
+
+ it "returns a DateTime representing the same instant before Gregorian" do
+ time = Time.utc(1582, 10, 14, 23, 58, 59)
+ datetime = time.to_datetime
+ datetime.year.should == 1582
+ datetime.month.should == 10
+ datetime.day.should == 4
+ datetime.hour.should == 23
+ datetime.min.should == 58
+ datetime.sec.should == 59
+ end
+
+ it "roundtrips" do
+ time = Time.utc(3, 12, 31, 23, 58, 59)
+ datetime = time.to_datetime
+ datetime.to_time.utc.should == time
+ end
+
+ it "yields a DateTime with the default Calendar reform day" do
+ Time.utc(1582, 10, 4, 1, 2, 3).to_datetime.start.should == Date::ITALY
+ Time.utc(1582, 10, 14, 1, 2, 3).to_datetime.start.should == Date::ITALY
+ Time.utc(1582, 10, 15, 1, 2, 3).to_datetime.start.should == Date::ITALY
+ end
+end
diff --git a/spec/ruby/library/datetime/to_date_spec.rb b/spec/ruby/library/datetime/to_date_spec.rb
index 48c05e7fed..31ccf5ae98 100644
--- a/spec/ruby/library/datetime/to_date_spec.rb
+++ b/spec/ruby/library/datetime/to_date_spec.rb
@@ -4,7 +4,7 @@ require 'date'
describe "DateTime#to_date" do
it "returns an instance of Date" do
dt = DateTime.new(2012, 12, 24, 12, 23, 00, '+05:00')
- dt.to_date.should be_kind_of(Date)
+ dt.to_date.should.is_a?(Date)
end
it "maintains the same year" do
diff --git a/spec/ruby/library/datetime/to_s_spec.rb b/spec/ruby/library/datetime/to_s_spec.rb
index 175fb807f4..ed0746f42b 100644
--- a/spec/ruby/library/datetime/to_s_spec.rb
+++ b/spec/ruby/library/datetime/to_s_spec.rb
@@ -4,7 +4,7 @@ require 'date'
describe "DateTime#to_s" do
it "returns a new String object" do
dt = DateTime.new(2012, 12, 24, 1, 2, 3, "+03:00")
- dt.to_s.should be_kind_of(String)
+ dt.to_s.should.is_a?(String)
end
it "maintains timezone regardless of local time" do
diff --git a/spec/ruby/library/datetime/to_time_spec.rb b/spec/ruby/library/datetime/to_time_spec.rb
index a11b6e30e1..a3ffc019fb 100644
--- a/spec/ruby/library/datetime/to_time_spec.rb
+++ b/spec/ruby/library/datetime/to_time_spec.rb
@@ -1,16 +1,17 @@
require_relative '../../spec_helper'
require 'date'
+date_version = defined?(Date::VERSION) ? Date::VERSION : '3.1.0'
describe "DateTime#to_time" do
it "yields a new Time object" do
- DateTime.now.to_time.should be_kind_of(Time)
+ DateTime.now.to_time.should.is_a?(Time)
end
it "returns a Time representing the same instant" do
- datetime = DateTime.civil(3, 12, 31, 23, 58, 59)
+ datetime = DateTime.civil(2012, 12, 31, 23, 58, 59)
time = datetime.to_time.utc
- time.year.should == 3
+ time.year.should == 2012
time.month.should == 12
time.day.should == 31
time.hour.should == 23
@@ -18,6 +19,17 @@ describe "DateTime#to_time" do
time.sec.should == 59
end
+ it "returns a Time representing the same instant before Gregorian" do
+ datetime = DateTime.civil(1582, 10, 4, 23, 58, 59)
+ time = datetime.to_time.utc
+ time.year.should == 1582
+ time.month.should == 10
+ time.day.should == 14
+ time.hour.should == 23
+ time.min.should == 58
+ time.sec.should == 59
+ end
+
it "preserves the same time regardless of local time or zone" do
date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00')
diff --git a/spec/ruby/library/datetime/yday_spec.rb b/spec/ruby/library/datetime/yday_spec.rb
new file mode 100644
index 0000000000..08a72c6480
--- /dev/null
+++ b/spec/ruby/library/datetime/yday_spec.rb
@@ -0,0 +1,7 @@
+require_relative '../../spec_helper'
+require_relative '../../shared/time/yday'
+require 'date'
+
+describe "DateTime#yday" do
+ it_behaves_like :time_yday, -> year, month, day { DateTime.new(year, month, day).yday }
+end