Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby 3.1: Add specs for Time#new in keyword #956

Merged
merged 3 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/time/at_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,10 @@
time.zone.should == zone
time.to_i.should == @epoch_time
end

it "raises ArgumentError if format is invalid" do
-> { Time.at(@epoch_time, in: "+09:99") }.should raise_error(ArgumentError)
-> { Time.at(@epoch_time, in: "ABC") }.should raise_error(ArgumentError)
end
end
end
51 changes: 51 additions & 0 deletions core/time/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,55 @@ def zone.local_to_utc(t)
end
end
end

ruby_version_is '3.1' do # https://bugs.ruby-lang.org/issues/17485
describe ":in keyword argument" do
it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do
time = Time.new(2000, 1, 1, 12, 0, 0, in: "+05:00")

time.utc_offset.should == 5*60*60
time.zone.should == nil

time = Time.new(2000, 1, 1, 12, 0, 0, in: "-09:00")

time.utc_offset.should == -9*60*60
time.zone.should == nil
end

it "could be UTC offset as a number of seconds" do
time = Time.new(2000, 1, 1, 12, 0, 0, in: 5*60*60)

time.utc_offset.should == 5*60*60
time.zone.should == nil

time = Time.new(2000, 1, 1, 12, 0, 0, in: -9*60*60)

time.utc_offset.should == -9*60*60
time.zone.should == nil
end

it "could be a timezone object" do
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
time = Time.new(2000, 1, 1, 12, 0, 0, in: zone)

time.utc_offset.should == 5*3600+30*60
time.zone.should == zone

zone = TimeSpecs::TimezoneWithName.new(name: "PST")
time = Time.new(2000, 1, 1, 12, 0, 0, in: zone)

time.utc_offset.should == -9*60*60
time.zone.should == zone
end

it "raises ArgumentError if format is invalid" do
-> { Time.new(2000, 1, 1, 12, 0, 0, in: "+09:99") }.should raise_error(ArgumentError)
-> { Time.new(2000, 1, 1, 12, 0, 0, in: "ABC") }.should raise_error(ArgumentError)
end

it "raises ArgumentError if two offset arguments are given" do
-> { Time.new(2000, 1, 1, 12, 0, 0, "+05:00", in: "+05:00") }.should raise_error(ArgumentError)
end
end
end
end
45 changes: 45 additions & 0 deletions core/time/now_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,49 @@

describe "Time.now" do
it_behaves_like :time_now, :now

describe ":in keyword argument" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I first thought those spec could be put into core/time/shared/now.rb as shared examples. But the in keyword behavior differs between Time.now and Time.new by Ruby version, so I just wrote these specs without ruby_version_is here.

it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do
time = Time.now(in: "+05:00")

time.utc_offset.should == 5*60*60
time.zone.should == nil

time = Time.now(in: "-09:00")

time.utc_offset.should == -9*60*60
time.zone.should == nil
end

it "could be UTC offset as a number of seconds" do
time = Time.now(in: 5*60*60)

time.utc_offset.should == 5*60*60
time.zone.should == nil

time = Time.now(in: -9*60*60)

time.utc_offset.should == -9*60*60
time.zone.should == nil
end

it "could be a timezone object" do
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
time = Time.now(in: zone)

time.utc_offset.should == 5*3600+30*60
time.zone.should == zone

zone = TimeSpecs::TimezoneWithName.new(name: "PST")
time = Time.now(in: zone)

time.utc_offset.should == -9*60*60
time.zone.should == zone
end

it "raises ArgumentError if format is invalid" do
-> { Time.now(in: "+09:99") }.should raise_error(ArgumentError)
-> { Time.now(in: "ABC") }.should raise_error(ArgumentError)
end
end
end