spec/data/zone_spec.rb in barometer-0.8.0 vs spec/data/zone_spec.rb in barometer-0.9.0
- old
+ new
@@ -1,366 +1,299 @@
-require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require_relative '../spec_helper'
-describe "Data::Zone" do
+module Barometer::Data
+ describe Zone do
+ describe '#new' do
+ let(:zone) { double(:zone) }
- # describe "and class methods" do
- #
- # it "responds to now and returns Time object" do
- # Data::Zone.respond_to?("now").should be_true
- # Data::Zone.now.is_a?(Time).should be_true
- # end
- #
- # it "responds to today and returns Date object" do
- # Data::Zone.respond_to?("today").should be_true
- # Data::Zone.today.is_a?(Date).should be_true
- # end
- #
- # end
+ before do
+ ZoneFull.stub(detect?: false)
+ ZoneOffset.stub(detect?: false)
+ ZoneCode.stub(detect?: false)
+ end
- describe "when initialized" do
+ it 'detects a full timezone input' do
+ ZoneFull.stub(detect?: true, new: nil)
+ Zone.new(zone)
+ expect( ZoneFull ).to have_received(:new).with(zone)
+ end
- describe "with a full zone" do
+ it 'detects a timezone code input' do
+ ZoneOffset.stub(detect?: true, new: nil)
+ Zone.new(zone)
+ expect( ZoneOffset ).to have_received(:new).with(zone)
+ end
- before(:each) do
- @utc = Time.now.utc
- @timezone = "Europe/Paris"
- @zone = Data::Zone.new(@timezone)
+ it 'detects a timezone offset input' do
+ ZoneCode.stub(detect?: true, new: nil)
+ Zone.new(zone)
+ expect( ZoneCode ).to have_received(:new).with(zone)
end
- it "responds to zone_full" do
- @zone.zone_full.should_not be_nil
- @zone.zone_full.should == @timezone
+ it 'raises an error when nothing detected' do
+ expect {
+ Zone.new(zone)
+ }.to raise_error(ArgumentError)
end
+ end
+ end
- it "responds to zone_code" do
- @zone.zone_code.should be_nil
+ describe ZoneFull do
+ def stub_time(utc_now)
+ now = double(:now, utc: utc_now)
+ double(:time_class, now: now)
+ end
+
+ describe '.detect?' do
+ it 'returns true when given a full timezone' do
+ expect( ZoneFull.detect?('America/Los_Angeles') ).to be_true
end
- it "responds to zone_offset" do
- @zone.zone_offset.should be_nil
+ it 'returns false when given a timezone code' do
+ expect( ZoneFull.detect?('PST') ).to be_false
end
- it "responds to tz" do
- lambda { Data::Zone.new("invalid timezone") }.should raise_error(ArgumentError)
+ it 'returns false when given an offset' do
+ expect( ZoneFull.detect?(10) ).to be_false
+ end
- zone = Data::Zone.new(@timezone)
- zone.tz.should_not be_nil
+ it 'returns false when given nothing' do
+ expect( ZoneFull.detect?('') ).to be_false
+ expect( ZoneFull.detect?(nil) ).to be_false
end
+ end
- it "responds to full" do
- @zone.respond_to?("full").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.full.should == @timezone
+ describe '#code' do
+ it 'returns the correct non-DST zone code' do
+ time = stub_time(::Time.utc(2013, 1, 1))
+ zone = ZoneFull.new('America/Los_Angeles', time)
- zone = Data::Zone.new(@timezone)
- zone.full.should == @timezone
+ expect( zone.code ).to eq 'PST'
end
- it "responds to code" do
- @zone.respond_to?("code").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.code.should be_nil
+ it 'returns the correct DST zone code' do
+ time = stub_time(::Time.utc(2013, 6, 1))
+ zone = ZoneFull.new('America/Los_Angeles', time)
- zone = Data::Zone.new(@timezone)
- # the expected result of this depends on the time of year
- # when summer expect "CEST", otherwise "CET"
- # just let TZINFO handle this
- zone.code.should == TZInfo::Timezone.get(@timezone).period_for_utc(Time.now.utc).zone_identifier.to_s
+ expect( zone.code ).to eq 'PDT'
end
+ end
- it "responds to dst?" do
- @zone.respond_to?("dst?").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.dst?.should be_nil
+ describe '#offset' do
+ it 'returns the current non-DST offset' do
+ time = stub_time(::Time.utc(2013, 1, 1, 18, 0, 0))
+ zone = ZoneFull.new('America/Los_Angeles', time)
+
+ expect( zone.offset ).to eq(-8 * 60 * 60)
end
- it "responds to now" do
- @zone.respond_to?("now").should be_true
- @zone.now.is_a?(Time).should be_true
+ it 'returns the current DST offset' do
+ time = stub_time(::Time.utc(2013, 6, 1, 18, 0, 0))
+ zone = ZoneFull.new('America/Los_Angeles', time)
- period = @zone.tz.period_for_utc(Time.now)
- actual_now = Time.now.utc + period.utc_total_offset
+ expect( zone.offset ).to eq(-7 * 60 * 60)
+ end
+ end
- now = @zone.now
- now.hour.should == actual_now.hour
- now.min.should == actual_now.min
- now.sec.should == actual_now.sec
- now.year.should == actual_now.year
- now.month.should == actual_now.month
- now.day.should == actual_now.day
+ describe '#now' do
+ it 'returns the current non-DST local time' do
+ time = stub_time(::Time.utc(2013, 1, 1, 18, 0, 0))
+ zone = ZoneFull.new('America/Los_Angeles', time)
+
+ expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 10, 0, 0)
end
- it "responds to today" do
- @zone.respond_to?("today").should be_true
- @zone.today.is_a?(Date).should be_true
+ it 'returns the current DST local time' do
+ time = stub_time(::Time.utc(2013, 6, 1, 18, 0, 0))
+ zone = ZoneFull.new('America/Los_Angeles', time)
- period = @zone.tz.period_for_utc(Time.now)
- actual_now = Time.now.utc + period.utc_total_offset
+ expect( zone.now ).to eq ::Time.utc(2013, 6, 1, 11, 0, 0)
+ end
+ end
- now = @zone.today
- now.year.should == actual_now.year
- now.month.should == actual_now.month
- now.day.should == actual_now.day
+ describe '#to_s' do
+ it 'returns the input zone' do
+ expect( ZoneFull.new('Europe/Paris').to_s ).to eq 'Europe/Paris'
end
+ end
- it "converts local_time to utc" do
- local_time = Time.now.utc
- utc_time = @zone.local_to_utc(local_time)
+ describe '#local_to_utc' do
+ it 'converts a time in the local time zone to UTC' do
+ zone = ZoneFull.new('America/Los_Angeles')
+ local_time = ::Time.now.utc
- offset = @zone.tz.period_for_utc(local_time).utc_total_offset
- utc_time.should == (local_time - offset)
+ expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
end
+ end
- it "converts utc to local_time" do
- utc_time = Time.now.utc
- local_time = @zone.utc_to_local(utc_time)
+ describe '#utc_to_local' do
+ it 'converts a time in the local time zone to UTC' do
+ zone = ZoneFull.new('America/Los_Angeles')
+ utc_time = ::Time.now.utc
- offset = @zone.tz.period_for_utc(local_time).utc_total_offset
- utc_time.should == (local_time - offset)
+ expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
end
+ end
+ end
+ describe ZoneOffset do
+ def stub_time(utc_now)
+ now = double(:now, utc: utc_now)
+ double(:time_class, now: now)
end
- describe "with a zone code" do
-
- before(:each) do
- @utc = Time.now.utc
- @timezone = "EAST"
- @zone = Data::Zone.new(@timezone)
+ describe '.detect?' do
+ it 'returns false when given a full timezone' do
+ expect( ZoneOffset.detect?('America/Los_Angeles') ).to be_false
end
- it "responds to zone_code" do
- @zone.zone_code.should_not be_nil
- @zone.zone_code.should == @timezone
+ it 'returns false when given a timezone code' do
+ expect( ZoneOffset.detect?('PST') ).to be_false
end
- it "responds to zone_full" do
- @zone.zone_full.should be_nil
+ it 'returns true when given an offset' do
+ expect( ZoneOffset.detect?(10) ).to be_true
end
- it "responds to zone_offset" do
- @zone.zone_offset.should be_nil
+ it 'returns false when given an offset out of range' do
+ expect( ZoneOffset.detect?(15) ).to be_false
end
- it "responds to tz" do
- zone = Data::Zone.new(@timezone)
- zone.tz.should be_nil
+ it 'returns false when given nothing' do
+ expect( ZoneOffset.detect?('') ).to be_false
+ expect( ZoneOffset.detect?(nil) ).to be_false
end
+ end
- it "responds to code" do
- @zone.respond_to?("code").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.code.should == @timezone
-
- zone = Data::Zone.new(@timezone)
- zone.code.should == @timezone
+ describe '#code' do
+ it 'returns nil' do
+ expect( ZoneOffset.new(10).code ).to be_nil
end
+ end
- it "responds to full" do
- @zone.respond_to?("full").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.full.should be_nil
-
- zone = Data::Zone.new(@timezone)
- zone.full.should be_nil
+ describe '#offset' do
+ it 'converts the input from hours to seconds' do
+ expect( ZoneOffset.new(5).offset ).to eq(5 * 60 * 60)
end
+ end
- it "responds to now" do
- @zone.respond_to?("now").should be_true
- @zone.now.is_a?(Time).should be_true
+ describe '#now' do
+ it 'returns the current local time' do
+ time = stub_time(::Time.utc(2013, 1, 1, 10, 0, 0))
+ zone = ZoneOffset.new(5, time)
- actual_now = Time.now.utc + (-6*60*60)
-
- now = @zone.now
- now.hour.should == actual_now.hour
- now.min.should == actual_now.min
- now.sec.should == actual_now.sec
- now.year.should == actual_now.year
- now.month.should == actual_now.month
- now.day.should == actual_now.day
+ expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 15, 0, 0)
end
+ end
- it "responds to today" do
- @zone.respond_to?("today").should be_true
- @zone.today.is_a?(Date).should be_true
-
- actual_now = Time.now.utc + (-6*60*60)
-
- now = @zone.today
- now.year.should == actual_now.year
- now.month.should == actual_now.month
- now.day.should == actual_now.day
+ describe '#to_s' do
+ it 'returns the input zone' do
+ expect( ZoneOffset.new(5).to_s ).to eq '5'
end
+ end
- it "converts local_time to utc" do
- local_time = Time.now.utc
- utc_time = @zone.local_to_utc(local_time)
+ describe '#local_to_utc' do
+ it 'converts a time in the local time zone to UTC' do
+ zone = ZoneOffset.new(5)
+ local_time = ::Time.now.utc
- utc_time.year.should == (local_time - @zone.offset).year
- utc_time.month.should == (local_time - @zone.offset).month
- utc_time.day.should == (local_time - @zone.offset).day
- utc_time.hour.should == (local_time - @zone.offset).hour
- utc_time.min.should == (local_time - @zone.offset).min
- utc_time.sec.should == (local_time - @zone.offset).sec
+ expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
end
+ end
- it "converts utc to local_time" do
- utc_time = Time.now.utc
- local_time = @zone.utc_to_local(utc_time)
+ describe '#utc_to_local' do
+ it 'converts a time in the local time zone to UTC' do
+ zone = ZoneOffset.new(5)
+ utc_time = ::Time.now.utc
- local_time.year.should == (utc_time + @zone.offset).year
- local_time.month.should == (utc_time + @zone.offset).month
- local_time.day.should == (utc_time + @zone.offset).day
- local_time.hour.should == (utc_time + @zone.offset).hour
- local_time.min.should == (utc_time + @zone.offset).min
- local_time.sec.should == (utc_time + @zone.offset).sec
+ expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
end
+ end
+ end
+ describe ZoneCode do
+ def stub_time(utc_now)
+ now = double(:now, utc: utc_now)
+ double(:time_class, now: now)
end
- describe "with a zone offset" do
-
- before(:each) do
- @utc = Time.now.utc
- @timezone = 8.5
- @zone = Data::Zone.new(@timezone)
+ describe '.detect?' do
+ it 'returns false when given a full timezone' do
+ expect( ZoneCode.detect?('America/Los_Angeles') ).to be_false
end
- it "responds to zone_offset" do
- @zone.zone_offset.should_not be_nil
- @zone.zone_offset.should == @timezone
+ it 'returns true when given a timezone code' do
+ expect( ZoneCode.detect?('PST') ).to be_true
end
- it "responds to zone_full" do
- @zone.zone_full.should be_nil
+ it 'returns true when given an obscure timezone code' do
+ expect( ZoneCode.detect?('CEST') ).to be_true
end
- it "responds to zone_code" do
- @zone.zone_code.should be_nil
+ it 'returns false when given an invalid timezone code' do
+ expect( ZoneCode.detect?('ABC') ).to be_false
end
- it "responds to tz" do
- zone = Data::Zone.new(@timezone)
- zone.tz.should be_nil
+ it 'returns false when given an offset' do
+ expect( ZoneCode.detect?(10) ).to be_false
end
- it "responds to offset" do
- @zone.respond_to?("offset").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.offset.should == (@timezone * 60 * 60)
-
- zone = Data::Zone.new(@timezone)
- zone.offset.should == (@timezone * 60 * 60)
+ it 'returns false when given nothing' do
+ expect( ZoneCode.detect?('') ).to be_false
+ expect( ZoneCode.detect?(nil) ).to be_false
end
+ end
- it "responds to full" do
- @zone.respond_to?("full").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.full.should be_nil
-
- zone = Data::Zone.new(@timezone)
- zone.full.should be_nil
+ describe '#code' do
+ it 'returns the input code' do
+ expect( ZoneCode.new('PST').code ).to eq 'PST'
end
+ end
- it "responds to code" do
- @zone.respond_to?("code").should be_true
- zone = Data::Zone.new(@timezone)
- zone.tz = nil
- zone.tz.should be_nil
- zone.code.should be_nil
-
- zone = Data::Zone.new(@timezone)
- zone.code.should be_nil
+ describe '#offset' do
+ it 'returns the offset in seconds' do
+ expect( ZoneCode.new('PST').offset ).to eq(-8 * 60 * 60)
end
- it "responds to now" do
- @zone.respond_to?("now").should be_true
- @zone.now.is_a?(Time).should be_true
-
- actual_now = Time.now.utc + (@timezone.to_f*60*60)
-
- now = @zone.now
- now.hour.should == actual_now.hour
- now.min.should == actual_now.min
- now.sec.should == actual_now.sec
- now.year.should == actual_now.year
- now.month.should == actual_now.month
- now.day.should == actual_now.day
+ it 'returns the offset in seconds for an obscure input code' do
+ expect( ZoneCode.new('CEST').offset ).to eq(2 * 60 * 60)
end
- it "responds to today" do
- @zone.respond_to?("today").should be_true
- @zone.today.is_a?(Date).should be_true
-
- actual_now = Time.now.utc + (@timezone.to_f*60*60)
-
- now = @zone.today
- now.year.should == actual_now.year
- now.month.should == actual_now.month
- now.day.should == actual_now.day
+ it 'returns 0 for unknown codes' do
+ expect( ZoneCode.new('ABC').offset ).to be_zero
end
+ end
- it "converts local_time to utc" do
- local_time = Time.now.utc
- utc_time = @zone.local_to_utc(local_time)
+ describe '#now' do
+ it 'returns the current local time' do
+ time = stub_time(::Time.utc(2013, 1, 1, 10, 0, 0))
+ zone = ZoneCode.new('PST', time)
- utc_time.year.should == (local_time - @zone.offset).year
- utc_time.month.should == (local_time - @zone.offset).month
- utc_time.day.should == (local_time - @zone.offset).day
- utc_time.hour.should == (local_time - @zone.offset).hour
- utc_time.min.should == (local_time - @zone.offset).min
- utc_time.sec.should == (local_time - @zone.offset).sec
+ expect( zone.now ).to eq ::Time.utc(2013, 1, 1, 2, 0, 0)
end
+ end
- it "converts utc to local_time" do
- utc_time = Time.now.utc
- local_time = @zone.utc_to_local(utc_time)
-
- local_time.year.should == (utc_time + @zone.offset).year
- local_time.month.should == (utc_time + @zone.offset).month
- local_time.day.should == (utc_time + @zone.offset).day
- local_time.hour.should == (utc_time + @zone.offset).hour
- local_time.min.should == (utc_time + @zone.offset).min
- local_time.sec.should == (utc_time + @zone.offset).sec
+ describe '#to_s' do
+ it 'returns the input zone' do
+ expect( ZoneCode.new('PST').to_s ).to eq 'PST'
end
-
end
- end
+ describe '#local_to_utc' do
+ it 'converts a time in the local time zone to UTC' do
+ zone = ZoneCode.new('PST')
+ local_time = ::Time.now.utc
- describe "when detecting zones" do
-
- it "recognozes a full time zone format" do
- Data::Zone.is_zone_full?("invalid").should be_false
- Data::Zone.is_zone_full?("America/New York").should be_true
+ expect( zone.local_to_utc(local_time).to_i ).to eq((local_time - zone.offset).to_i)
+ end
end
- it "matches a zone offset" do
- Data::Zone.is_zone_offset?("invalid").should be_false
- Data::Zone.is_zone_offset?("MST").should be_false
- Data::Zone.is_zone_offset?("10").should be_false
- Data::Zone.is_zone_offset?(-10).should be_true
- end
+ describe '#utc_to_local' do
+ it 'converts a time in the local time zone to UTC' do
+ zone = ZoneCode.new('PST')
+ utc_time = ::Time.now.utc
- it "matches a zone code" do
- Data::Zone.is_zone_code?("invalid").should be_false
- Data::Zone.is_zone_code?("MST").should be_true
- Data::Zone.is_zone_code?("EAST").should be_true
+ expect( zone.utc_to_local(utc_time).to_i ).to eq((utc_time + zone.offset).to_i)
+ end
end
-
end
-
end