countries 5.7.0 → 5.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/countries/cache/countries.json +1 -1
- data/lib/countries/cache/locales/ja.json +1 -1
- data/lib/countries/country/class_methods.rb +1 -1
- data/lib/countries/country/country_subdivision_methods.rb +2 -2
- data/lib/countries/country/emoji.rb +1 -1
- data/lib/countries/country/finder_methods.rb +1 -1
- data/lib/countries/country.rb +2 -2
- data/lib/countries/data/countries/CZ.yaml +1 -3
- data/lib/countries/data/countries/LB.yaml +1 -1
- data/lib/countries/data/countries/PS.yaml +2 -0
- data/lib/countries/data/subdivisions/IN.yaml +9 -9
- data/lib/countries/data/subdivisions/IR.yaml +1 -1
- data/lib/countries/data/translations/countries-ja.yaml +1 -1
- data/lib/countries/version.rb +1 -1
- metadata +2 -28
- data/.codeclimate.yml +0 -61
- data/.github/FUNDING.yml +0 -1
- data/.github/workflows/codeql-analysis.yml +0 -70
- data/.github/workflows/tests.yml +0 -32
- data/.gitignore +0 -25
- data/.rspec +0 -2
- data/.rubocop.yml +0 -49
- data/CHANGELOG.md +0 -776
- data/Gemfile +0 -15
- data/Gemfile.lock +0 -102
- data/LICENSE +0 -24
- data/README.md +0 -404
- data/Rakefile +0 -62
- data/UPGRADE.md +0 -39
- data/bin/console +0 -15
- data/countries.gemspec +0 -31
- data/spec/00_global_spec.rb +0 -58
- data/spec/configuration_spec.rb +0 -60
- data/spec/country_spec.rb +0 -1327
- data/spec/data_spec.rb +0 -260
- data/spec/mongoid_spec.rb +0 -61
- data/spec/perf_spec.rb +0 -74
- data/spec/spec_helper.rb +0 -19
- data/spec/subdivision_spec.rb +0 -42
- data/spec/thread_safety_spec.rb +0 -47
- data/spec/timezone_spec.rb +0 -34
data/spec/country_spec.rb
DELETED
@@ -1,1327 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
NUM_OF_COUNTRIES = 249
|
5
|
-
describe ISO3166::Country do
|
6
|
-
before do
|
7
|
-
ISO3166::Data.reset
|
8
|
-
ISO3166.configuration.enable_currency_extension!
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:country) { ISO3166::Country.search('US') }
|
12
|
-
|
13
|
-
it 'allows to create a country object from a symbol representation of the alpha2 code' do
|
14
|
-
country = described_class.new(:us)
|
15
|
-
expect(country.data).not_to be_nil
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'allows to create a country object from a lowercase alpha2 code' do
|
19
|
-
country = described_class.new('us')
|
20
|
-
expect(country.data).not_to be_nil
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'allows countries to be compared' do
|
24
|
-
c1 = ISO3166::Country.new('US')
|
25
|
-
c2 = ISO3166::Country.new('US')
|
26
|
-
c3 = ISO3166::Country.new('AU')
|
27
|
-
expect(c1).to eq(c2)
|
28
|
-
expect(c1).to_not eq(nil)
|
29
|
-
expect(c1.hash).to eq(c2.hash)
|
30
|
-
expect(c3.hash).to_not eq(c2.hash)
|
31
|
-
|
32
|
-
hsh = {}
|
33
|
-
hsh[c1] = 1
|
34
|
-
hsh[c2] = 2
|
35
|
-
expect(hsh.keys.count).to eq 1
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'should return 3166 number' do
|
39
|
-
expect(country.number).to eq('840')
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should return 3166 alpha2 code' do
|
43
|
-
expect(country.alpha2).to eq('US')
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should return 3166 alpha3 code' do
|
47
|
-
expect(country.alpha3).to eq('USA')
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'should return 3166 iso_short_name' do
|
51
|
-
expect(country.iso_short_name).to eq('United States of America')
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'should return alternate names' do
|
55
|
-
expect(country.unofficial_names).to eq(['United States', 'USA',
|
56
|
-
'Vereinigte Staaten von Amerika',
|
57
|
-
'États-Unis', 'Estados Unidos',
|
58
|
-
'アメリカ合衆国', 'Verenigde Staten',
|
59
|
-
'Соединенные Штаты Америки'])
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'should return translations' do
|
63
|
-
expect(country.translations).to be
|
64
|
-
expect(country.translations['en']).to eq('United States')
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'should return latitude' do
|
68
|
-
expect(country.latitude).to eq(37.09024)
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'should return longitude' do
|
72
|
-
expect(country.longitude).to eq(-95.712891)
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'should return bounds' do
|
76
|
-
expect(country.bounds['northeast']['lat']).to eq(71.3577635769)
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'should return continent' do
|
80
|
-
expect(country.continent).to eq('North America')
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'should return distance unit' do
|
84
|
-
expect(country.distance_unit).to eq('MI')
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'knows about whether or not the country uses postal codes' do
|
88
|
-
expect(country.zip).to be_truthy
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'knows when a country does not require postal codes' do
|
92
|
-
antarctica = ISO3166::Country.search('AQ')
|
93
|
-
expect(antarctica.postal_code).to eq(false)
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'knows about the country postal code format' do
|
97
|
-
expect(country.postal_code_format).to_not be_nil
|
98
|
-
|
99
|
-
regex = Regexp.new(country.postal_code_format)
|
100
|
-
expect(regex).to match('12345-6789')
|
101
|
-
expect(regex).not_to match('12345-67890')
|
102
|
-
|
103
|
-
antarctica = ISO3166::Country.search('AQ')
|
104
|
-
expect(antarctica.postal_code_format).to be_nil
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'should return region' do
|
108
|
-
expect(country.region).to eq('Americas')
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'should return subregion' do
|
112
|
-
expect(country.subregion).to eq('Northern America')
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'should return world region' do
|
116
|
-
expect(country.world_region).to eq('AMER')
|
117
|
-
end
|
118
|
-
|
119
|
-
context 'with Türkiye' do
|
120
|
-
let(:country) { ISO3166::Country.search('TR') }
|
121
|
-
|
122
|
-
it 'should indicate EMEA as the world region' do
|
123
|
-
expect(country.world_region).to eq('EMEA')
|
124
|
-
end
|
125
|
-
|
126
|
-
it 'has iso_short_name Türkiye' do
|
127
|
-
expect(country.iso_short_name).to eq('Türkiye')
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'has iso_long_name Republic of Türkiye' do
|
131
|
-
expect(country.iso_long_name).to eq('The Republic of Türkiye')
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
context 'with Japan' do
|
136
|
-
let(:country) { ISO3166::Country.search('JP') }
|
137
|
-
|
138
|
-
it 'should indicate APAC as the world region' do
|
139
|
-
expect(country.world_region).to eq('APAC')
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
context 'with Belgium' do
|
144
|
-
let(:country) { ISO3166::Country.search('BE') }
|
145
|
-
|
146
|
-
it 'should return its local names based on its languages' do
|
147
|
-
expect(country.local_names).to match_array(%w[België Belgique Belgien])
|
148
|
-
end
|
149
|
-
|
150
|
-
it 'should return its first local name' do
|
151
|
-
expect(country.local_name).to eq('België')
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
context 'with Brazil' do
|
156
|
-
context 'with pt-BR translation' do
|
157
|
-
before do
|
158
|
-
ISO3166::Data.register(
|
159
|
-
alpha2: 'BR2',
|
160
|
-
iso_short_name: 'Brazil',
|
161
|
-
languages_official: %w[pt-BR],
|
162
|
-
translations: {
|
163
|
-
'pt-BR' => 'Translation for pt-BR',
|
164
|
-
'pt' => 'Translation for pt'
|
165
|
-
}
|
166
|
-
)
|
167
|
-
end
|
168
|
-
|
169
|
-
let(:country) { ISO3166::Country.search('BR2') }
|
170
|
-
|
171
|
-
it 'should return its local name based on its language' do
|
172
|
-
expect(country.local_names).to match_array(['Translation for pt-BR'])
|
173
|
-
end
|
174
|
-
|
175
|
-
after do
|
176
|
-
ISO3166::Data.unregister('BR2')
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
context 'without pt-BR translation' do
|
181
|
-
before do
|
182
|
-
ISO3166::Data.register(
|
183
|
-
alpha2: 'BR2',
|
184
|
-
iso_short_name: 'Brazil',
|
185
|
-
languages_official: %w[pt-BR],
|
186
|
-
translations: {
|
187
|
-
'pt' => 'Translation for pt'
|
188
|
-
}
|
189
|
-
)
|
190
|
-
end
|
191
|
-
|
192
|
-
let(:country) { ISO3166::Country.search('BR2') }
|
193
|
-
|
194
|
-
it 'should return its local name based on its language' do
|
195
|
-
expect(country.local_names).to match_array(['Translation for pt'])
|
196
|
-
end
|
197
|
-
|
198
|
-
after do
|
199
|
-
ISO3166::Data.unregister('BR2')
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
it 'should return ioc code' do
|
205
|
-
expect(country.ioc).to eq('USA')
|
206
|
-
end
|
207
|
-
|
208
|
-
it 'should return UN/LOCODE' do
|
209
|
-
expect(country.un_locode).to eq('US')
|
210
|
-
end
|
211
|
-
|
212
|
-
it 'should be identical to itself' do
|
213
|
-
expect(country).to eq(ISO3166::Country.search('US'))
|
214
|
-
end
|
215
|
-
|
216
|
-
it 'should return language' do
|
217
|
-
expect(country.languages[0]).to eq('en')
|
218
|
-
end
|
219
|
-
|
220
|
-
describe 'e164' do
|
221
|
-
it 'should return country_code' do
|
222
|
-
expect(country.country_code).to eq('1')
|
223
|
-
end
|
224
|
-
|
225
|
-
it 'should return national_destination_code_lengths' do
|
226
|
-
expect(country.national_destination_code_lengths).to eq([3])
|
227
|
-
end
|
228
|
-
|
229
|
-
it 'should return national_number_lengths' do
|
230
|
-
expect(country.national_number_lengths).to eq([10])
|
231
|
-
end
|
232
|
-
|
233
|
-
it 'should return international_prefix' do
|
234
|
-
expect(country.international_prefix).to eq('011')
|
235
|
-
end
|
236
|
-
|
237
|
-
it 'should return national_prefix' do
|
238
|
-
expect(country.national_prefix).to eq('1')
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
describe 'subdivisions' do
|
243
|
-
let(:virginia) { country.subdivisions['VA'] }
|
244
|
-
it 'should return an empty hash for a country with no ISO3166-2' do
|
245
|
-
expect(ISO3166::Country.search('VA').subdivisions.size).to eq(0)
|
246
|
-
end
|
247
|
-
|
248
|
-
it 'should return a hash with all sub divisions' do
|
249
|
-
expect(country.subdivisions.size).to eq(57)
|
250
|
-
end
|
251
|
-
|
252
|
-
it '#states should be deprecated' do
|
253
|
-
expect { country.states }.to output.to_stderr
|
254
|
-
end
|
255
|
-
|
256
|
-
it 'should be a subdivision object' do
|
257
|
-
expect(virginia).to be_a(ISO3166::Subdivision)
|
258
|
-
end
|
259
|
-
|
260
|
-
it 'should have a name' do
|
261
|
-
expect(virginia.name).to eq('Virginia')
|
262
|
-
end
|
263
|
-
|
264
|
-
it 'should have a code' do
|
265
|
-
expect(virginia.code).to eq('VA')
|
266
|
-
end
|
267
|
-
|
268
|
-
it 'should behave like a hash' do
|
269
|
-
expect(virginia['name']).to eq('Virginia')
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
describe 'subdivision_types' do
|
274
|
-
it 'should return an array of subdivision types' do
|
275
|
-
expect(country.subdivision_types).to contain_exactly('district', 'state', 'outlying_area')
|
276
|
-
end
|
277
|
-
|
278
|
-
it 'should return an array of subdivision types even if there is only a single type' do
|
279
|
-
expect(ISO3166::Country['LI'].subdivision_types).to contain_exactly('commune')
|
280
|
-
end
|
281
|
-
|
282
|
-
it 'should return an empty array if country has no subdivisions' do
|
283
|
-
expect(ISO3166::Country['AS'].subdivisions?).to be_falsey
|
284
|
-
expect(ISO3166::Country['AS'].subdivision_types).to eq([])
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
describe 'subdivisions_of_types' do
|
289
|
-
it 'given a single type, should return an array of subdivisions that match the type' do
|
290
|
-
us_states = country.subdivisions_of_types(%w[state])
|
291
|
-
expect(us_states.size).to eq(50)
|
292
|
-
dc = country.subdivisions_of_types(%w[district])
|
293
|
-
expect(dc.size).to eq(1)
|
294
|
-
end
|
295
|
-
|
296
|
-
it 'given multiple types, should return an array of subdivisions matching the types' do
|
297
|
-
us_states_plus_dc = country.subdivisions_of_types(%w[state district])
|
298
|
-
expect(us_states_plus_dc.size).to eq(51)
|
299
|
-
end
|
300
|
-
|
301
|
-
it 'given multiple types where at least one does not exist for that country, should work without issue' do
|
302
|
-
us_states_plus_dc = country.subdivisions_of_types(%w[state district governorate])
|
303
|
-
expect(us_states_plus_dc.size).to eq(51)
|
304
|
-
end
|
305
|
-
|
306
|
-
it 'given only types that do not exist for that country, should return an empty collection' do
|
307
|
-
should_be_empty = ISO3166::Country['PT'].subdivisions_of_types(%w[state county])
|
308
|
-
expect(should_be_empty).to be_empty
|
309
|
-
end
|
310
|
-
end
|
311
|
-
|
312
|
-
describe 'humanized_subdivision_types' do
|
313
|
-
it 'should return an array of humanized subdivision types' do
|
314
|
-
expect(country.humanized_subdivision_types).to contain_exactly('District', 'State', 'Outlying area')
|
315
|
-
end
|
316
|
-
|
317
|
-
it 'should return an array of subdivision types even if there is only a single type' do
|
318
|
-
expect(ISO3166::Country['LI'].humanized_subdivision_types).to contain_exactly('Commune')
|
319
|
-
end
|
320
|
-
|
321
|
-
it 'should return an empty array if country has no subdivisions' do
|
322
|
-
expect(ISO3166::Country['AS'].subdivisions?).to be_falsey
|
323
|
-
expect(ISO3166::Country['AS'].humanized_subdivision_types).to eq([])
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
describe 'subdivision_names_with_codes' do
|
328
|
-
it 'should return an alphabetized list of all subdivisions names with codes' do
|
329
|
-
subdivisions = ISO3166::Country.search('EG').subdivision_names_with_codes
|
330
|
-
expect(subdivisions).to be_an(Array)
|
331
|
-
expect(subdivisions.first[0]).to be_a(String)
|
332
|
-
expect(subdivisions.size).to eq(27)
|
333
|
-
expect(subdivisions.first[0]).to eq('Alexandria')
|
334
|
-
end
|
335
|
-
|
336
|
-
it 'should return an alphabetized list of subdivision names translated to current locale with codes' do
|
337
|
-
ISO3166.configuration.locales = %i[es de en]
|
338
|
-
|
339
|
-
subdivisions = ISO3166::Country.search('EG').subdivision_names_with_codes(:es)
|
340
|
-
expect(subdivisions).to be_an(Array)
|
341
|
-
expect(subdivisions.first[0]).to be_a(String)
|
342
|
-
expect(subdivisions.size).to eq(27)
|
343
|
-
expect(subdivisions.first[0]).to eq('Al Iskandariyah')
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
describe 'subdivision_names' do
|
348
|
-
it 'should return an alphabetized list of all subdivisions names' do
|
349
|
-
subdivisions = ISO3166::Country.search('EG').subdivision_names
|
350
|
-
expect(subdivisions).to be_an(Array)
|
351
|
-
expect(subdivisions.first).to be_a(String)
|
352
|
-
expect(subdivisions.size).to eq(27)
|
353
|
-
expect(subdivisions.first).to eq('Alexandria')
|
354
|
-
end
|
355
|
-
|
356
|
-
it 'should return an alphabetized list of subdivision names translated to current locale with codes' do
|
357
|
-
ISO3166.configuration.locales = %i[es de en]
|
358
|
-
|
359
|
-
subdivisions = ISO3166::Country.search('EG').subdivision_names(:es)
|
360
|
-
expect(subdivisions).to be_an(Array)
|
361
|
-
expect(subdivisions.first).to be_a(String)
|
362
|
-
expect(subdivisions.size).to eq(27)
|
363
|
-
expect(subdivisions.first).to eq('Al Iskandariyah')
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
describe 'valid?' do
|
368
|
-
it 'should return true if country is valid' do
|
369
|
-
expect(ISO3166::Country.new('US')).to be_valid
|
370
|
-
end
|
371
|
-
|
372
|
-
it 'should return false if country is invalid' do
|
373
|
-
expect(ISO3166::Country.new({})).not_to be_valid
|
374
|
-
end
|
375
|
-
end
|
376
|
-
|
377
|
-
describe 'new' do
|
378
|
-
it 'should return new country object when a valid alpha2 string is passed' do
|
379
|
-
expect(ISO3166::Country.new('US')).to be_a(ISO3166::Country)
|
380
|
-
end
|
381
|
-
|
382
|
-
it 'should return nil when an invalid alpha2 string is passed' do
|
383
|
-
expect(ISO3166::Country.new('fubar')).to be_nil
|
384
|
-
end
|
385
|
-
|
386
|
-
it 'should return new country object when a valid alpha2 symbol is passed' do
|
387
|
-
expect(ISO3166::Country.new(:us)).to be_a(ISO3166::Country)
|
388
|
-
end
|
389
|
-
|
390
|
-
it 'should return nil when an invalid alpha2 symbol is passed' do
|
391
|
-
expect(ISO3166::Country.new(:fubar)).to be_nil
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
describe 'compare' do
|
396
|
-
it 'should compare itself with other countries by its name' do
|
397
|
-
canada = ISO3166::Country.search('CA')
|
398
|
-
mexico = ISO3166::Country.search('MX')
|
399
|
-
expect(mexico <=> canada).to eq(1)
|
400
|
-
expect(canada <=> mexico).to eq(-1)
|
401
|
-
end
|
402
|
-
end
|
403
|
-
|
404
|
-
describe 'all' do
|
405
|
-
it 'should return an array list of all countries' do
|
406
|
-
countries = ISO3166::Country.all
|
407
|
-
expect(countries).to be_an(Array)
|
408
|
-
expect(countries.first).to be_an(ISO3166::Country)
|
409
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
410
|
-
end
|
411
|
-
|
412
|
-
it 'should allow to customize each country representation passing a block to the method' do
|
413
|
-
countries = ISO3166::Country.all { |country, data| [data['iso_short_name'], country, data['country_code']] }
|
414
|
-
expect(countries).to be_an(Array)
|
415
|
-
expect(countries.first).to be_an(Array)
|
416
|
-
expect(countries.first.size).to eq(3)
|
417
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
|
-
describe 'all_translated' do
|
422
|
-
it 'should return an alphabetized list of all country names translated to the selected locale' do
|
423
|
-
countries = ISO3166::Country.all_translated('fr')
|
424
|
-
expect(countries).to be_an(Array)
|
425
|
-
expect(countries.first).to be_a(String)
|
426
|
-
expect(countries.first).to eq('Andorre')
|
427
|
-
# countries missing the desired locale will not be added to the list
|
428
|
-
# so all 250 countries may not be returned, 'fr' returns 249, for example
|
429
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
430
|
-
end
|
431
|
-
|
432
|
-
it 'should return an alphabetized list of all country names in English if no locale is passed' do
|
433
|
-
countries = ISO3166::Country.all_translated
|
434
|
-
expect(countries).to be_an(Array)
|
435
|
-
expect(countries.first).to be_a(String)
|
436
|
-
expect(countries.first).to eq('Andorra')
|
437
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
438
|
-
end
|
439
|
-
|
440
|
-
context 'with custom countries' do
|
441
|
-
before do
|
442
|
-
ISO3166::Data.register(
|
443
|
-
alpha2: 'XX',
|
444
|
-
iso_short_name: 'Custom Country',
|
445
|
-
translations: { 'en' => 'Custom Country' }
|
446
|
-
)
|
447
|
-
end
|
448
|
-
|
449
|
-
it 'should include custom registered countries' do
|
450
|
-
custom_country = ISO3166::Country.find_by_alpha2('XX')[1]
|
451
|
-
countries = ISO3166::Country.all_translated
|
452
|
-
expect(countries).to include(custom_country['iso_short_name'])
|
453
|
-
end
|
454
|
-
|
455
|
-
after do
|
456
|
-
ISO3166::Data.unregister('XX')
|
457
|
-
end
|
458
|
-
end
|
459
|
-
end
|
460
|
-
|
461
|
-
describe 'all_names_with_codes' do
|
462
|
-
require 'active_support/core_ext/string/output_safety'
|
463
|
-
it 'should return an alphabetized list of all country names with ISOCODE alpha2' do
|
464
|
-
countries = ISO3166::Country.all_names_with_codes
|
465
|
-
expect(countries).to be_an(Array)
|
466
|
-
expect(countries.first[0]).to be_a(String)
|
467
|
-
expect(countries.first[0]).to eq('Afghanistan')
|
468
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
469
|
-
expect(countries.any? { |pair| !pair[0].html_safe? }).to eq(false)
|
470
|
-
end
|
471
|
-
|
472
|
-
it 'should return an alphabetized list of all country names translated to current locale with ISOCODE alpha2' do
|
473
|
-
ISO3166.configuration.locales = %i[es de en]
|
474
|
-
|
475
|
-
countries = ISO3166::Country.all_names_with_codes(:es)
|
476
|
-
expect(countries).to be_an(Array)
|
477
|
-
expect(countries.first[0]).to be_a(String)
|
478
|
-
expect(countries.first[0]).to eq('Afganistán')
|
479
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
480
|
-
end
|
481
|
-
end
|
482
|
-
|
483
|
-
describe 'all_names_with_codes_without_active_support' do
|
484
|
-
it 'should return an alphabetized list of all country names with ISOCODE alpha2' do
|
485
|
-
countries = ISO3166::Country.all_names_with_codes
|
486
|
-
expect(countries).to be_an(Array)
|
487
|
-
expect(countries.first[0]).to be_a(String)
|
488
|
-
expect(countries.first[0]).to eq('Afghanistan')
|
489
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
490
|
-
end
|
491
|
-
|
492
|
-
it 'should return an alphabetized list of all country names translated to current locale with ISOCODE alpha2' do
|
493
|
-
ISO3166.configuration.locales = %i[es de en]
|
494
|
-
|
495
|
-
countries = ISO3166::Country.all_names_with_codes(:es)
|
496
|
-
expect(countries).to be_an(Array)
|
497
|
-
expect(countries.first[0]).to be_a(String)
|
498
|
-
expect(countries.first[0]).to eq('Afganistán')
|
499
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
500
|
-
end
|
501
|
-
end
|
502
|
-
|
503
|
-
describe 'translation' do
|
504
|
-
it 'should return the localized name for a country to the selected locale' do
|
505
|
-
ISO3166.configuration.locales = %i[es de en]
|
506
|
-
countries = ISO3166::Country.new(:de).translation('de')
|
507
|
-
expect(countries).to be_an(String)
|
508
|
-
expect(countries).to eq('Deutschland')
|
509
|
-
end
|
510
|
-
|
511
|
-
it 'should return the localized name for a country in English' do
|
512
|
-
countries = ISO3166::Country.new(:de).translation
|
513
|
-
expect(countries).to be_an(String)
|
514
|
-
expect(countries).to eq('Germany')
|
515
|
-
end
|
516
|
-
|
517
|
-
it 'should return nil when a translation is not found' do
|
518
|
-
countries = ISO3166::Country.new(:de).translation('xxx')
|
519
|
-
expect(countries).to be_nil
|
520
|
-
end
|
521
|
-
|
522
|
-
context 'should return variant locales' do
|
523
|
-
it 'should return different value for Chinese variants' do
|
524
|
-
ISO3166.configuration.locales = %i[zh-cn zh-hk zh-tw]
|
525
|
-
name_cn = ISO3166::Country['TW'].translation('zh-cn')
|
526
|
-
name_hk = ISO3166::Country['TW'].translation('zh-hk')
|
527
|
-
name_tw = ISO3166::Country['TW'].translation('zh-tw')
|
528
|
-
expect([name_cn, name_hk, name_tw].uniq.size).to eql 3
|
529
|
-
end
|
530
|
-
|
531
|
-
it 'should return different value for Portuguese variants' do
|
532
|
-
ISO3166.configuration.locales = %i[pt pt-br]
|
533
|
-
name_pt = ISO3166::Country['SG'].translation('pt')
|
534
|
-
name_br = ISO3166::Country['SG'].translation('pt-br')
|
535
|
-
expect([name_pt, name_br].uniq.size).to eql 2
|
536
|
-
end
|
537
|
-
end
|
538
|
-
end
|
539
|
-
|
540
|
-
describe 'translations' do
|
541
|
-
it 'should return an hash of all country names translated to the selected locale' do
|
542
|
-
countries = ISO3166::Country.translations('fr')
|
543
|
-
expect(countries).to be_an(Hash)
|
544
|
-
expect(countries.first[0]).to eq('AD')
|
545
|
-
expect(countries.first).to eq(%w[AD Andorre])
|
546
|
-
# countries missing the desired locale will not be added to the list
|
547
|
-
# so all 250 countries may not be returned, 'fr' returns 249, for example
|
548
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
549
|
-
end
|
550
|
-
|
551
|
-
it 'should return an hash of all country names in English if no locale is passed' do
|
552
|
-
countries = ISO3166::Country.translations
|
553
|
-
expect(countries).to be_an(Hash)
|
554
|
-
expect(countries.first[0]).to eq('AD')
|
555
|
-
expect(countries.first).to eq(%w[AD Andorra])
|
556
|
-
expect(countries.size).to eq(NUM_OF_COUNTRIES)
|
557
|
-
end
|
558
|
-
end
|
559
|
-
|
560
|
-
describe 'countries' do
|
561
|
-
it 'should be the same as all' do
|
562
|
-
expect(ISO3166::Country.countries).to eq(ISO3166::Country.all)
|
563
|
-
end
|
564
|
-
end
|
565
|
-
|
566
|
-
describe 'search' do
|
567
|
-
it 'should return new country object when a valid alpha2 string is passed' do
|
568
|
-
expect(ISO3166::Country.search('US')).to be_a(ISO3166::Country)
|
569
|
-
end
|
570
|
-
|
571
|
-
it 'should return nil when an invalid alpha2 string is passed' do
|
572
|
-
expect(ISO3166::Country.search('fubar')).to be_nil
|
573
|
-
end
|
574
|
-
|
575
|
-
it 'should return new country object when a valid alpha2 symbol is passed' do
|
576
|
-
expect(ISO3166::Country.search(:us)).to be_a(ISO3166::Country)
|
577
|
-
end
|
578
|
-
|
579
|
-
it 'should return nil when an invalid alpha2 symbol is passed' do
|
580
|
-
expect(ISO3166::Country.search(:fubar)).to be_nil
|
581
|
-
end
|
582
|
-
end
|
583
|
-
|
584
|
-
describe 'currency' do
|
585
|
-
it 'should return an instance of Currency' do
|
586
|
-
expect(country.currency).to be_a(Money::Currency)
|
587
|
-
end
|
588
|
-
|
589
|
-
it 'should allow access to symbol' do
|
590
|
-
expect(country.currency.symbol).to eq('$')
|
591
|
-
end
|
592
|
-
end
|
593
|
-
|
594
|
-
describe 'codes' do
|
595
|
-
it 'returns a hash with the data of the country' do
|
596
|
-
expect(ISO3166::Country.codes).to be_a Array
|
597
|
-
expect(ISO3166::Country.codes.size).to eq(NUM_OF_COUNTRIES)
|
598
|
-
end
|
599
|
-
end
|
600
|
-
|
601
|
-
describe 'find_all_by' do
|
602
|
-
context 'when searchead attribute equals the given value' do
|
603
|
-
let(:spain_data) { ISO3166::Country.find_all_by('alpha2', 'ES') }
|
604
|
-
|
605
|
-
it 'returns a hash with the data of the country' do
|
606
|
-
expect(spain_data).to be_a Hash
|
607
|
-
expect(spain_data.keys.size).to eq(1)
|
608
|
-
end
|
609
|
-
end
|
610
|
-
|
611
|
-
context 'when searchead attribute is list and one of its elements equals the given value' do
|
612
|
-
let(:spain_data) { ISO3166::Country.find_all_by('languages', 'en') }
|
613
|
-
|
614
|
-
it 'returns a hash with the data of the country' do
|
615
|
-
expect(spain_data).to be_a Hash
|
616
|
-
expect(spain_data.size).to be > 1
|
617
|
-
end
|
618
|
-
end
|
619
|
-
|
620
|
-
it 'also finds results if the given values is not upcased/downcased properly' do
|
621
|
-
spain_data = ISO3166::Country.find_all_by('alpha2', 'es')
|
622
|
-
expect(spain_data).to be_a Hash
|
623
|
-
expect(spain_data.keys.size).to eq(1)
|
624
|
-
end
|
625
|
-
|
626
|
-
it 'also finds results if the attribute is given as a symbol' do
|
627
|
-
spain_data = ISO3166::Country.find_all_by(:alpha2, 'ES')
|
628
|
-
expect(spain_data).to be_a Hash
|
629
|
-
expect(spain_data.keys.size).to eq(1)
|
630
|
-
end
|
631
|
-
|
632
|
-
it 'casts the given value to a string to perform the search' do
|
633
|
-
spain_data = ISO3166::Country.find_all_by(:country_code, 34)
|
634
|
-
expect(spain_data).to be_a Hash
|
635
|
-
expect(spain_data.keys).to eq(['ES'])
|
636
|
-
end
|
637
|
-
|
638
|
-
it 'also performs searches with regexps and forces it to ignore case' do
|
639
|
-
spain_data = ISO3166::Country.find_all_by(:unofficial_names, /Españ/)
|
640
|
-
expect(spain_data).to be_a Hash
|
641
|
-
expect(spain_data.keys).to eq(['ES'])
|
642
|
-
end
|
643
|
-
|
644
|
-
it 'finds country from a subdivision name' do
|
645
|
-
gb_data = ISO3166::Country.find_all_by(:subdivision_names, 'Scotland')
|
646
|
-
expect(gb_data).to be_a Hash
|
647
|
-
expect(gb_data.keys).to eq(['GB'])
|
648
|
-
end
|
649
|
-
|
650
|
-
it 'performs reasonably' do
|
651
|
-
start = Time.now
|
652
|
-
250.times do
|
653
|
-
lookup = ['ZM', 'ZMB', 'Zambia', 'US', 'USA', 'United States'].sample
|
654
|
-
case lookup.length
|
655
|
-
when 2 then ISO3166::Country.find_all_by(:alpha2, lookup)
|
656
|
-
when 3 then ISO3166::Country.find_all_by(:alpha3, lookup)
|
657
|
-
else ISO3166::Country.find_all_by(:iso_short_name, lookup)
|
658
|
-
end
|
659
|
-
end
|
660
|
-
expect(Time.now - start).to be < 1
|
661
|
-
end
|
662
|
-
end
|
663
|
-
|
664
|
-
describe 'hash finder methods' do
|
665
|
-
context "when search name in 'iso_short_name'" do
|
666
|
-
subject { ISO3166::Country.find_by_iso_short_name('Poland') }
|
667
|
-
it 'should return' do
|
668
|
-
expect(subject.first).to eq('PL')
|
669
|
-
end
|
670
|
-
end
|
671
|
-
|
672
|
-
context "when search lowercase name in 'iso_short_name'" do
|
673
|
-
subject { ISO3166::Country.find_by_iso_short_name('poland') }
|
674
|
-
it 'should return' do
|
675
|
-
expect(subject.first).to eq('PL')
|
676
|
-
end
|
677
|
-
end
|
678
|
-
|
679
|
-
context "when search name with comma in 'iso_short_name'" do
|
680
|
-
subject { ISO3166::Country.find_by_iso_short_name(country_name) }
|
681
|
-
|
682
|
-
context 'with Republic of Korea' do
|
683
|
-
let(:country_name) { 'Korea, Republic of' }
|
684
|
-
it 'should return' do
|
685
|
-
expect(subject.first).to eq('KR')
|
686
|
-
end
|
687
|
-
end
|
688
|
-
|
689
|
-
context 'with Bolivia' do
|
690
|
-
let(:country_name) { 'Bolivia, Plurinational State of' }
|
691
|
-
it 'should return' do
|
692
|
-
expect(subject.first).to eq('BO')
|
693
|
-
end
|
694
|
-
end
|
695
|
-
|
696
|
-
context 'with Bonaire' do
|
697
|
-
let(:country_name) { 'Bonaire, Sint Eustatius and Saba' }
|
698
|
-
it 'should return' do
|
699
|
-
expect(subject.first).to eq('BQ')
|
700
|
-
end
|
701
|
-
end
|
702
|
-
end
|
703
|
-
|
704
|
-
context 'when search lowercase multibyte name found' do
|
705
|
-
subject { ISO3166::Country.find_country_by_unofficial_names('россия') }
|
706
|
-
|
707
|
-
it 'should be a country instance' do
|
708
|
-
expect(subject).to be_a(ISO3166::Country)
|
709
|
-
expect(subject.alpha2).to eq('RU')
|
710
|
-
end
|
711
|
-
end
|
712
|
-
|
713
|
-
context 'when search lowercase multibyte name found' do
|
714
|
-
subject { ISO3166::Country.find_country_by_unofficial_names(/россия/) }
|
715
|
-
|
716
|
-
it 'should be a country instance' do
|
717
|
-
expect(subject).to be_a(ISO3166::Country)
|
718
|
-
expect(subject.alpha2).to eq('RU')
|
719
|
-
end
|
720
|
-
end
|
721
|
-
|
722
|
-
context 'when accents are not used' do
|
723
|
-
subject { ISO3166::Country.find_country_by_unofficial_names('emirats Arabes Unis') }
|
724
|
-
|
725
|
-
it 'should be a country instance' do
|
726
|
-
expect(subject).to be_a(ISO3166::Country)
|
727
|
-
expect(subject.alpha2).to eq('AE')
|
728
|
-
end
|
729
|
-
end
|
730
|
-
|
731
|
-
context "when search name in 'unofficial_names'" do
|
732
|
-
subject { ISO3166::Country.find_by_unofficial_names('Polonia') }
|
733
|
-
it 'should return' do
|
734
|
-
expect(subject.first).to eq('PL')
|
735
|
-
end
|
736
|
-
end
|
737
|
-
|
738
|
-
context "when search name in 'subdivision_names'" do
|
739
|
-
subject { ISO3166::Country.find_by_subdivision_names('Scotland') }
|
740
|
-
it 'should return' do
|
741
|
-
expect(subject.first).to eq('GB')
|
742
|
-
end
|
743
|
-
end
|
744
|
-
|
745
|
-
context "when search name in 'translated_names'" do
|
746
|
-
before do
|
747
|
-
ISO3166.configure do |config|
|
748
|
-
config.locales = [:bs]
|
749
|
-
end
|
750
|
-
end
|
751
|
-
subject { ISO3166::Country.find_by_translated_names('Poljska') }
|
752
|
-
it 'should return' do
|
753
|
-
expect(subject.first).to eq('PL')
|
754
|
-
end
|
755
|
-
end
|
756
|
-
|
757
|
-
context 'when finding by invalid attribute' do
|
758
|
-
it 'should raise an error' do
|
759
|
-
expect { ISO3166::Country.find_by_invalid('invalid') }.to(
|
760
|
-
raise_error(RuntimeError, "Invalid attribute name 'invalid'")
|
761
|
-
)
|
762
|
-
end
|
763
|
-
end
|
764
|
-
|
765
|
-
context 'when using find_all method' do
|
766
|
-
let(:list) { ISO3166::Country.find_all_by_currency('USD') }
|
767
|
-
|
768
|
-
it 'should be an Array of Arrays' do
|
769
|
-
expect(list).to be_a(Array)
|
770
|
-
expect(list.first).to be_a(Array)
|
771
|
-
end
|
772
|
-
end
|
773
|
-
|
774
|
-
context 'when using find_by method' do
|
775
|
-
subject { ISO3166::Country.find_by_alpha3('CAN') }
|
776
|
-
it 'should return' do
|
777
|
-
expect(subject.length).to eq 2
|
778
|
-
expect(subject.first).to be_a(String)
|
779
|
-
expect(subject.last).to be_a(Hash)
|
780
|
-
end
|
781
|
-
end
|
782
|
-
end
|
783
|
-
|
784
|
-
describe 'country finder methods' do
|
785
|
-
context 'when search name found' do
|
786
|
-
let(:uk) { ISO3166::Country.find_country_by_unofficial_names('United Kingdom') }
|
787
|
-
|
788
|
-
it 'should be a country instance' do
|
789
|
-
expect(uk).to be_a(ISO3166::Country)
|
790
|
-
expect(uk.alpha2).to eq('GB')
|
791
|
-
end
|
792
|
-
end
|
793
|
-
|
794
|
-
context 'when search lowercase name found' do
|
795
|
-
let(:uk) { ISO3166::Country.find_country_by_unofficial_names('united kingdom') }
|
796
|
-
|
797
|
-
it 'should be a country instance' do
|
798
|
-
expect(uk).to be_a(ISO3166::Country)
|
799
|
-
expect(uk.alpha2).to eq('GB')
|
800
|
-
end
|
801
|
-
end
|
802
|
-
|
803
|
-
context 'when the search term contains comma' do
|
804
|
-
let(:korea) { ISO3166::Country.find_country_by_unofficial_names('Korea, Republic of') }
|
805
|
-
|
806
|
-
it 'should be a country instance' do
|
807
|
-
expect(korea).to be_a(ISO3166::Country)
|
808
|
-
expect(korea.alpha2).to eq('KR')
|
809
|
-
end
|
810
|
-
end
|
811
|
-
|
812
|
-
context 'when search translation found' do
|
813
|
-
before do
|
814
|
-
ISO3166.configure do |config|
|
815
|
-
config.locales = [:bs]
|
816
|
-
end
|
817
|
-
end
|
818
|
-
let(:uk) { ISO3166::Country.find_country_by_translated_names('Velika Britanija') }
|
819
|
-
|
820
|
-
it 'should be a country instance' do
|
821
|
-
expect(uk).to be_a(ISO3166::Country)
|
822
|
-
expect(uk.alpha2).to eq('GB')
|
823
|
-
end
|
824
|
-
end
|
825
|
-
|
826
|
-
describe '#find_country_by_any_name' do
|
827
|
-
context 'when search name found' do
|
828
|
-
let(:uk) { ISO3166::Country.find_country_by_any_name('United Kingdom') }
|
829
|
-
|
830
|
-
it 'should be a country instance' do
|
831
|
-
expect(uk).to be_a(ISO3166::Country)
|
832
|
-
expect(uk.alpha2).to eq('GB')
|
833
|
-
end
|
834
|
-
end
|
835
|
-
|
836
|
-
context 'when search lowercase name found' do
|
837
|
-
let(:uk) { ISO3166::Country.find_country_by_any_name('united kingdom') }
|
838
|
-
|
839
|
-
it 'should be a country instance' do
|
840
|
-
expect(uk).to be_a(ISO3166::Country)
|
841
|
-
expect(uk.alpha2).to eq('GB')
|
842
|
-
end
|
843
|
-
end
|
844
|
-
|
845
|
-
context 'when the search term contains comma' do
|
846
|
-
let(:korea) { ISO3166::Country.find_country_by_any_name('Korea, Republic of') }
|
847
|
-
|
848
|
-
it 'should be a country instance' do
|
849
|
-
expect(korea).to be_a(ISO3166::Country)
|
850
|
-
expect(korea.alpha2).to eq('KR')
|
851
|
-
end
|
852
|
-
end
|
853
|
-
|
854
|
-
context 'when search translation found' do
|
855
|
-
before do
|
856
|
-
ISO3166.configure do |config|
|
857
|
-
config.locales = [:bs]
|
858
|
-
end
|
859
|
-
end
|
860
|
-
let(:uk) { ISO3166::Country.find_country_by_any_name('Velika Britanija') }
|
861
|
-
|
862
|
-
it 'should be a country instance' do
|
863
|
-
expect(uk).to be_a(ISO3166::Country)
|
864
|
-
expect(uk.alpha2).to eq('GB')
|
865
|
-
end
|
866
|
-
end
|
867
|
-
end
|
868
|
-
|
869
|
-
context 'sanity check for #771' do
|
870
|
-
let(:turkey) { ISO3166::Country.find_country_by_any_name('Turkey') }
|
871
|
-
|
872
|
-
it 'should be a country instance' do
|
873
|
-
expect(turkey).to be_a(ISO3166::Country)
|
874
|
-
expect(turkey.alpha2).to eq('TR')
|
875
|
-
end
|
876
|
-
end
|
877
|
-
|
878
|
-
context 'regression test for #746' do
|
879
|
-
let(:no_country) { ISO3166::Country.find_country_by_any_name(nil) }
|
880
|
-
|
881
|
-
it 'should not be a country instance' do
|
882
|
-
expect(no_country).to_not be_a(ISO3166::Country)
|
883
|
-
expect(no_country).to eq nil
|
884
|
-
end
|
885
|
-
end
|
886
|
-
|
887
|
-
context 'regression test for #388/#746/#776' do
|
888
|
-
before do
|
889
|
-
ISO3166.configure do |config|
|
890
|
-
config.locales = %i[af am ar as az be bg bn br bs ca cs cy da de dz el en
|
891
|
-
eo es et eu fa fi fo fr ga gl gu he hi hr hu hy ia id
|
892
|
-
is it ja ka kk km kn ko ku lt lv mi mk ml mn mr ms mt
|
893
|
-
nb ne nl nn oc or pa pl ps pt ro ru rw si sk sl so sq
|
894
|
-
sr sv sw ta te th ti tk tl tr tt ug uk ve vi wa wo xh
|
895
|
-
zh zu]
|
896
|
-
end
|
897
|
-
end
|
898
|
-
|
899
|
-
let(:no_country) { ISO3166::Country.find_country_by_translated_names(nil) }
|
900
|
-
let(:zimbabwe) { ISO3166::Country['ZW'] }
|
901
|
-
|
902
|
-
it 'should not be a country instance' do
|
903
|
-
expect(no_country).to_not be_a(ISO3166::Country)
|
904
|
-
expect(no_country).to eq nil
|
905
|
-
end
|
906
|
-
|
907
|
-
it 'translated_names should not include nil values' do
|
908
|
-
expect(zimbabwe.translation('no')).to be_nil
|
909
|
-
expect(zimbabwe.translated_names).not_to include(nil)
|
910
|
-
end
|
911
|
-
end
|
912
|
-
|
913
|
-
context 'when attempting to search by translations hash' do
|
914
|
-
let(:uk) { ISO3166::Country.find_country_by_translations({}) }
|
915
|
-
|
916
|
-
it 'should be a country instance' do
|
917
|
-
expect { uk }.to raise_error(RuntimeError)
|
918
|
-
end
|
919
|
-
end
|
920
|
-
|
921
|
-
context 'when search name not found' do
|
922
|
-
let(:bogus) { ISO3166::Country.find_country_by_unofficial_names('Does not exist') }
|
923
|
-
|
924
|
-
it 'should be a country instance' do
|
925
|
-
expect(bogus).to eq(nil)
|
926
|
-
end
|
927
|
-
end
|
928
|
-
|
929
|
-
# Spot checks #243
|
930
|
-
context 'when search name not found' do
|
931
|
-
let(:belgium) { ISO3166::Country.find_country_by_unofficial_names('Belgium') }
|
932
|
-
|
933
|
-
it 'should be a country instance' do
|
934
|
-
expect(belgium.alpha2).to eq('BE')
|
935
|
-
end
|
936
|
-
end
|
937
|
-
|
938
|
-
# Spot checks #240
|
939
|
-
context 'when search name not found' do
|
940
|
-
let(:canada) { ISO3166::Country.find_country_by_unofficial_names('Canada') }
|
941
|
-
|
942
|
-
it 'should be a country instance' do
|
943
|
-
expect(canada.alpha2).to eq('CA')
|
944
|
-
end
|
945
|
-
end
|
946
|
-
|
947
|
-
# Spot checks #241
|
948
|
-
context 'when search name not found' do
|
949
|
-
let(:israel) { ISO3166::Country.find_country_by_unofficial_names('Israel') }
|
950
|
-
|
951
|
-
it 'should be a country instance' do
|
952
|
-
expect(israel.alpha2).to eq('IL')
|
953
|
-
end
|
954
|
-
end
|
955
|
-
|
956
|
-
# Spot checks #241
|
957
|
-
context 'when search name not found' do
|
958
|
-
let(:israel) { ISO3166::Country.find_by_iso_short_name('Israel') }
|
959
|
-
|
960
|
-
it 'should be a country instance' do
|
961
|
-
expect(israel[0]).to eq('IL')
|
962
|
-
end
|
963
|
-
end
|
964
|
-
|
965
|
-
# Spot checks #241
|
966
|
-
context 'when search name not found' do
|
967
|
-
let(:israel) { ISO3166::Country.find_all_by(:iso_short_name, 'Israel') }
|
968
|
-
|
969
|
-
it 'should be a country instance' do
|
970
|
-
expect(israel.size).to eq(1)
|
971
|
-
expect(israel.first[0]).to eq('IL')
|
972
|
-
end
|
973
|
-
end
|
974
|
-
|
975
|
-
context 'when finding by invalid attribute' do
|
976
|
-
it 'should raise an error' do
|
977
|
-
expect { ISO3166::Country.find_country_by_invalid('invalid') }.to(
|
978
|
-
raise_error(RuntimeError, "Invalid attribute name 'invalid'")
|
979
|
-
)
|
980
|
-
end
|
981
|
-
end
|
982
|
-
|
983
|
-
context 'when using find_all method' do
|
984
|
-
let(:list) { ISO3166::Country.find_all_countries_by_currency('USD') }
|
985
|
-
|
986
|
-
it 'should be an Array of Country objects' do
|
987
|
-
expect(list).to be_a(Array)
|
988
|
-
expect(list.first).to be_a(ISO3166::Country)
|
989
|
-
end
|
990
|
-
end
|
991
|
-
|
992
|
-
context 'when using find_by method' do
|
993
|
-
let(:country) { ISO3166::Country.find_country_by_alpha3('CAN') }
|
994
|
-
|
995
|
-
it 'should be a single country object' do
|
996
|
-
expect(country).to be_a(ISO3166::Country)
|
997
|
-
end
|
998
|
-
end
|
999
|
-
end
|
1000
|
-
|
1001
|
-
describe 'finder methods respond_to_missing?' do
|
1002
|
-
subject { ISO3166::Country.respond_to?(method_name) }
|
1003
|
-
describe 'find_all_by' do
|
1004
|
-
context 'find by a valid Country attribute' do
|
1005
|
-
let(:method_name) { :find_all_by_currency }
|
1006
|
-
it { is_expected.to be true }
|
1007
|
-
end
|
1008
|
-
|
1009
|
-
context 'find by an invalid attribute' do
|
1010
|
-
let(:method_name) { :find_all_by_invalid }
|
1011
|
-
it { is_expected.to be false }
|
1012
|
-
end
|
1013
|
-
end
|
1014
|
-
|
1015
|
-
describe 'hash finder methods' do
|
1016
|
-
context 'find by a valid Country attribute' do
|
1017
|
-
let(:method_name) { :find_by_iso_short_name }
|
1018
|
-
it { is_expected.to be true }
|
1019
|
-
end
|
1020
|
-
|
1021
|
-
context 'find by an invalid attribute' do
|
1022
|
-
let(:method_name) { :find_by_invalid }
|
1023
|
-
it { is_expected.to be false }
|
1024
|
-
end
|
1025
|
-
end
|
1026
|
-
|
1027
|
-
describe 'country finder methods' do
|
1028
|
-
context 'find country by a valid Country attribute' do
|
1029
|
-
let(:method_name) { :find_country_by_alpha3 }
|
1030
|
-
it { is_expected.to be true }
|
1031
|
-
end
|
1032
|
-
|
1033
|
-
context 'find by an invalid attribute' do
|
1034
|
-
let(:method_name) { :find_country_by_invalid }
|
1035
|
-
it { is_expected.to be false }
|
1036
|
-
end
|
1037
|
-
end
|
1038
|
-
end
|
1039
|
-
|
1040
|
-
describe 'Norway' do
|
1041
|
-
let(:norway) { ISO3166::Country.search('NO') }
|
1042
|
-
|
1043
|
-
it 'should have a currency' do
|
1044
|
-
expect(norway.currency).to be_a(Money::Currency)
|
1045
|
-
end
|
1046
|
-
end
|
1047
|
-
|
1048
|
-
describe 'Guernsey' do
|
1049
|
-
let(:guernsey) { ISO3166::Country.search('GG') }
|
1050
|
-
|
1051
|
-
it 'should have a currency' do
|
1052
|
-
expect(guernsey.currency.iso_code).to eq('GBP')
|
1053
|
-
end
|
1054
|
-
end
|
1055
|
-
|
1056
|
-
describe 'Languages' do
|
1057
|
-
let(:german_speaking_countries) { ISO3166::Country.find_all_countries_by_languages('de') }
|
1058
|
-
|
1059
|
-
it 'should find countries by language' do
|
1060
|
-
expect(german_speaking_countries.size).to eq(6)
|
1061
|
-
end
|
1062
|
-
end
|
1063
|
-
|
1064
|
-
describe 'in_eu?' do
|
1065
|
-
let(:netherlands) { ISO3166::Country.search('NL') }
|
1066
|
-
|
1067
|
-
it 'should return false for countries without eu_member flag' do
|
1068
|
-
expect(country.in_eu?).to be_falsey
|
1069
|
-
end
|
1070
|
-
|
1071
|
-
it 'should return true for countries with eu_member flag set to true' do
|
1072
|
-
expect(netherlands.in_eu?).to be_truthy
|
1073
|
-
end
|
1074
|
-
end
|
1075
|
-
|
1076
|
-
describe 'in_eea?' do
|
1077
|
-
let(:netherlands) { ISO3166::Country.search('NL') }
|
1078
|
-
|
1079
|
-
it 'should return false for countries without eea_member flag' do
|
1080
|
-
expect(country.in_eea?).to be_falsey
|
1081
|
-
end
|
1082
|
-
|
1083
|
-
it 'should return true for countries with eea_member flag set to true' do
|
1084
|
-
expect(netherlands.in_eea?).to be_truthy
|
1085
|
-
end
|
1086
|
-
end
|
1087
|
-
|
1088
|
-
describe 'gdpr_compliant?' do
|
1089
|
-
let(:united_kigndom) { ISO3166::Country.search('GB') }
|
1090
|
-
let(:france) { ISO3166::Country.search('FR') }
|
1091
|
-
let(:mexico) { ISO3166::Country.search('MX') }
|
1092
|
-
|
1093
|
-
it 'should return false for countries without eea_member flag' do
|
1094
|
-
expect(mexico.gdpr_compliant?).to be_falsey
|
1095
|
-
end
|
1096
|
-
|
1097
|
-
it 'should return true for countries with eea_member flag set to true' do
|
1098
|
-
expect(france.gdpr_compliant?).to be_truthy
|
1099
|
-
end
|
1100
|
-
|
1101
|
-
it 'should return true for UK' do
|
1102
|
-
expect(united_kigndom.gdpr_compliant?).to be_truthy
|
1103
|
-
end
|
1104
|
-
end
|
1105
|
-
|
1106
|
-
describe 'in_esm?' do
|
1107
|
-
let(:netherlands) { ISO3166::Country.search('NL') }
|
1108
|
-
let(:switzerland) { ISO3166::Country.search('CH') }
|
1109
|
-
|
1110
|
-
it 'should return false for countries without esm_member or eea_member flag' do
|
1111
|
-
expect(country.in_esm?).to be_falsey
|
1112
|
-
end
|
1113
|
-
|
1114
|
-
it 'should return true for countries with eea_member flag set to true' do
|
1115
|
-
expect(netherlands.in_esm?).to be_truthy
|
1116
|
-
end
|
1117
|
-
|
1118
|
-
it 'should return true for countries with esm_member flag set to true' do
|
1119
|
-
expect(switzerland.in_esm?).to be_truthy
|
1120
|
-
end
|
1121
|
-
end
|
1122
|
-
|
1123
|
-
describe 'in_eu_vat?' do
|
1124
|
-
let(:netherlands) { ISO3166::Country.search('NL') }
|
1125
|
-
let(:guadeloupe) { ISO3166::Country.search('GP') }
|
1126
|
-
let(:monaco) { ISO3166::Country.search('MC') }
|
1127
|
-
|
1128
|
-
it 'should return false for countries without euvat_member or eu_member flag' do
|
1129
|
-
expect(country.in_eu_vat?).to be_falsey
|
1130
|
-
end
|
1131
|
-
|
1132
|
-
it 'should return true for countries with eu_member flag set to true' do
|
1133
|
-
expect(netherlands.in_eu_vat?).to be_truthy
|
1134
|
-
end
|
1135
|
-
|
1136
|
-
it 'should return false for countries with euvat_member flag set to false' do
|
1137
|
-
expect(guadeloupe.in_eu_vat?).to be_falsey
|
1138
|
-
end
|
1139
|
-
|
1140
|
-
it 'should return true for countries with euvat_member flag set to true' do
|
1141
|
-
expect(monaco.in_eu_vat?).to be_truthy
|
1142
|
-
end
|
1143
|
-
end
|
1144
|
-
|
1145
|
-
describe 'gec' do
|
1146
|
-
it 'should return the country\'s GEC code' do
|
1147
|
-
expect(ISO3166::Country.new('NA').gec).to eql 'WA'
|
1148
|
-
end
|
1149
|
-
|
1150
|
-
it 'should return nil if the country does not have a GEC code' do
|
1151
|
-
expect(ISO3166::Country.new('UM').gec).to eql nil
|
1152
|
-
end
|
1153
|
-
end
|
1154
|
-
|
1155
|
-
describe 'to_s' do
|
1156
|
-
it 'should return the country iso_short_name' do
|
1157
|
-
expect(ISO3166::Country.new('GB').to_s).to eq('United Kingdom of Great Britain and Northern Ireland')
|
1158
|
-
end
|
1159
|
-
end
|
1160
|
-
|
1161
|
-
describe 'VAT rates' do
|
1162
|
-
let(:belgium) { ISO3166::Country.search('BE') }
|
1163
|
-
|
1164
|
-
it 'should not return a vat_rate for countries without federal VAT' do
|
1165
|
-
expect(country.vat_rates).to eq(nil)
|
1166
|
-
end
|
1167
|
-
|
1168
|
-
it 'should contain all keys for vat_rates' do
|
1169
|
-
expect(belgium.vat_rates).to be_a(Hash)
|
1170
|
-
expect(belgium.vat_rates.keys).to eq(%w[standard reduced super_reduced parking])
|
1171
|
-
end
|
1172
|
-
|
1173
|
-
it 'should return an array of reduced vat rates' do
|
1174
|
-
expect(belgium.vat_rates['reduced']).to be_an(Array)
|
1175
|
-
expect(belgium.vat_rates['reduced']).to eq([6, 12])
|
1176
|
-
end
|
1177
|
-
end
|
1178
|
-
|
1179
|
-
describe 'ISO3166::Country()' do
|
1180
|
-
it 'should return same object if instance of ISO3166::Country given' do
|
1181
|
-
expect(ISO3166::Country(country)).to eq country
|
1182
|
-
end
|
1183
|
-
|
1184
|
-
it 'should return country if instance of String given' do
|
1185
|
-
expect(ISO3166::Country('us')).to eq country
|
1186
|
-
end
|
1187
|
-
|
1188
|
-
it 'should return country if not convertable input given' do
|
1189
|
-
expect do
|
1190
|
-
ISO3166::Country(42)
|
1191
|
-
end.to raise_error(TypeError, /can't convert ([A-z]+) into ISO3166::Country/)
|
1192
|
-
end
|
1193
|
-
end
|
1194
|
-
|
1195
|
-
describe 'Added country names to search by' do
|
1196
|
-
it 'should return country code for Democratic Republic of the Congo' do
|
1197
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('Democratic Republic of the Congo').alpha2).to eq 'CD'
|
1198
|
-
end
|
1199
|
-
it 'should return country code for Ivory Coast' do
|
1200
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('Ivory Coast').alpha2).to eq 'CI'
|
1201
|
-
end
|
1202
|
-
it 'should return Pakistan code for Guinea Bissau' do
|
1203
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('Guinea Bissau').alpha2).to eq 'GW'
|
1204
|
-
end
|
1205
|
-
it 'should return Pakistan code for St Kitts and Nevis' do
|
1206
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('St Kitts and Nevis').alpha2).to eq 'KN'
|
1207
|
-
end
|
1208
|
-
it 'should return Pakistan code for St Lucia' do
|
1209
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('St Lucia').alpha2).to eq 'LC'
|
1210
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('St. Lucia').alpha2).to eq 'LC'
|
1211
|
-
end
|
1212
|
-
it 'should return Pakistan code for Turks and Caicos' do
|
1213
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('Turks and Caicos').alpha2).to eq 'TC'
|
1214
|
-
end
|
1215
|
-
it 'should return Pakistan code for St Vincent Grenadines' do
|
1216
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('St Vincent Grenadines').alpha2).to eq 'VC'
|
1217
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('St. Vincent Grenadines').alpha2).to eq 'VC'
|
1218
|
-
end
|
1219
|
-
it 'should return country code for Palestinian Authority' do
|
1220
|
-
expect(ISO3166::Country.find_country_by_unofficial_names('Palestinian Authority').alpha2).to eq 'PS'
|
1221
|
-
end
|
1222
|
-
end
|
1223
|
-
|
1224
|
-
describe 'Emoji' do
|
1225
|
-
it 'has an emoji flag' do
|
1226
|
-
expect(country.emoji_flag).to eq '🇺🇸'
|
1227
|
-
end
|
1228
|
-
end
|
1229
|
-
|
1230
|
-
describe '#un_locode' do
|
1231
|
-
let(:countries) { ISO3166::Country.all }
|
1232
|
-
|
1233
|
-
it 'should have two letter un_locode for each country' do
|
1234
|
-
expect(countries.all? { |country| !country.un_locode.nil? }).to be
|
1235
|
-
expect(countries.all? { |country| country.un_locode.length == 2 }).to be
|
1236
|
-
end
|
1237
|
-
end
|
1238
|
-
|
1239
|
-
describe '.pluck' do
|
1240
|
-
let(:args) { [] }
|
1241
|
-
|
1242
|
-
subject { ISO3166::Country.pluck(*args) }
|
1243
|
-
|
1244
|
-
it 'returns empty arrays' do
|
1245
|
-
expect(subject.first).to be_empty
|
1246
|
-
expect(subject.last).to be_empty
|
1247
|
-
end
|
1248
|
-
|
1249
|
-
context 'when asking for alpha2, alpha3 and iso_short_name' do
|
1250
|
-
let(:args) { %i[alpha2 alpha3 iso_short_name] }
|
1251
|
-
|
1252
|
-
it 'returns the correct values' do
|
1253
|
-
expect(subject.first).to eq(%w[AD AND Andorra])
|
1254
|
-
expect(subject.last).to eq(%w[ZW ZWE Zimbabwe])
|
1255
|
-
end
|
1256
|
-
end
|
1257
|
-
|
1258
|
-
context 'with invalid attributes' do
|
1259
|
-
let(:args) { %i[alpha2 bad_attribute] }
|
1260
|
-
|
1261
|
-
it 'should raise an error' do
|
1262
|
-
expect { subject }.to(
|
1263
|
-
raise_error(KeyError, 'key not found: "bad_attribute"')
|
1264
|
-
)
|
1265
|
-
end
|
1266
|
-
end
|
1267
|
-
end
|
1268
|
-
|
1269
|
-
describe 'find_subdivision_by_name' do
|
1270
|
-
let(:italy) { ISO3166::Country.new('IT') }
|
1271
|
-
let(:napoli) { italy.subdivisions['NA'] }
|
1272
|
-
|
1273
|
-
before do
|
1274
|
-
ISO3166.configuration.locales = %i[pt]
|
1275
|
-
end
|
1276
|
-
|
1277
|
-
it 'should find a subdivision using the official name' do
|
1278
|
-
expect(ISO3166::Country.new('IT').find_subdivision_by_name('Napoli')).to eq napoli
|
1279
|
-
end
|
1280
|
-
|
1281
|
-
it 'should find a subdivision using the code' do
|
1282
|
-
expect(ISO3166::Country.new('IT').find_subdivision_by_name('NA')).to eq napoli
|
1283
|
-
end
|
1284
|
-
|
1285
|
-
it 'should find a subdivision using a translated name' do
|
1286
|
-
expect(ISO3166::Country.new('IT').find_subdivision_by_name('Nápoles')).to eq napoli
|
1287
|
-
end
|
1288
|
-
end
|
1289
|
-
|
1290
|
-
describe 'collect_countries_with' do
|
1291
|
-
let(:italy) { ISO3166::Country.new('IT') }
|
1292
|
-
let(:vatican) { ISO3166::Country.new('VA') }
|
1293
|
-
let(:san_marino) { ISO3166::Country.new('SM') }
|
1294
|
-
let(:switzerland) { ISO3166::Country.new('CH') }
|
1295
|
-
let(:seychelles) { ISO3166::Country.new('SC') }
|
1296
|
-
|
1297
|
-
it 'defaults to querying alpha2 and returning the countries' do
|
1298
|
-
expect(ISO3166::Country.collect_countries_with('IT')).to eq [italy]
|
1299
|
-
end
|
1300
|
-
|
1301
|
-
it 'allows querying by other attributes' do
|
1302
|
-
expect(ISO3166::Country.collect_countries_with('🇸🇨', :emoji_flag)).to eq [seychelles]
|
1303
|
-
expect(ISO3166::Country.collect_countries_with('it',
|
1304
|
-
:languages_spoken)).to eq [switzerland, italy, san_marino, vatican]
|
1305
|
-
end
|
1306
|
-
|
1307
|
-
it 'allows applying a method to the result set' do
|
1308
|
-
expect(ISO3166::Country.collect_countries_with('Caribbean', :subregion,
|
1309
|
-
:languages_spoken).flatten.uniq).to eq %w[en nl fr es ht]
|
1310
|
-
end
|
1311
|
-
end
|
1312
|
-
|
1313
|
-
describe 'collect_likely_countries_by_subdivision_name' do
|
1314
|
-
let(:costa_rica) { ISO3166::Country.new('CR') }
|
1315
|
-
let(:uruguay) { ISO3166::Country.new('UY') }
|
1316
|
-
|
1317
|
-
it 'defaults to returning the countries' do
|
1318
|
-
expect(ISO3166::Country.collect_likely_countries_by_subdivision_name('San José')).to eq [costa_rica, uruguay]
|
1319
|
-
end
|
1320
|
-
|
1321
|
-
it 'allows applying a method to the result set' do
|
1322
|
-
expect(ISO3166::Country.collect_likely_countries_by_subdivision_name('San José',
|
1323
|
-
:iso_short_name)).to eq ['Costa Rica',
|
1324
|
-
'Uruguay']
|
1325
|
-
end
|
1326
|
-
end
|
1327
|
-
end
|