/
tc_country.rb
238 lines (188 loc) · 5.65 KB
/
tc_country.rb
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require File.join(File.expand_path(File.dirname(__FILE__)), 'test_utils')
include TZInfo
# Use send as a workaround for erroneous 'wrong number of arguments' errors with
# JRuby 9.0.5.0 when calling methods with Java implementations. See #114.
send(:using, TaintExt) if Module.const_defined?(:TaintExt)
class TCCountry < Minitest::Test
def setup
@orig_data_source = DataSource.get
Country.send :init_countries
end
def teardown
DataSource.set(@orig_data_source)
end
def test_get_valid
c = Country.get('GB')
assert c
assert_equal('GB', c.code)
end
def test_get_not_exist
assert_raises(InvalidCountryCode) {
Country.get('ZZ')
}
end
def test_get_invalid
assert_raises(InvalidCountryCode) {
Country.get('../Countries/GB')
}
end
def test_get_nil
assert_raises(InvalidCountryCode) {
Country.get(nil)
}
end
def test_get_case
assert_raises(InvalidCountryCode) {
Country.get('gb')
}
end
def test_get_tainted_loaded
Country.get('GB')
safe_test(:unavailable => :skip) do
code = 'GB'.dup.taint
assert(code.tainted?)
country = Country.get(code)
assert_equal('GB', country.code)
assert(code.tainted?)
end
end
def test_get_tainted_and_frozen_loaded
Country.get('GB')
safe_test do
country = Country.get('GB'.dup.taint.freeze)
assert_equal('GB', country.code)
end
end
def test_get_tainted_not_previously_loaded
safe_test(:unavailable => :skip) do
code = 'GB'.dup.taint
assert(code.tainted?)
country = Country.get(code)
assert_equal('GB', country.code)
assert(code.tainted?)
end
end
def test_get_tainted_and_frozen_not_previously_loaded
safe_test do
country = Country.get('GB'.dup.taint.freeze)
assert_equal('GB', country.code)
end
end
def test_new_nil
assert_raises(InvalidCountryCode) {
Country.new(nil)
}
end
def test_new_arg
c = Country.new('GB')
assert_same(Country.get('GB'), c)
end
def test_new_arg_not_exist
assert_raises(InvalidCountryCode) {
Country.new('ZZ')
}
end
def test_all_codes
all_codes = Country.all_codes
assert_kind_of(Array, all_codes)
end
def test_all
all = Country.all
assert_equal(Country.all_codes, all.collect {|c| c.code})
end
def test_code
assert_equal('US', Country.get('US').code)
end
def test_name
assert_kind_of(String, Country.get('US').name)
end
def test_to_s
assert_equal(Country.get('US').name, Country.get('US').to_s)
assert_equal(Country.get('GB').name, Country.get('GB').to_s)
end
def test_zone_identifiers
zone_names = Country.get('US').zone_names
assert_kind_of(Array, zone_names)
assert_equal(true, zone_names.frozen?)
end
def test_zone_names
assert_equal(Country.get('US').zone_identifiers, Country.get('US').zone_names)
end
def test_zones
zones = Country.get('US').zones
assert_kind_of(Array, zones)
assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.identifier})
zones.each {|z| assert_kind_of(TimezoneProxy, z)}
end
def test_zone_info
zones = Country.get('US').zone_info
assert_kind_of(Array, zones)
assert_equal(true, zones.frozen?)
assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.identifier})
assert_equal(Country.get('US').zone_identifiers, zones.collect {|z| z.timezone.identifier})
zones.each {|z| assert_kind_of(CountryTimezone, z)}
end
def test_compare
assert_equal(0, Country.get('GB') <=> Country.get('GB'))
assert_equal(-1, Country.get('GB') <=> Country.get('US'))
assert_equal(1, Country.get('US') <=> Country.get('GB'))
assert_equal(-1, Country.get('FR') <=> Country.get('US'))
assert_equal(1, Country.get('US') <=> Country.get('FR'))
end
def test_compare_non_comparable
assert_nil(Country.get('GB') <=> Object.new)
end
def test_equality
assert_equal(true, Country.get('GB') == Country.get('GB'))
assert_equal(false, Country.get('GB') == Country.get('US'))
assert(!(Country.get('GB') == Object.new))
end
def test_eql
assert_equal(true, Country.get('GB').eql?(Country.get('GB')))
assert_equal(false, Country.get('GB').eql?(Country.get('US')))
assert(!Country.get('GB').eql?(Object.new))
end
def test_hash
assert_equal('GB'.hash, Country.get('GB').hash)
assert_equal('US'.hash, Country.get('US').hash)
end
def test_marshal
c = Country.get('US')
# Should get back the same instance because load calls Country.get.
assert_same(c, Marshal.load(Marshal.dump(c)))
end
def test_reload
# If country gets reloaded for some reason, it needs to force a reload of
# the country index.
assert_equal('US', Country.get('US').code)
# Suppress redefined method warnings.
without_warnings do
load 'tzinfo/country.rb'
end
assert_equal('US', Country.get('US').code)
end
def test_get_missing_data_source
DataSource.set(DataSource.new)
assert_raises(InvalidDataSource) do
Country.get('GB')
end
end
def test_new_missing_data_source
DataSource.set(DataSource.new)
assert_raises(InvalidDataSource) do
Country.new('GB')
end
end
def test_all_codes_missing_data_source
DataSource.set(DataSource.new)
assert_raises(InvalidDataSource) do
Country.all_codes
end
end
def test_all_missing_data_source
DataSource.set(DataSource.new)
assert_raises(InvalidDataSource) do
Country.all
end
end
end