tzinfo 1.2.3 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.yardopts +3 -0
  5. data/CHANGES.md +501 -372
  6. data/LICENSE +13 -13
  7. data/README.md +368 -113
  8. data/lib/tzinfo.rb +64 -29
  9. data/lib/tzinfo/country.rb +141 -129
  10. data/lib/tzinfo/country_timezone.rb +71 -101
  11. data/lib/tzinfo/data_source.rb +389 -144
  12. data/lib/tzinfo/data_sources.rb +8 -0
  13. data/lib/tzinfo/data_sources/constant_offset_data_timezone_info.rb +56 -0
  14. data/lib/tzinfo/data_sources/country_info.rb +42 -0
  15. data/lib/tzinfo/data_sources/data_timezone_info.rb +91 -0
  16. data/lib/tzinfo/data_sources/linked_timezone_info.rb +33 -0
  17. data/lib/tzinfo/data_sources/ruby_data_source.rb +143 -0
  18. data/lib/tzinfo/data_sources/timezone_info.rb +47 -0
  19. data/lib/tzinfo/data_sources/transitions_data_timezone_info.rb +214 -0
  20. data/lib/tzinfo/data_sources/zoneinfo_data_source.rb +575 -0
  21. data/lib/tzinfo/data_sources/zoneinfo_reader.rb +286 -0
  22. data/lib/tzinfo/data_timezone.rb +33 -47
  23. data/lib/tzinfo/datetime_with_offset.rb +153 -0
  24. data/lib/tzinfo/format1.rb +10 -0
  25. data/lib/tzinfo/format1/country_definer.rb +17 -0
  26. data/lib/tzinfo/format1/country_index_definition.rb +64 -0
  27. data/lib/tzinfo/format1/timezone_definer.rb +64 -0
  28. data/lib/tzinfo/format1/timezone_definition.rb +39 -0
  29. data/lib/tzinfo/format1/timezone_index_definition.rb +77 -0
  30. data/lib/tzinfo/format2.rb +10 -0
  31. data/lib/tzinfo/format2/country_definer.rb +68 -0
  32. data/lib/tzinfo/format2/country_index_definer.rb +68 -0
  33. data/lib/tzinfo/format2/country_index_definition.rb +46 -0
  34. data/lib/tzinfo/format2/timezone_definer.rb +94 -0
  35. data/lib/tzinfo/format2/timezone_definition.rb +73 -0
  36. data/lib/tzinfo/format2/timezone_index_definer.rb +45 -0
  37. data/lib/tzinfo/format2/timezone_index_definition.rb +55 -0
  38. data/lib/tzinfo/info_timezone.rb +26 -21
  39. data/lib/tzinfo/linked_timezone.rb +33 -52
  40. data/lib/tzinfo/offset_timezone_period.rb +42 -0
  41. data/lib/tzinfo/string_deduper.rb +118 -0
  42. data/lib/tzinfo/time_with_offset.rb +128 -0
  43. data/lib/tzinfo/timestamp.rb +548 -0
  44. data/lib/tzinfo/timestamp_with_offset.rb +85 -0
  45. data/lib/tzinfo/timezone.rb +989 -498
  46. data/lib/tzinfo/timezone_offset.rb +90 -57
  47. data/lib/tzinfo/timezone_period.rb +155 -197
  48. data/lib/tzinfo/timezone_proxy.rb +71 -74
  49. data/lib/tzinfo/timezone_transition.rb +77 -99
  50. data/lib/tzinfo/transitions_timezone_period.rb +63 -0
  51. data/lib/tzinfo/untaint_ext.rb +18 -0
  52. data/lib/tzinfo/version.rb +7 -0
  53. data/lib/tzinfo/with_offset.rb +61 -0
  54. metadata +63 -120
  55. metadata.gz.sig +0 -0
  56. data/Rakefile +0 -107
  57. data/lib/tzinfo/country_index_definition.rb +0 -31
  58. data/lib/tzinfo/country_info.rb +0 -42
  59. data/lib/tzinfo/data_timezone_info.rb +0 -55
  60. data/lib/tzinfo/linked_timezone_info.rb +0 -26
  61. data/lib/tzinfo/offset_rationals.rb +0 -77
  62. data/lib/tzinfo/ruby_core_support.rb +0 -146
  63. data/lib/tzinfo/ruby_country_info.rb +0 -70
  64. data/lib/tzinfo/ruby_data_source.rb +0 -136
  65. data/lib/tzinfo/time_or_datetime.rb +0 -333
  66. data/lib/tzinfo/timezone_definition.rb +0 -36
  67. data/lib/tzinfo/timezone_index_definition.rb +0 -54
  68. data/lib/tzinfo/timezone_info.rb +0 -30
  69. data/lib/tzinfo/timezone_transition_definition.rb +0 -101
  70. data/lib/tzinfo/transition_data_timezone_info.rb +0 -274
  71. data/lib/tzinfo/zoneinfo_country_info.rb +0 -35
  72. data/lib/tzinfo/zoneinfo_data_source.rb +0 -487
  73. data/lib/tzinfo/zoneinfo_timezone_info.rb +0 -294
  74. data/test/tc_country.rb +0 -236
  75. data/test/tc_country_index_definition.rb +0 -69
  76. data/test/tc_country_info.rb +0 -16
  77. data/test/tc_country_timezone.rb +0 -161
  78. data/test/tc_data_source.rb +0 -218
  79. data/test/tc_data_timezone.rb +0 -99
  80. data/test/tc_data_timezone_info.rb +0 -18
  81. data/test/tc_info_timezone.rb +0 -34
  82. data/test/tc_linked_timezone.rb +0 -155
  83. data/test/tc_linked_timezone_info.rb +0 -23
  84. data/test/tc_offset_rationals.rb +0 -23
  85. data/test/tc_ruby_core_support.rb +0 -168
  86. data/test/tc_ruby_country_info.rb +0 -80
  87. data/test/tc_ruby_data_source.rb +0 -143
  88. data/test/tc_time_or_datetime.rb +0 -639
  89. data/test/tc_timezone.rb +0 -1350
  90. data/test/tc_timezone_definition.rb +0 -113
  91. data/test/tc_timezone_index_definition.rb +0 -73
  92. data/test/tc_timezone_info.rb +0 -11
  93. data/test/tc_timezone_london.rb +0 -143
  94. data/test/tc_timezone_melbourne.rb +0 -142
  95. data/test/tc_timezone_new_york.rb +0 -142
  96. data/test/tc_timezone_offset.rb +0 -126
  97. data/test/tc_timezone_period.rb +0 -548
  98. data/test/tc_timezone_proxy.rb +0 -127
  99. data/test/tc_timezone_transition.rb +0 -352
  100. data/test/tc_timezone_transition_definition.rb +0 -284
  101. data/test/tc_timezone_utc.rb +0 -27
  102. data/test/tc_transition_data_timezone_info.rb +0 -423
  103. data/test/tc_zoneinfo_country_info.rb +0 -64
  104. data/test/tc_zoneinfo_data_source.rb +0 -1193
  105. data/test/tc_zoneinfo_timezone_info.rb +0 -1128
  106. data/test/test_utils.rb +0 -134
  107. data/test/ts_all.rb +0 -7
  108. data/test/ts_all_ruby.rb +0 -5
  109. data/test/ts_all_zoneinfo.rb +0 -7
  110. data/test/tzinfo-data/tzinfo/data.rb +0 -8
  111. data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +0 -89
  112. data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +0 -315
  113. data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +0 -218
  114. data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +0 -19
  115. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +0 -21
  116. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +0 -21
  117. data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +0 -21
  118. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +0 -261
  119. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +0 -186
  120. data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +0 -321
  121. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +0 -265
  122. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +0 -220
  123. data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +0 -16
  124. data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +0 -927
  125. data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +0 -596
  126. data/test/tzinfo-data/tzinfo/data/version.rb +0 -14
  127. data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
  128. data/test/zoneinfo/America/New_York +0 -0
  129. data/test/zoneinfo/Australia/Melbourne +0 -0
  130. data/test/zoneinfo/EST +0 -0
  131. data/test/zoneinfo/Etc/UTC +0 -0
  132. data/test/zoneinfo/Europe/Amsterdam +0 -0
  133. data/test/zoneinfo/Europe/Andorra +0 -0
  134. data/test/zoneinfo/Europe/London +0 -0
  135. data/test/zoneinfo/Europe/Paris +0 -0
  136. data/test/zoneinfo/Europe/Prague +0 -0
  137. data/test/zoneinfo/Factory +0 -0
  138. data/test/zoneinfo/iso3166.tab +0 -275
  139. data/test/zoneinfo/posix/Europe/London +0 -0
  140. data/test/zoneinfo/posixrules +0 -0
  141. data/test/zoneinfo/right/Europe/London +0 -0
  142. data/test/zoneinfo/zone.tab +0 -439
  143. data/test/zoneinfo/zone1970.tab +0 -369
  144. data/tzinfo.gemspec +0 -21
@@ -1,294 +0,0 @@
1
- module TZInfo
2
- # An InvalidZoneinfoFile exception is raised if an attempt is made to load an
3
- # invalid zoneinfo file.
4
- class InvalidZoneinfoFile < StandardError
5
- end
6
-
7
- # Represents a timezone defined by a compiled zoneinfo TZif (\0, 2 or 3) file.
8
- #
9
- # @private
10
- class ZoneinfoTimezoneInfo < TransitionDataTimezoneInfo #:nodoc:
11
-
12
- # Minimum supported timestamp (inclusive).
13
- #
14
- # Time.utc(1700, 1, 1).to_i
15
- MIN_TIMESTAMP = -8520336000
16
-
17
- # Maximum supported timestamp (exclusive).
18
- #
19
- # Time.utc(2500, 1, 1).to_i
20
- MAX_TIMESTAMP = 16725225600
21
-
22
- # Constructs the new ZoneinfoTimezoneInfo with an identifier and path
23
- # to the file.
24
- def initialize(identifier, file_path)
25
- super(identifier)
26
-
27
- File.open(file_path, 'rb') do |file|
28
- parse(file)
29
- end
30
- end
31
-
32
- private
33
- # Unpack will return unsigned 32-bit integers. Translate to
34
- # signed 32-bit.
35
- def make_signed_int32(long)
36
- long >= 0x80000000 ? long - 0x100000000 : long
37
- end
38
-
39
- # Unpack will return a 64-bit integer as two unsigned 32-bit integers
40
- # (most significant first). Translate to signed 64-bit
41
- def make_signed_int64(high, low)
42
- unsigned = (high << 32) | low
43
- unsigned >= 0x8000000000000000 ? unsigned - 0x10000000000000000 : unsigned
44
- end
45
-
46
- # Read bytes from file and check that the correct number of bytes could
47
- # be read. Raises InvalidZoneinfoFile if the number of bytes didn't match
48
- # the number requested.
49
- def check_read(file, bytes)
50
- result = file.read(bytes)
51
-
52
- unless result && result.length == bytes
53
- raise InvalidZoneinfoFile, "Expected #{bytes} bytes reading '#{file.path}', but got #{result ? result.length : 0} bytes"
54
- end
55
-
56
- result
57
- end
58
-
59
- # Zoneinfo files don't include the offset from standard time (std_offset)
60
- # for DST periods. Derive the base offset (utc_offset) where DST is
61
- # observed from either the previous or next non-DST period.
62
- #
63
- # Returns the index of the offset to be used prior to the first
64
- # transition.
65
- def derive_offsets(transitions, offsets)
66
- # The first non-DST offset (if there is one) is the offset observed
67
- # before the first transition. Fallback to the first DST offset if there
68
- # are no non-DST offsets.
69
- first_non_dst_offset_index = offsets.index {|o| !o[:is_dst] }
70
- first_offset_index = first_non_dst_offset_index || 0
71
- return first_offset_index if transitions.empty?
72
-
73
- # Determine the utc_offset of the next non-dst offset at each transition.
74
- utc_offset_from_next = nil
75
-
76
- transitions.reverse_each do |transition|
77
- offset = offsets[transition[:offset]]
78
- if offset[:is_dst]
79
- transition[:utc_offset_from_next] = utc_offset_from_next if utc_offset_from_next
80
- else
81
- utc_offset_from_next = offset[:utc_total_offset]
82
- end
83
- end
84
-
85
- utc_offset_from_previous = first_non_dst_offset_index ? offsets[first_non_dst_offset_index][:utc_total_offset] : nil
86
- defined_offsets = {}
87
-
88
- transitions.each do |transition|
89
- offset_index = transition[:offset]
90
- offset = offsets[offset_index]
91
- utc_total_offset = offset[:utc_total_offset]
92
-
93
- if offset[:is_dst]
94
- utc_offset_from_next = transition[:utc_offset_from_next]
95
-
96
- difference_to_previous = utc_total_offset - (utc_offset_from_previous || utc_total_offset)
97
- difference_to_next = utc_total_offset - (utc_offset_from_next || utc_total_offset)
98
-
99
- utc_offset = if difference_to_previous > 0 && difference_to_next > 0
100
- difference_to_previous < difference_to_next ? utc_offset_from_previous : utc_offset_from_next
101
- elsif difference_to_previous > 0
102
- utc_offset_from_previous
103
- elsif difference_to_next > 0
104
- utc_offset_from_next
105
- else # difference_to_previous <= 0 && difference_to_next <= 0
106
- # DST, but the either the offset has stayed the same or decreased
107
- # relative to both the previous and next used base utc offset, or
108
- # there are no non-DST offsets. Assume a 1 hour offset from base.
109
- utc_total_offset - 3600
110
- end
111
-
112
- if !offset[:utc_offset]
113
- offset[:utc_offset] = utc_offset
114
- defined_offsets[offset] = offset_index
115
- elsif offset[:utc_offset] != utc_offset
116
- # An earlier transition has already derived a different
117
- # utc_offset. Define a new offset or reuse an existing identically
118
- # defined offset.
119
- new_offset = offset.dup
120
- new_offset[:utc_offset] = utc_offset
121
-
122
- offset_index = defined_offsets[new_offset]
123
-
124
- unless offset_index
125
- offsets << new_offset
126
- offset_index = offsets.length - 1
127
- defined_offsets[new_offset] = offset_index
128
- end
129
-
130
- transition[:offset] = offset_index
131
- end
132
- else
133
- utc_offset_from_previous = utc_total_offset
134
- end
135
- end
136
-
137
- first_offset_index
138
- end
139
-
140
- # Defines an offset for the timezone based on the given index and offset
141
- # Hash.
142
- def define_offset(index, offset)
143
- utc_total_offset = offset[:utc_total_offset]
144
- utc_offset = offset[:utc_offset]
145
-
146
- if utc_offset
147
- # DST offset with base utc_offset derived by derive_offsets.
148
- std_offset = utc_total_offset - utc_offset
149
- elsif offset[:is_dst]
150
- # DST offset unreferenced by a transition (offset in use before the
151
- # first transition). No derived base UTC offset, so assume 1 hour
152
- # DST.
153
- utc_offset = utc_total_offset - 3600
154
- std_offset = 3600
155
- else
156
- # Non-DST offset.
157
- utc_offset = utc_total_offset
158
- std_offset = 0
159
- end
160
-
161
- offset index, utc_offset, std_offset, offset[:abbr].untaint.to_sym
162
- end
163
-
164
- # Parses a zoneinfo file and intializes the DataTimezoneInfo structures.
165
- def parse(file)
166
- magic, version, ttisgmtcnt, ttisstdcnt, leapcnt, timecnt, typecnt, charcnt =
167
- check_read(file, 44).unpack('a4 a x15 NNNNNN')
168
-
169
- if magic != 'TZif'
170
- raise InvalidZoneinfoFile, "The file '#{file.path}' does not start with the expected header."
171
- end
172
-
173
- if (version == '2' || version == '3') && RubyCoreSupport.time_supports_64bit
174
- # Skip the first 32-bit section and read the header of the second 64-bit section
175
- file.seek(timecnt * 5 + typecnt * 6 + charcnt + leapcnt * 8 + ttisgmtcnt + ttisstdcnt, IO::SEEK_CUR)
176
-
177
- prev_version = version
178
-
179
- magic, version, ttisgmtcnt, ttisstdcnt, leapcnt, timecnt, typecnt, charcnt =
180
- check_read(file, 44).unpack('a4 a x15 NNNNNN')
181
-
182
- unless magic == 'TZif' && (version == prev_version)
183
- raise InvalidZoneinfoFile, "The file '#{file.path}' contains an invalid 64-bit section header."
184
- end
185
-
186
- using_64bit = true
187
- elsif version != '3' && version != '2' && version != "\0"
188
- raise InvalidZoneinfoFile, "The file '#{file.path}' contains a version of the zoneinfo format that is not currently supported."
189
- else
190
- using_64bit = false
191
- end
192
-
193
- unless leapcnt == 0
194
- raise InvalidZoneinfoFile, "The zoneinfo file '#{file.path}' contains leap second data. TZInfo requires zoneinfo files that omit leap seconds."
195
- end
196
-
197
- transitions = []
198
-
199
- if using_64bit
200
- timecnt.times do |i|
201
- high, low = check_read(file, 8).unpack('NN'.freeze)
202
- transition_time = make_signed_int64(high, low)
203
- transitions << {:at => transition_time}
204
- end
205
- else
206
- timecnt.times do |i|
207
- transition_time = make_signed_int32(check_read(file, 4).unpack('N'.freeze)[0])
208
- transitions << {:at => transition_time}
209
- end
210
- end
211
-
212
- timecnt.times do |i|
213
- localtime_type = check_read(file, 1).unpack('C'.freeze)[0]
214
- transitions[i][:offset] = localtime_type
215
- end
216
-
217
- offsets = []
218
-
219
- typecnt.times do |i|
220
- gmtoff, isdst, abbrind = check_read(file, 6).unpack('NCC'.freeze)
221
- gmtoff = make_signed_int32(gmtoff)
222
- isdst = isdst == 1
223
- offset = {:utc_total_offset => gmtoff, :is_dst => isdst, :abbr_index => abbrind}
224
-
225
- unless isdst
226
- offset[:utc_offset] = gmtoff
227
- offset[:std_offset] = 0
228
- end
229
-
230
- offsets << offset
231
- end
232
-
233
- abbrev = check_read(file, charcnt)
234
-
235
- offsets.each do |o|
236
- abbrev_start = o[:abbr_index]
237
- raise InvalidZoneinfoFile, "Abbreviation index is out of range in file '#{file.path}'" unless abbrev_start < abbrev.length
238
-
239
- abbrev_end = abbrev.index("\0", abbrev_start)
240
- raise InvalidZoneinfoFile, "Missing abbreviation null terminator in file '#{file.path}'" unless abbrev_end
241
-
242
- o[:abbr] = RubyCoreSupport.force_encoding(abbrev[abbrev_start...abbrev_end], 'UTF-8')
243
- end
244
-
245
- transitions.each do |t|
246
- if t[:offset] < 0 || t[:offset] >= offsets.length
247
- raise InvalidZoneinfoFile, "Invalid offset referenced by transition in file '#{file.path}'."
248
- end
249
- end
250
-
251
- # Derive the offsets from standard time (std_offset).
252
- first_offset_index = derive_offsets(transitions, offsets)
253
-
254
- define_offset(first_offset_index, offsets[first_offset_index])
255
-
256
- offsets.each_with_index do |o, i|
257
- define_offset(i, o) unless i == first_offset_index
258
- end
259
-
260
- if !using_64bit && !RubyCoreSupport.time_supports_negative
261
- # Filter out transitions that are not supported by Time on this
262
- # platform.
263
-
264
- # Move the last transition before the epoch up to the epoch. This
265
- # allows for accurate conversions for all supported timestamps on the
266
- # platform.
267
-
268
- before_epoch, after_epoch = transitions.partition {|t| t[:at] < 0}
269
-
270
- if before_epoch.length > 0 && after_epoch.length > 0 && after_epoch.first[:at] != 0
271
- last_before = before_epoch.last
272
- last_before[:at] = 0
273
- transitions = [last_before] + after_epoch
274
- else
275
- transitions = after_epoch
276
- end
277
- end
278
-
279
- # Ignore transitions that occur outside of a defined window. The
280
- # transition index cannot handle a large range of transition times.
281
- #
282
- # This is primarily intended to ignore the far in the past transition
283
- # added in zic 2014c (at timestamp -2**63 in zic 2014c and at the
284
- # approximate time of the big bang from zic 2014d).
285
- transitions.each do |t|
286
- at = t[:at]
287
- if at >= MIN_TIMESTAMP && at < MAX_TIMESTAMP
288
- time = Time.at(at).utc
289
- transition time.year, time.mon, t[:offset], at
290
- end
291
- end
292
- end
293
- end
294
- end
@@ -1,236 +0,0 @@
1
- require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
2
-
3
- include TZInfo
4
-
5
- class TCCountry < Minitest::Test
6
- def setup
7
- @orig_data_source = DataSource.get
8
- Country.send :init_countries
9
- end
10
-
11
- def teardown
12
- DataSource.set(@orig_data_source)
13
- end
14
-
15
- def test_get_valid
16
- c = Country.get('GB')
17
-
18
- assert c
19
- assert_equal('GB', c.code)
20
- end
21
-
22
- def test_get_not_exist
23
- assert_raises(InvalidCountryCode) {
24
- Country.get('ZZ')
25
- }
26
- end
27
-
28
- def test_get_invalid
29
- assert_raises(InvalidCountryCode) {
30
- Country.get('../Countries/GB')
31
- }
32
- end
33
-
34
- def test_get_nil
35
- assert_raises(InvalidCountryCode) {
36
- Country.get(nil)
37
- }
38
- end
39
-
40
- def test_get_case
41
- assert_raises(InvalidCountryCode) {
42
- Country.get('gb')
43
- }
44
- end
45
-
46
- def test_get_tainted_loaded
47
- Country.get('GB')
48
-
49
- safe_test do
50
- code = 'GB'.dup.taint
51
- assert(code.tainted?)
52
- country = Country.get(code)
53
- assert_equal('GB', country.code)
54
- assert(code.tainted?)
55
- end
56
- end
57
-
58
- def test_get_tainted_and_frozen_loaded
59
- Country.get('GB')
60
-
61
- safe_test do
62
- country = Country.get('GB'.dup.taint.freeze)
63
- assert_equal('GB', country.code)
64
- end
65
- end
66
-
67
- def test_get_tainted_not_previously_loaded
68
- safe_test do
69
- code = 'GB'.dup.taint
70
- assert(code.tainted?)
71
- country = Country.get(code)
72
- assert_equal('GB', country.code)
73
- assert(code.tainted?)
74
- end
75
- end
76
-
77
- def test_get_tainted_and_frozen_not_previously_loaded
78
- safe_test do
79
- country = Country.get('GB'.dup.taint.freeze)
80
- assert_equal('GB', country.code)
81
- end
82
- end
83
-
84
- def test_new_nil
85
- assert_raises(InvalidCountryCode) {
86
- Country.new(nil)
87
- }
88
- end
89
-
90
- def test_new_arg
91
- c = Country.new('GB')
92
- assert_same(Country.get('GB'), c)
93
- end
94
-
95
- def test_new_arg_not_exist
96
- assert_raises(InvalidCountryCode) {
97
- Country.new('ZZ')
98
- }
99
- end
100
-
101
- def test_all_codes
102
- all_codes = Country.all_codes
103
- assert_kind_of(Array, all_codes)
104
- end
105
-
106
- def test_all
107
- all = Country.all
108
- assert_equal(Country.all_codes, all.collect {|c| c.code})
109
- end
110
-
111
- def test_code
112
- assert_equal('US', Country.get('US').code)
113
- end
114
-
115
- def test_name
116
- assert_kind_of(String, Country.get('US').name)
117
- end
118
-
119
- def test_to_s
120
- assert_equal(Country.get('US').name, Country.get('US').to_s)
121
- assert_equal(Country.get('GB').name, Country.get('GB').to_s)
122
- end
123
-
124
- def test_zone_identifiers
125
- zone_names = Country.get('US').zone_names
126
- assert_kind_of(Array, zone_names)
127
- assert_equal(true, zone_names.frozen?)
128
- end
129
-
130
- def test_zone_names
131
- assert_equal(Country.get('US').zone_identifiers, Country.get('US').zone_names)
132
- end
133
-
134
- def test_zones
135
- zones = Country.get('US').zones
136
- assert_kind_of(Array, zones)
137
- assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.identifier})
138
-
139
- zones.each {|z| assert_kind_of(TimezoneProxy, z)}
140
- end
141
-
142
- def test_zone_info
143
- zones = Country.get('US').zone_info
144
- assert_kind_of(Array, zones)
145
- assert_equal(true, zones.frozen?)
146
-
147
- assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.identifier})
148
- assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.timezone.identifier})
149
-
150
- zones.each {|z| assert_kind_of(CountryTimezone, z)}
151
- end
152
-
153
- def test_compare
154
- assert_equal(0, Country.get('GB') <=> Country.get('GB'))
155
- assert_equal(-1, Country.get('GB') <=> Country.get('US'))
156
- assert_equal(1, Country.get('US') <=> Country.get('GB'))
157
- assert_equal(-1, Country.get('FR') <=> Country.get('US'))
158
- assert_equal(1, Country.get('US') <=> Country.get('FR'))
159
- end
160
-
161
- def test_compare_non_comparable
162
- assert_nil(Country.get('GB') <=> Object.new)
163
- end
164
-
165
- def test_equality
166
- assert_equal(true, Country.get('GB') == Country.get('GB'))
167
- assert_equal(false, Country.get('GB') == Country.get('US'))
168
- assert(!(Country.get('GB') == Object.new))
169
- end
170
-
171
- def test_eql
172
- assert_equal(true, Country.get('GB').eql?(Country.get('GB')))
173
- assert_equal(false, Country.get('GB').eql?(Country.get('US')))
174
- assert(!Country.get('GB').eql?(Object.new))
175
- end
176
-
177
- def test_hash
178
- assert_equal('GB'.hash, Country.get('GB').hash)
179
- assert_equal('US'.hash, Country.get('US').hash)
180
- end
181
-
182
- def test_marshal
183
- c = Country.get('US')
184
-
185
- # Should get back the same instance because load calls Country.get.
186
- assert_same(c, Marshal.load(Marshal.dump(c)))
187
- end
188
-
189
- def test_reload
190
- # If country gets reloaded for some reason, it needs to force a reload of
191
- # the country index.
192
-
193
- c = Country.get('US')
194
- assert_equal('US', Country.get('US').code)
195
-
196
- # Suppress redefined method warnings.
197
- without_warnings do
198
- load 'tzinfo/country.rb'
199
- end
200
-
201
- c = Country.get('US')
202
- assert_equal('US', Country.get('US').code)
203
- end
204
-
205
- def test_get_missing_data_source
206
- DataSource.set(DataSource.new)
207
-
208
- assert_raises(InvalidDataSource) do
209
- Country.get('GB')
210
- end
211
- end
212
-
213
- def test_new_missing_data_source
214
- DataSource.set(DataSource.new)
215
-
216
- assert_raises(InvalidDataSource) do
217
- Country.new('GB')
218
- end
219
- end
220
-
221
- def test_all_codes_missing_data_source
222
- DataSource.set(DataSource.new)
223
-
224
- assert_raises(InvalidDataSource) do
225
- Country.all_codes
226
- end
227
- end
228
-
229
- def test_all_missing_data_source
230
- DataSource.set(DataSource.new)
231
-
232
- assert_raises(InvalidDataSource) do
233
- Country.all
234
- end
235
- end
236
- end