File: tzinfo_spec.rb

package info (click to toggle)
ruby-icalendar 2.4.1-2~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 468 kB
  • sloc: ruby: 2,507; makefile: 4
file content (110 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'spec_helper'
require 'icalendar/tzinfo'

describe 'TZInfo::Timezone' do

  let(:tz) { TZInfo::Timezone.get 'Europe/Copenhagen' }
  let(:date) { DateTime.new 2014 }
  subject { tz.ical_timezone date }

  describe 'daylight offset' do
    specify { expect(subject.daylights.first.tzoffsetto.value_ical).to eq "+0200" }
    specify { expect(subject.daylights.first.tzoffsetfrom.value_ical).to eq "+0100" }
  end

  describe 'standard offset' do
    specify { expect(subject.standards.first.tzoffsetto.value_ical).to eq "+0100" }
    specify { expect(subject.standards.first.tzoffsetfrom.value_ical).to eq "+0200" }
  end

  describe 'daylight recurrence rule' do
    specify { expect(subject.daylights.first.rrule.first.value_ical).to eq "FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3" }
  end

  describe 'standard recurrence rule' do
    specify { expect(subject.standards.first.rrule.first.value_ical).to eq "FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10" }
  end

  describe 'no end transition' do
    let(:tz) { TZInfo::Timezone.get 'Asia/Shanghai' }
    let(:date) { DateTime.now }

    it 'only creates a standard component' do
      expect(subject.to_ical).to eq <<-EXPECTED.gsub "\n", "\r\n"
BEGIN:VTIMEZONE
TZID:Asia/Shanghai
BEGIN:STANDARD
DTSTART:19910915T010000
TZOFFSETFROM:+0900
TZOFFSETTO:+0800
TZNAME:CST
END:STANDARD
END:VTIMEZONE
      EXPECTED
    end
  end

  describe 'no transition' do
    let(:tz) { TZInfo::Timezone.get 'UTC' }
    let(:date) { DateTime.now }

    it 'creates a standard component with equal offsets' do
      expect(subject.to_ical).to eq <<-EXPECTED.gsub "\n", "\r\n"
BEGIN:VTIMEZONE
TZID:UTC
BEGIN:STANDARD
DTSTART:19700101T000000
TZOFFSETFROM:+0000
TZOFFSETTO:+0000
TZNAME:UTC
END:STANDARD
END:VTIMEZONE
      EXPECTED
    end
  end

  describe 'dst transition' do
    subject { TZInfo::Timezone.get 'America/Los_Angeles' }
    let(:now) { subject.now }
    # freeze in DST transition in America/Los_Angeles
    before(:each) { Timecop.freeze DateTime.new(2013, 11, 03, 1, 30, 0, '-08:00') }
    after(:each) { Timecop.return }

    specify { expect { subject.ical_timezone now, nil }.to raise_error TZInfo::AmbiguousTime }
    specify { expect { subject.ical_timezone now, true }.not_to raise_error }
    specify { expect { subject.ical_timezone now, false }.not_to raise_error }

    context 'TZInfo::Timezone.default_dst = nil' do
      before(:each) { TZInfo::Timezone.default_dst = nil }
      specify { expect { subject.ical_timezone now }.to raise_error TZInfo::AmbiguousTime }
    end

    context 'TZInfo::Timezone.default_dst = true' do
      before(:each) { TZInfo::Timezone.default_dst = true }
      specify { expect { subject.ical_timezone now }.not_to raise_error }
    end

    context 'TZInfo::Timezone.default_dst = false' do
      before(:each) { TZInfo::Timezone.default_dst = false }
      specify { expect { subject.ical_timezone now }.not_to raise_error }
    end
  end

  describe 'tzname for offset' do
    # Check for CET/CEST correctness, which doesn't follow
    # the more common *ST/*DT style abbreviations.
    let(:tz) { TZInfo::Timezone.get 'Europe/Prague' }
    let(:ical_tz) { tz.ical_timezone date }

    describe '#daylight' do
      subject(:tzname) { ical_tz.daylights.first.tzname.first }
      it { should eql 'CEST' }
    end

    describe '#standard' do
      subject(:tzname) { ical_tz.standards.first.tzname.first }
      it { should eql 'CET' }
    end
  end

end