diff options
Diffstat (limited to 'spec/ruby/core/time/at_spec.rb')
| -rw-r--r-- | spec/ruby/core/time/at_spec.rb | 244 |
1 files changed, 153 insertions, 91 deletions
diff --git a/spec/ruby/core/time/at_spec.rb b/spec/ruby/core/time/at_spec.rb index 368eeb7fca..10d4d36a68 100644 --- a/spec/ruby/core/time/at_spec.rb +++ b/spec/ruby/core/time/at_spec.rb @@ -14,13 +14,13 @@ describe "Time.at" do end it "returns a non-UTC Time" do - Time.at(1184027924).utc?.should == false + Time.at(1184027924).should_not.utc? end it "returns a subclass instance on a Time subclass" do c = Class.new(Time) t = c.at(0) - t.should be_an_instance_of(c) + t.should.instance_of?(c) end it "roundtrips a Rational produced by #to_r" do @@ -32,10 +32,17 @@ describe "Time.at" do t2.nsec.should == t.nsec end - describe "passed BigDecimal" do - it "doesn't round input value" do - require 'bigdecimal' - Time.at(BigDecimal('1.1')).to_f.should == 1.1 + describe "passed Rational" do + it "returns Time with correct microseconds" do + t = Time.at(Rational(1_486_570_508_539_759, 1_000_000)) + t.usec.should == 539_759 + t.nsec.should == 539_759_000 + end + + it "returns Time with correct nanoseconds" do + t = Time.at(Rational(1_486_570_508_539_759_123, 1_000_000_000)) + t.usec.should == 539_759 + t.nsec.should == 539_759_123 end end end @@ -49,33 +56,33 @@ describe "Time.at" do it "creates a dup time object with the value given by time" do t1 = Time.new t2 = Time.at(t1) - t1.should_not equal t2 + t1.should_not.equal? t2 end it "returns a UTC time if the argument is UTC" do t = Time.now.getgm - Time.at(t).utc?.should == true + Time.at(t).should.utc? end it "returns a non-UTC time if the argument is non-UTC" do t = Time.now - Time.at(t).utc?.should == false + Time.at(t).should_not.utc? end it "returns a subclass instance" do c = Class.new(Time) t = c.at(Time.now) - t.should be_an_instance_of(c) + t.should.instance_of?(c) end end describe "passed non-Time, non-Numeric" do it "raises a TypeError with a String argument" do - -> { Time.at("0") }.should raise_error(TypeError) + -> { Time.at("0") }.should.raise(TypeError) end it "raises a TypeError with a nil argument" do - -> { Time.at(nil) }.should raise_error(TypeError) + -> { Time.at(nil) }.should.raise(TypeError) end describe "with an argument that responds to #to_int" do @@ -92,6 +99,12 @@ describe "Time.at" do o.should_receive(:to_r).and_return(Rational(5, 2)) Time.at(o).should == Time.at(Rational(5, 2)) end + + it "needs for the argument to respond to #to_int too" do + o = mock('rational-but-no-to_int') + def o.to_r; Rational(5, 2) end + -> { Time.at(o) }.should.raise(TypeError, "can't convert MockObject into an exact number") + end end end @@ -127,127 +140,176 @@ describe "Time.at" do describe "passed [Integer, nil]" do it "raises a TypeError" do - -> { Time.at(0, nil) }.should raise_error(TypeError) + -> { Time.at(0, nil) }.should.raise(TypeError) end end describe "passed [Integer, String]" do it "raises a TypeError" do - -> { Time.at(0, "0") }.should raise_error(TypeError) + -> { Time.at(0, "0") }.should.raise(TypeError) end end describe "passed [Time, Integer]" do # #8173 it "raises a TypeError" do - -> { Time.at(Time.now, 500000) }.should raise_error(TypeError) + -> { Time.at(Time.now, 500000) }.should.raise(TypeError) end end - ruby_version_is "2.5" do - describe "passed [Time, Numeric, format]" do - context ":nanosecond format" do - it "treats second argument as nanoseconds" do - Time.at(0, 123456789, :nanosecond).nsec.should == 123456789 - end + describe "passed [Time, Numeric, format]" do + context ":nanosecond format" do + it "treats second argument as nanoseconds" do + Time.at(0, 123456789, :nanosecond).nsec.should == 123456789 end + end - context ":nsec format" do - it "treats second argument as nanoseconds" do - Time.at(0, 123456789, :nsec).nsec.should == 123456789 - end + context ":nsec format" do + it "treats second argument as nanoseconds" do + Time.at(0, 123456789, :nsec).nsec.should == 123456789 end + end - context ":microsecond format" do - it "treats second argument as microseconds" do - Time.at(0, 123456, :microsecond).nsec.should == 123456000 - end + context ":microsecond format" do + it "treats second argument as microseconds" do + Time.at(0, 123456, :microsecond).nsec.should == 123456000 end + end - context ":usec format" do - it "treats second argument as microseconds" do - Time.at(0, 123456, :usec).nsec.should == 123456000 - end + context ":usec format" do + it "treats second argument as microseconds" do + Time.at(0, 123456, :usec).nsec.should == 123456000 end + end - context ":millisecond format" do - it "treats second argument as milliseconds" do - Time.at(0, 123, :millisecond).nsec.should == 123000000 - end + context ":millisecond format" do + it "treats second argument as milliseconds" do + Time.at(0, 123, :millisecond).nsec.should == 123000000 end + end - context "not supported format" do - it "raises ArgumentError" do - -> { Time.at(0, 123456, 2) }.should raise_error(ArgumentError) - -> { Time.at(0, 123456, nil) }.should raise_error(ArgumentError) - -> { Time.at(0, 123456, :invalid) }.should raise_error(ArgumentError) - end - - it "does not try to convert format to Symbol with #to_sym" do - format = "usec" - format.should_not_receive(:to_sym) - -> { Time.at(0, 123456, format) }.should raise_error(ArgumentError) - end + context "not supported format" do + it "raises ArgumentError" do + -> { Time.at(0, 123456, 2) }.should.raise(ArgumentError) + -> { Time.at(0, 123456, nil) }.should.raise(ArgumentError) + -> { Time.at(0, 123456, :invalid) }.should.raise(ArgumentError) end - it "supports Float second argument" do - Time.at(0, 123456789.500, :nanosecond).nsec.should == 123456789 - Time.at(0, 123456789.500, :nsec).nsec.should == 123456789 - Time.at(0, 123456.500, :microsecond).nsec.should == 123456500 - Time.at(0, 123456.500, :usec).nsec.should == 123456500 - Time.at(0, 123.500, :millisecond).nsec.should == 123500000 + it "does not try to convert format to Symbol with #to_sym" do + format = +"usec" + format.should_not_receive(:to_sym) + -> { Time.at(0, 123456, format) }.should.raise(ArgumentError) end end + + it "supports Float second argument" do + Time.at(0, 123456789.500, :nanosecond).nsec.should == 123456789 + Time.at(0, 123456789.500, :nsec).nsec.should == 123456789 + Time.at(0, 123456.500, :microsecond).nsec.should == 123456500 + Time.at(0, 123456.500, :usec).nsec.should == 123456500 + Time.at(0, 123.500, :millisecond).nsec.should == 123500000 + end end - ruby_version_is "2.6" do - describe ":in keyword argument" do - before do - @epoch_time = Time.now.to_i - end + describe ":in keyword argument" do + before do + @epoch_time = Time.now.to_i + end - it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do - time = Time.at(@epoch_time, in: "+05:00") + it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do + time = Time.at(@epoch_time, in: "+05:00") - time.utc_offset.should == 5*60*60 - time.zone.should == nil - time.to_i.should == @epoch_time + time.utc_offset.should == 5*60*60 + time.zone.should == nil + time.to_i.should == @epoch_time - time = Time.at(@epoch_time, in: "-09:00") + time = Time.at(@epoch_time, in: "-09:00") - time.utc_offset.should == -9*60*60 - time.zone.should == nil - time.to_i.should == @epoch_time - end + time.utc_offset.should == -9*60*60 + time.zone.should == nil + time.to_i.should == @epoch_time - it "could be UTC offset as a number of seconds" do - time = Time.at(@epoch_time, in: 5*60*60) + time = Time.at(@epoch_time, in: "-09:00:01") - time.utc_offset.should == 5*60*60 - time.zone.should == nil - time.to_i.should == @epoch_time + time.utc_offset.should == -(9*60*60 + 1) + time.zone.should == nil + time.to_i.should == @epoch_time + end - time = Time.at(@epoch_time, in: -9*60*60) + it "could be UTC offset as a number of seconds" do + time = Time.at(@epoch_time, in: 5*60*60) - time.utc_offset.should == -9*60*60 - time.zone.should == nil - time.to_i.should == @epoch_time - end + time.utc_offset.should == 5*60*60 + time.zone.should == nil + time.to_i.should == @epoch_time + + time = Time.at(@epoch_time, in: -9*60*60) + + time.utc_offset.should == -9*60*60 + time.zone.should == nil + time.to_i.should == @epoch_time + end + + it "could be UTC offset as a 'UTC' String" do + time = Time.at(@epoch_time, in: "UTC") + + time.utc_offset.should == 0 + time.zone.should == "UTC" + time.to_i.should == @epoch_time + end + + it "could be UTC offset as a military zone A-Z" do + time = Time.at(@epoch_time, in: "B") + + time.utc_offset.should == 3600 * 2 + time.zone.should == nil + time.to_i.should == @epoch_time + end + + it "could be a timezone object" do + zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo") + time = Time.at(@epoch_time, in: zone) + + time.utc_offset.should == 5*3600+30*60 + time.zone.should == zone + time.to_i.should == @epoch_time - it "could be a timezone object" do - zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo") - time = Time.at(@epoch_time, in: zone) + zone = TimeSpecs::TimezoneWithName.new(name: "PST") + time = Time.at(@epoch_time, in: zone) - time.utc_offset.should == 5*3600+30*60 - time.zone.should == zone - time.to_i.should == @epoch_time + time.utc_offset.should == -9*60*60 + 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(ArgumentError) + -> { Time.at(@epoch_time, in: "ABC") }.should.raise(ArgumentError) + end + + it "raises ArgumentError if hours greater than 23" do # TODO + -> { Time.at(@epoch_time, in: "+24:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.at(@epoch_time, in: "+2400") }.should.raise(ArgumentError, "utc_offset out of range") + + -> { Time.at(@epoch_time, in: "+99:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.at(@epoch_time, in: "+9900") }.should.raise(ArgumentError, "utc_offset out of range") + end + + it "raises ArgumentError if minutes greater than 59" do # TODO + -> { Time.at(@epoch_time, in: "+00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') + -> { Time.at(@epoch_time, in: "+0060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') + + -> { Time.at(@epoch_time, in: "+00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') + -> { Time.at(@epoch_time, in: "+0099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') + end - zone = TimeSpecs::TimezoneWithName.new(name: "PST") - time = Time.at(@epoch_time, in: zone) + ruby_bug '#20797', ''...'3.4' do + it "raises ArgumentError if seconds greater than 59" do + -> { Time.at(@epoch_time, in: "+00:00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') + -> { Time.at(@epoch_time, in: "+000060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') - time.utc_offset.should == -9*60*60 - time.zone.should == zone - time.to_i.should == @epoch_time + -> { Time.at(@epoch_time, in: "+00:00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') + -> { Time.at(@epoch_time, in: "+000099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') end end end |
