spec/timeliness/parser_spec.rb in timeliness-0.4.0 vs spec/timeliness/parser_spec.rb in timeliness-0.4.1

- old
+ new

@@ -1,6 +1,21 @@ describe Timeliness::Parser do + around(:all) do |example| + current_default = Timeliness.default_timezone + current_zone = Time.zone + example.call + Time.zone = current_zone + Timeliness.default_timezone = current_default + end + + def self.timezone_settings(zone: nil, output: nil) + before do + Time.zone = zone if zone + Timeliness.default_timezone = output if output + end + end + before(:all) do Timecop.freeze(2010,1,1,0,0,0) end describe "parse" do @@ -68,14 +83,11 @@ should_not_parse("2000-02-01T25:13:14+02:00") end context "string with zone offset value" do context "when current timezone is earler than string zone" do - before(:all) do - Timeliness.default_timezone = :current - Time.zone = 'Australia/Melbourne' - end + timezone_settings zone: 'Australia/Melbourne', output: :current it 'should return value shifted by positive offset in default timezone' do value = parse("2000-06-01T12:00:00+02:00") expect(value).to eq Time.zone.local(2000,6,1,20,0,0) expect(value.utc_offset).to eq 10.hours @@ -84,22 +96,14 @@ it 'should return value shifted by negative offset in default timezone' do value = parse("2000-06-01T12:00:00-01:00") expect(value).to eq Time.zone.local(2000,6,1,23,0,0) expect(value.utc_offset).to eq 10.hours end - - after(:all) do - Time.zone = nil - Timeliness.default_timezone = :local - end end context "when current timezone is later than string zone" do - before(:all) do - Timeliness.default_timezone = :current - Time.zone = 'America/Phoenix' - end + timezone_settings zone: 'America/Phoenix', output: :current it 'should return value shifted by positive offset in default timezone' do value = parse("2000-06-01T12:00:00+02:00") expect(value).to eq Time.zone.local(2000,6,1,3,0,0) expect(value.utc_offset).to eq -7.hours @@ -108,22 +112,15 @@ it 'should return value shifted by negative offset in default timezone' do value = parse("2000-06-01T12:00:00-01:00") expect(value).to eq Time.zone.local(2000,6,1,6,0,0) expect(value.utc_offset).to eq -7.hours end - - after(:all) do - Time.zone = nil - Timeliness.default_timezone = :local - end end end context "string with zone abbreviation" do - before(:all) do - Time.zone = 'Australia/Melbourne' - end + timezone_settings zone: 'Australia/Melbourne' it 'should return value using string zone adjusted to default :local timezone' do Timeliness.default_timezone = :local value = parse("Thu, 01 Jun 2000 03:00:00 MST") expect(value).to eq Time.utc(2000,6,1,10,0,0).getlocal @@ -142,13 +139,21 @@ Timeliness.default_timezone = :local value = parse("Thu, 01 Jun 2000 03:00:00 MST", :zone => 'Perth') expect(value).to eq Time.use_zone('Perth') { Time.zone.local(2000,6,1,18,0,0) } expect(value.utc_offset).to eq 8.hours end + end - after(:all) do - Time.zone = nil + context "string with zulu time abbreviation 'Z'" do + timezone_settings zone: 'Australia/Melbourne' + + it 'should return value using string zone adjusted to default :current timezone' do + Timeliness.default_timezone = :current + + value = parse("2000-06-01T12:00:00Z") + expect(value).to eq Time.zone.local(2000,6,1,22,0,0) + expect(value.utc_offset).to eq 10.hours end end context "with :datetime type" do it "should return time object for valid datetime string" do @@ -224,12 +229,13 @@ expect(parse("2000-06-01 12:60", :datetime, :zone => :local)).to be_nil end end context ":current" do + timezone_settings zone: 'Adelaide' + it "should return time object in current timezone" do - Time.zone = 'Adelaide' time = parse("2000-06-01 12:13:14", :datetime, :zone => :current) expect(time.utc_offset).to eq 9.5.hours end it 'should return nil for partial invalid time component' do @@ -467,21 +473,13 @@ time = parser.make_time([2010,9,8,25,13,14,0,1]) expect(time).to be_nil end context "default timezone" do - before do - @default_timezone = Timeliness.default_timezone - end - it "should be used if no zone value" do Timeliness.default_timezone = :utc time = parser.make_time([2000,6,1,12,0,0]) expect(time.utc_offset).to eq 0 - end - - after do - Timeliness.default_timezone = @default_timezone end end context "with zone value" do context ":utc" do