From a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 Mon Sep 17 00:00:00 2001 From: eregon Date: Sun, 7 May 2017 12:04:49 +0000 Subject: Add in-tree mspec and ruby/spec * For easier modifications of ruby/spec by MRI developers. * .gitignore: track changes under spec. * spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec. These files can therefore be updated like any other file in MRI. Instructions are provided in spec/README. [Feature #13156] [ruby-core:79246] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/rubyspec/core/time/new_spec.rb | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 spec/rubyspec/core/time/new_spec.rb (limited to 'spec/rubyspec/core/time/new_spec.rb') diff --git a/spec/rubyspec/core/time/new_spec.rb b/spec/rubyspec/core/time/new_spec.rb new file mode 100644 index 0000000000..a92715c81c --- /dev/null +++ b/spec/rubyspec/core/time/new_spec.rb @@ -0,0 +1,99 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../shared/now', __FILE__) +require File.expand_path('../shared/local', __FILE__) +require File.expand_path('../shared/time_params', __FILE__) + +describe "Time.new" do + it_behaves_like(:time_now, :new) +end + +describe "Time.new" do + it_behaves_like(:time_local, :new) + it_behaves_like(:time_params, :new) +end + +describe "Time.new with a utc_offset argument" do + it "returns a non-UTC time" do + Time.new(2000, 1, 1, 0, 0, 0, 0).utc?.should == false + end + + it "returns a Time with a UTC offset of the specified number of Integer seconds" do + Time.new(2000, 1, 1, 0, 0, 0, 123).utc_offset.should == 123 + end + + describe "with an argument that responds to #to_int" do + it "coerces using #to_int" do + o = mock('integer') + o.should_receive(:to_int).and_return(123) + Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should == 123 + end + end + + it "returns a Time with a UTC offset of the specified number of Rational seconds" do + Time.new(2000, 1, 1, 0, 0, 0, Rational(5, 2)).utc_offset.should eql(Rational(5, 2)) + end + + describe "with an argument that responds to #to_r" do + it "coerces using #to_r" do + o = mock_numeric('rational') + o.should_receive(:to_r).and_return(Rational(5, 2)) + Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should eql(Rational(5, 2)) + end + end + + it "returns a Time with a UTC offset specified as +HH:MM" do + Time.new(2000, 1, 1, 0, 0, 0, "+05:30").utc_offset.should == 19800 + end + + it "returns a Time with a UTC offset specified as -HH:MM" do + Time.new(2000, 1, 1, 0, 0, 0, "-04:10").utc_offset.should == -15000 + end + + describe "with an argument that responds to #to_str" do + it "coerces using #to_str" do + o = mock('string') + o.should_receive(:to_str).and_return("+05:30") + Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should == 19800 + end + end + + it "returns a local Time if the argument is nil" do + with_timezone("PST", -8) do + t = Time.new(2000, 1, 1, 0, 0, 0, nil) + t.utc_offset.should == -28800 + t.zone.should == "PST" + end + end + + # [Bug #8679], r47676 + it "disallows a value for minutes greater than 59" do + lambda { + Time.new(2000, 1, 1, 0, 0, 0, "+01:60") + }.should raise_error(ArgumentError) + lambda { + Time.new(2000, 1, 1, 0, 0, 0, "+01:99") + }.should raise_error(ArgumentError) + end + + it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do + lambda { Time.new(2000, 1, 1, 0, 0, 0, "3600") }.should raise_error(ArgumentError) + end + + it "raises ArgumentError if the hour value is greater than 23" do + lambda { Time.new(2000, 1, 1, 0, 0, 0, "+24:00") }.should raise_error(ArgumentError) + end + + it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do + lambda { Time.new(2000, 1, 1, 0, 0, 0, "-04:10".encode("UTF-16LE")) }.should raise_error(ArgumentError) + end + + it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do + Time.new(2000, 1, 1, 0, 0, 0, -86400 + 1).utc_offset.should == (-86400 + 1) + lambda { Time.new(2000, 1, 1, 0, 0, 0, -86400) }.should raise_error(ArgumentError) + end + + it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do + Time.new(2000, 1, 1, 0, 0, 0, 86400 - 1).utc_offset.should == (86400 - 1) + lambda { Time.new(2000, 1, 1, 0, 0, 0, 86400) }.should raise_error(ArgumentError) + end +end -- cgit v1.2.3