diff options
Diffstat (limited to 'spec/ruby/optional/capi/time_spec.rb')
| -rw-r--r-- | spec/ruby/optional/capi/time_spec.rb | 223 |
1 files changed, 111 insertions, 112 deletions
diff --git a/spec/ruby/optional/capi/time_spec.rb b/spec/ruby/optional/capi/time_spec.rb index c8f0d9d4e0..dc0b09376e 100644 --- a/spec/ruby/optional/capi/time_spec.rb +++ b/spec/ruby/optional/capi/time_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../spec_helper', __FILE__) +require_relative 'spec_helper' load_extension("time") @@ -16,7 +16,7 @@ describe "CApiTimeSpecs" do describe "TIMET2NUM" do it "returns an Integer" do - @s.TIMET2NUM.should be_kind_of(Integer) + @s.TIMET2NUM.should.is_a?(Integer) end end @@ -32,7 +32,7 @@ describe "CApiTimeSpecs" do it "creates a Time in the local zone with only a timestamp" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(1232141421, nil) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1232141421 platform_is_not :windows do time.gmt_offset.should == 3600 @@ -43,7 +43,7 @@ describe "CApiTimeSpecs" do it "creates a Time with the given offset" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(1232141421, 7200) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1232141421 time.gmt_offset.should == 7200 end @@ -52,7 +52,7 @@ describe "CApiTimeSpecs" do it "creates a Time with a Float timestamp" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(1.5, 7200) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1 time.nsec.should == 500000000 time.gmt_offset.should == 7200 @@ -62,7 +62,7 @@ describe "CApiTimeSpecs" do it "creates a Time with a Rational timestamp" do with_timezone("Europe/Amsterdam") do time = @s.rb_time_num_new(Rational(3, 2), 7200) - time.should be_an_instance_of(Time) + time.should.instance_of?(Time) time.to_i.should == 1 time.nsec.should == 500000000 time.gmt_offset.should == 7200 @@ -73,32 +73,32 @@ describe "CApiTimeSpecs" do describe "rb_time_interval" do it "creates a timeval interval for a Fixnum" do sec, usec = @s.rb_time_interval(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 0 end it "creates a timeval interval for a Float" do sec, usec = @s.rb_time_interval(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval interval for a Rational" do sec, usec = @s.rb_time_interval(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "throws an argument error for a negative value" do - lambda { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) - lambda { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) - lambda { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) + -> { @s.rb_time_interval(-1232141421) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(Rational(-3, 2)) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(-1.5) }.should.raise(ArgumentError) end end @@ -106,36 +106,36 @@ describe "CApiTimeSpecs" do describe "rb_time_interval" do it "creates a timeval interval for a Fixnum" do sec, usec = @s.rb_time_interval(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 0 end it "creates a timeval interval for a Float" do sec, usec = @s.rb_time_interval(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval interval for a Rational" do sec, usec = @s.rb_time_interval(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "throws an argument error for a negative value" do - lambda { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) - lambda { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) - lambda { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) + -> { @s.rb_time_interval(-1232141421) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(Rational(-3, 2)) }.should.raise(ArgumentError) + -> { @s.rb_time_interval(-1.5) }.should.raise(ArgumentError) end it "throws an argument error when given a Time instance" do - lambda { @s.rb_time_interval(Time.now) }.should raise_error(TypeError) + -> { @s.rb_time_interval(Time.now) }.should.raise(TypeError) end end @@ -143,52 +143,50 @@ describe "CApiTimeSpecs" do describe "rb_time_timeval" do it "creates a timeval for a Fixnum" do sec, usec = @s.rb_time_timeval(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 0 end it "creates a timeval for a Float" do sec, usec = @s.rb_time_timeval(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end it "creates a timeval for a Rational" do sec, usec = @s.rb_time_timeval(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - usec.should be_kind_of(Integer) + usec.should.is_a?(Integer) usec.should == 500000 end - platform_is_not :mingw32 do - it "creates a timeval for a negative Fixnum" do - sec, usec = @s.rb_time_timeval(-1232141421) - sec.should be_kind_of(Integer) - sec.should == -1232141421 - usec.should be_kind_of(Integer) - usec.should == 0 - end + it "creates a timeval for a negative Fixnum" do + sec, usec = @s.rb_time_timeval(-1232141421) + sec.should.is_a?(Integer) + sec.should == -1232141421 + usec.should.is_a?(Integer) + usec.should == 0 + end - it "creates a timeval for a negative Float" do - sec, usec = @s.rb_time_timeval(-1.5) - sec.should be_kind_of(Integer) - sec.should == -2 - usec.should be_kind_of(Integer) - usec.should == 500000 - end + it "creates a timeval for a negative Float" do + sec, usec = @s.rb_time_timeval(-1.5) + sec.should.is_a?(Integer) + sec.should == -2 + usec.should.is_a?(Integer) + usec.should == 500000 + end - it "creates a timeval for a negative Rational" do - sec, usec = @s.rb_time_timeval(Rational(-3, 2)) - sec.should be_kind_of(Integer) - sec.should == -2 - usec.should be_kind_of(Integer) - usec.should == 500000 - end + it "creates a timeval for a negative Rational" do + sec, usec = @s.rb_time_timeval(Rational(-3, 2)) + sec.should.is_a?(Integer) + sec.should == -2 + usec.should.is_a?(Integer) + usec.should == 500000 end it "creates a timeval from a Time object" do @@ -202,52 +200,50 @@ describe "CApiTimeSpecs" do describe "rb_time_timespec" do it "creates a timespec for a Fixnum" do sec, nsec = @s.rb_time_timespec(1232141421) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1232141421 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 0 end it "creates a timespec for a Float" do sec, nsec = @s.rb_time_timespec(1.5) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 500000000 end it "creates a timespec for a Rational" do sec, nsec = @s.rb_time_timespec(Rational(3, 2)) - sec.should be_kind_of(Integer) + sec.should.is_a?(Integer) sec.should == 1 - nsec.should be_kind_of(Integer) + nsec.should.is_a?(Integer) nsec.should == 500000000 end - platform_is_not :mingw32 do - it "creates a timespec for a negative Fixnum" do - sec, nsec = @s.rb_time_timespec(-1232141421) - sec.should be_kind_of(Integer) - sec.should == -1232141421 - nsec.should be_kind_of(Integer) - nsec.should == 0 - end + it "creates a timespec for a negative Fixnum" do + sec, nsec = @s.rb_time_timespec(-1232141421) + sec.should.is_a?(Integer) + sec.should == -1232141421 + nsec.should.is_a?(Integer) + nsec.should == 0 + end - it "creates a timespec for a negative Float" do - sec, nsec = @s.rb_time_timespec(-1.5) - sec.should be_kind_of(Integer) - sec.should == -2 - nsec.should be_kind_of(Integer) - nsec.should == 500000000 - end + it "creates a timespec for a negative Float" do + sec, nsec = @s.rb_time_timespec(-1.5) + sec.should.is_a?(Integer) + sec.should == -2 + nsec.should.is_a?(Integer) + nsec.should == 500000000 + end - it "creates a timespec for a negative Rational" do - sec, nsec = @s.rb_time_timespec(Rational(-3, 2)) - sec.should be_kind_of(Integer) - sec.should == -2 - nsec.should be_kind_of(Integer) - nsec.should == 500000000 - end + it "creates a timespec for a negative Rational" do + sec, nsec = @s.rb_time_timespec(Rational(-3, 2)) + sec.should.is_a?(Integer) + sec.should == -2 + nsec.should.is_a?(Integer) + nsec.should == 500000000 end it "creates a timespec from a Time object" do @@ -258,45 +254,48 @@ describe "CApiTimeSpecs" do end end - ruby_version_is "2.3" do - describe "rb_time_timespec_new" do - it "returns a time object with the given timespec and UTC offset" do - @s.rb_time_timespec_new(1447087832, 476451125, 32400).should == Time.at(1447087832, 476451.125).localtime(32400) - end - - describe "when offset given is within range of -86400 and 86400 (exclusive)" do - it "sets time's is_gmt to false" do - @s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should be_false - end + describe "rb_time_timespec_new" do + it "returns a time object with the given timespec and UTC offset" do + @s.rb_time_timespec_new(1447087832, 476451125, 32400).should == Time.at(1447087832, 476451.125).localtime(32400) + end - it "sets time's offset to the offset given" do - @s.rb_time_timespec_new(1447087832, 476451125, 86399).gmtoff.should == 86399 - end + describe "when offset given is within range of -86400 and 86400 (exclusive)" do + it "sets time's is_gmt to false" do + @s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should == false end - it "returns time object in UTC if offset given equals INT_MAX - 1" do - @s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should be_true + it "sets time's offset to the offset given" do + @s.rb_time_timespec_new(1447087832, 476451125, 86399).gmtoff.should == 86399 end + end - it "returns time object in localtime if offset given equals INT_MAX" do - @s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).should == Time.at(1447087832, 476451.125).localtime - t = Time.now - @s.rb_time_timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff.should == t.gmtoff - end + it "returns time object in UTC if offset given equals INT_MAX - 1" do + @s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should == true + end - it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do - lambda { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError) - lambda { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError) - end + it "returns time object in localtime if offset given equals INT_MAX" do + @s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).should == Time.at(1447087832, 476451.125).localtime + t = Time.now + @s.rb_time_timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff.should == t.gmtoff end - describe "rb_timespec_now" do - it "fills a struct timespec with the current time" do - now = Time.now - time = @s.rb_time_from_timespec(now.utc_offset) - time.should be_an_instance_of(Time) - (time - now).should be_close(0, 10) - end + it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do + -> { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should.raise(ArgumentError) + -> { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should.raise(ArgumentError) + end + + it "doesn't call Time.at directly" do + Time.should_not_receive(:at) + @s.rb_time_timespec_new(1447087832, 476451125, 32400).should.is_a?(Time) + end + end + + describe "rb_timespec_now" do + it "fills a struct timespec with the current time" do + now = Time.now + time = @s.rb_time_from_timespec(now.utc_offset) + time.should.instance_of?(Time) + (time - now).should be_close(0, TIME_TOLERANCE) end end end |
