Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
227 lines (194 loc) · 8.13 KB
/
Copy pathtest.js
File metadata and controls
227 lines (194 loc) · 8.13 KB
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
var assert = require("assert");
var countries = require("./index.js");
describe("country code", function () {
it("loads the countries into an array", function () {
assert(countries.countries.length >= 239, "not all countries loaded, found " + countries.countries.length);
});
it("finds countries by fips", function () {
var uk = countries.byFips("UK");
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
assert.strictEqual(uk.continent, "Europe");
assert.strictEqual(uk.region, "Western Europe");
assert.strictEqual(uk.capital, "London");
});
it("finds countries by iso 2", function () {
var uk = countries.byIso("GB");
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
});
it("finds countries by iso 3", function () {
var uk = countries.byIso("GBR");
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
});
it("finds countries by iso number", function () {
var uk = countries.byIso(826);
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
});
it("finds countries by country name", function () {
var uk = countries.byCountry("United Kingdom");
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
assert.strictEqual(uk.continent, "Europe");
assert.strictEqual(uk.region, "Western Europe");
assert.strictEqual(uk.capital, "London");
});
it("finds countries by iso number as a string", function () {
var uk = countries.byIso("826");
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
});
[1826, -826, 826.9, 1000, 1e21, NaN, Infinity, -Infinity].forEach(function (code) {
it("returns null for invalid numeric ISO code " + code, function () {
assert.strictEqual(countries.byIso(code), null);
});
});
["1826", "1000", "999999999999999999999826"].forEach(function (code) {
it("returns null for out-of-range numeric ISO string " + code, function () {
assert.strictEqual(countries.byIso(code), null);
});
});
it("preserves numeric ISO padding and leading-zero lookups", function () {
[4, "4", "04", "004"].forEach(function (code) {
assert.strictEqual(countries.byIso(code).iso2, "AF");
});
[50, "50", "050", "00050"].forEach(function (code) {
assert.strictEqual(countries.byIso(code).iso2, "BD");
});
[0, "000", 999, "999"].forEach(function (code) {
assert.strictEqual(countries.byIso(code), null);
});
});
it("finds countries by internet code", function () {
var uk = countries.byInternet("UK");
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
});
it("finds countries by lower case fips", function () {
var uk = countries.byFips("uk");
assert(uk, "no country");
assert.strictEqual(uk.country, "United Kingdom");
});
it("throws an error if the iso code is invalid", function () {
assert.throws(function () {
countries.byIso("foo bar baz");
});
});
it("returns null if no country is found", function () {
var uk = countries.byIso(23452);
assert.strictEqual(uk, null);
});
it("Romania is now ROU", function () {
assert.strictEqual(countries.byIso("ROM"), null);
assert(countries.byIso("ROU"), "Romania is now ROU");
});
it("Democratic Republic of Congo", function () {
assert(countries.byIso("COD"), "Democratic Republic of Congo is COD");
assert(countries.byIso("CD"), "Democratic Republic of Congo is CD");
});
it("Zambia is ZMB", function () {
assert(countries.byIso("ZMB"), "Zambia is ZMB");
});
it("Finds countries with leading zeros", function () {
var bangladesh = countries.byIso("050");
assert(bangladesh, "Bangladesh not found");
assert.strictEqual(bangladesh.country, "Bangladesh");
});
it("All iso numbers are 3 digits", function () {
var bad = countries.countries
.filter(country => country.country !== "Republic of Kosovo")
.filter(country => country.isoNo.length !== 3);
assert.strictEqual(bad.length, 0, "iso numbers are not all 3 digits - check " + (bad[0] || {}).country);
});
it("We now call the Czech Republic 'Czechia'", function () {
var czechia = countries.byFips("EZ");
assert.strictEqual(czechia.country, "Czechia");
});
it("Sudan is 729", function () {
var sudan = countries.byIso("SDN");
assert.strictEqual(sudan.isoNo, "729");
});
it("Kosovo is XKX", function () {
var kosovo = countries.byIso("XKX");
assert.strictEqual(kosovo.iso3, "XKX");
});
[
["CV", "country", "Cabo Verde"],
["YT", "capital", "Mamoudzou"],
["FM", "fips", "FM"],
["MM", "capital", "Naypyidaw"],
["PW", "capital", "Ngerulmud"],
["ZA", "capital", "Pretoria"],
["TZ", "capital", "Dodoma"],
["VE", "internet", "VE"],
["WS", "country", "Samoa"],
["CD", "internet", "CD"],
["TF", "internet", "TF"],
["SH", "capital", "Jamestown"],
["AX", "fips", ""],
["BQ", "fips", ""]
].forEach(function (correction) {
it("uses the corrected " + correction[1] + " for " + correction[0], function () {
assert.strictEqual(countries.byIso(correction[0])[correction[1]], correction[2]);
});
});
it("finds Micronesia by FIPS without treating ISO-only codes as FIPS", function () {
assert.strictEqual(countries.byFips("FM"), countries.byIso("FM"));
assert.strictEqual(countries.byFips("fm"), countries.byIso("FM"));
["AX", "ax", "BQ", "bq"].forEach(function (code) {
assert.strictEqual(countries.byFips(code), null);
});
});
it("finds corrected internet codes and rejects the obsolete values", function () {
["VE", "CD", "TF"].forEach(function (code) {
assert.strictEqual(countries.byInternet(code), countries.byIso(code));
assert.strictEqual(countries.byInternet(code.toLowerCase()), countries.byIso(code));
});
["UE", "ZR", "--"].forEach(function (code) {
assert.strictEqual(countries.byInternet(code), null);
});
});
it("finds the updated country names and rejects the old names", function () {
assert.strictEqual(countries.byCountry("Cabo Verde"), countries.byIso("CV"));
assert.strictEqual(countries.byCountry("Samoa"), countries.byIso("WS"));
assert.strictEqual(countries.byCountry("Cape Verde"), null);
assert.strictEqual(countries.byCountry("Western Samoa"), null);
});
// null/undefined input guards
it("byFips returns null for null/undefined/non-string input", function () {
assert.strictEqual(countries.byFips(null), null);
assert.strictEqual(countries.byFips(undefined), null);
assert.strictEqual(countries.byFips(123), null);
});
it("byIso returns null for null/undefined", function () {
assert.strictEqual(countries.byIso(null), null);
assert.strictEqual(countries.byIso(undefined), null);
});
it("byIso returns null for non-string/non-number input without coercion", function () {
[true, [826], { toString: function () { return "826"; } }, 826n, Symbol("826")].forEach(function (code) {
assert.strictEqual(countries.byIso(code), null);
});
});
it("byInternet returns null for null/undefined/non-string input", function () {
assert.strictEqual(countries.byInternet(null), null);
assert.strictEqual(countries.byInternet(undefined), null);
assert.strictEqual(countries.byInternet(42), null);
});
it("byCountry returns null for null/undefined/non-string input", function () {
assert.strictEqual(countries.byCountry(null), null);
assert.strictEqual(countries.byCountry(undefined), null);
assert.strictEqual(countries.byCountry(99), null);
});
// byIso does not treat mixed alpha-numeric strings as numbers
it("byIso does not misparse mixed alpha-numeric strings as numbers", function () {
// "1A" has length 2, so it's treated as an iso2 lookup (not a number), returning null
assert.strictEqual(countries.byIso("1A"), null);
});
// countries array is frozen
it("countries array is frozen and immutable", function () {
assert(Object.isFrozen(countries.countries), "countries array should be frozen");
assert(Object.isFrozen(countries.countries[0]), "country objects should be frozen");
});
});