/
y18n-test.cjs
375 lines (310 loc) · 11 KB
/
y18n-test.cjs
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* global describe, it, after, beforeEach */
const expect = require('chai').expect
const fs = require('fs')
const rimraf = require('rimraf')
const y18n = require('../build/index.cjs')
const path = require('path')
require('chai').should()
describe('y18n', function () {
describe('configure', function () {
it('allows you to override the default y18n configuration', function () {
const y = y18n({ locale: 'fr' })
y.locale.should.equal('fr')
})
})
describe('_readLocaleFile', function () {
it('throws a helpful error if language file has invalid syntax', function () {
expect(function () {
var __ = y18n({
locale: 'bad-locale',
directory: path.join(__dirname, 'locales')
}).__
__('Hello')
}).to.throw(/syntax error/)
})
})
describe('__', function () {
it('can be used as a tag for template literals', function () {
var __ = y18n({
locale: 'pirate',
directory: path.join(__dirname, 'locales')
}).__
__`Hi, ${'Ben'} ${'Coe'}!`.should.equal('Yarr! Shiver me timbers, why \'tis Ben Coe!')
})
it('can be used as a tag for template literals with falsy arguments', function () {
var __ = y18n({
locale: 'pirate',
directory: path.join(__dirname, 'locales')
}).__
__`Hi, ${'Ben'} ${''}!`.should.equal('Yarr! Shiver me timbers, why \'tis Ben !')
})
it('uses replacements from the default locale if none is configured', function () {
var __ = y18n({
directory: path.join(__dirname, 'locales')
}).__
__('Hello').should.equal('Hello!')
})
it('uses replacements from the configured locale', function () {
var __ = y18n({
locale: 'pirate',
directory: path.join(__dirname, 'locales')
}).__
__('Hello').should.equal('Avast ye mateys!')
})
it('uses language file if language_territory file does not exist', function () {
var __ = y18n({
locale: 'pirate_JM',
directory: path.join(__dirname, 'locales')
}).__
__('Hello').should.equal('Avast ye mateys!')
})
it('does not fallback to language file if fallbackToLanguage is false', function () {
var __ = y18n({
locale: 'pirate_JM',
fallbackToLanguage: false,
updateFiles: false,
directory: path.join(__dirname, 'locales')
}).__
__('Hello').should.equal('Hello')
})
it('uses strings as given if no matching locale files found', function () {
var __ = y18n({
locale: 'zz_ZZ',
updateFiles: false,
directory: path.join(__dirname, 'locales')
}).__
__('Hello').should.equal('Hello')
})
it('expands arguments into %s placeholders', function () {
var __ = y18n({
directory: path.join(__dirname, 'locales')
}).__
__('Hello %s %s', 'Ben', 'Coe').should.equal('Hello Ben Coe')
})
describe('the first time observing a word', function () {
beforeEach(function (done) {
rimraf('./test/locales/fr*.json', function () {
return done()
})
})
it('returns the word immediately', function () {
var __ = y18n({
locale: 'fr',
directory: path.join(__dirname, 'locales')
}).__
__('banana').should.equal('banana')
})
it('writes new word to locale file if updateFiles is true', function (done) {
var __ = y18n({
locale: 'fr_FR',
directory: path.join(__dirname, 'locales')
}).__
__('banana', function (err) {
var locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8'))
locale.banana.should.equal('banana')
return done(err)
})
})
it('writes new word to language file if language_territory file does not exist', function (done) {
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8')
var __ = y18n({
locale: 'fr_FR',
directory: path.join(__dirname, 'locales')
}).__
__('meow').should.equal('le meow')
__('banana', function (err) {
var locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
locale.banana.should.equal('banana')
return done(err)
})
})
it('writes word to missing locale file, if no fallback takes place', function (done) {
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8')
var __ = y18n({
locale: 'fr_FR',
fallbackToLanguage: false,
directory: path.join(__dirname, 'locales')
}).__
__('banana', function (err) {
// 'banana' should be written to fr_FR.json
var locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8'))
locale.should.deep.equal({
banana: 'banana'
})
// fr.json should remain untouched
var frJson = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
frJson.should.deep.equal({
meow: 'le meow'
})
return done(err)
})
})
it('handles enqueuing multiple writes at the same time', function (done) {
var __ = y18n({
locale: 'fr',
directory: path.join(__dirname, 'locales')
}).__
__('apple')
__('banana', function () {
__('foo')
__('bar', function (err) {
var locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
locale.apple.should.equal('apple')
locale.banana.should.equal('banana')
locale.foo.should.equal('foo')
locale.bar.should.equal('bar')
return done(err)
})
})
})
it('does not write the locale file if updateFiles is false', function (done) {
var __ = y18n({
locale: 'fr',
updateFiles: false,
directory: path.join(__dirname, 'locales')
}).__
__('banana', function (err) {
fs.existsSync('./test/locales/fr.json').should.equal(false)
return done(err)
})
})
})
})
describe('__n', function () {
it('uses the singular form if quantity is 1', function () {
var __n = y18n({
directory: path.join(__dirname, 'locales')
}).__n
__n('%d cat', '%d cats', 1).should.equal('1 cat')
})
it('uses the plural form if quantity is greater than 1', function () {
var __n = y18n({
directory: path.join(__dirname, 'locales')
}).__n
__n('%d cat', '%d cats', 2).should.equal('2 cats')
})
it('allows additional arguments to be printed', function () {
var __n = y18n({
directory: path.join(__dirname, 'locales')
}).__n
__n('%d %s cat', '%d %s cats', 2, 'black').should.equal('2 black cats')
})
it('allows an alternative locale to be set', function () {
var __n = y18n({
locale: 'pirate',
directory: path.join(__dirname, 'locales')
}).__n
__n('%d cat', '%d cats', 1).should.equal('1 land catfish')
__n('%d cat', '%d cats', 3).should.equal('3 land catfishes')
})
// See: https://github.com/bcoe/yargs/pull/210
it('allows a quantity placeholder to be provided in the plural but not singular form', function () {
var __n = y18n({
directory: path.join(__dirname, 'locales')
}).__n
var singular = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 1, 'tree')
var plural = __n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, 'tree')
singular.should.equal('There is one monkey in the tree')
plural.should.equal('There are 3 monkeys in the tree')
})
describe('the first time observing a pluralization', function () {
beforeEach(function (done) {
rimraf('./test/locales/fr.json', function () {
return done()
})
})
it('returns the pluralization immediately', function () {
var __n = y18n({
locale: 'fr',
directory: path.join(__dirname, 'locales')
}).__n
__n('%d le cat', '%d le cats', 1).should.equal('1 le cat')
})
it('writes to the locale file if updateFiles is true', function (done) {
var __n = y18n({
locale: 'fr',
directory: path.join(__dirname, 'locales')
}).__n
__n('%d apple %s', '%d apples %s', 2, 'dude', function (err) {
var locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8'))
locale['%d apple %s'].one.should.equal('%d apple %s')
locale['%d apple %s'].other.should.equal('%d apples %s')
return done(err)
})
})
it('does not write the locale file if updateFiles is false', function (done) {
var __n = y18n({
locale: 'fr',
updateFiles: false,
directory: path.join(__dirname, 'locales')
}).__n
__n('%d apple %s', '%d apples %s', 2, 'dude', function (err) {
fs.existsSync('./test/locales/fr.json').should.equal(false)
return done(err)
})
})
})
})
describe('setLocale', function () {
it('switches the locale', function () {
var i18n = y18n({
directory: path.join(__dirname, 'locales')
})
i18n.__('Hello').should.equal('Hello!')
i18n.setLocale('pirate')
i18n.__('Hello').should.equal('Avast ye mateys!')
})
})
describe('updateLocale', function () {
beforeEach(function (done) {
rimraf('./test/locales/fr.json', function () {
return done()
})
})
it('updates the locale with the new lookups provided', function () {
var i18n = y18n({
locale: 'fr',
directory: path.join(__dirname, 'locales')
})
i18n.updateLocale({
foo: 'le bar'
})
i18n.__('foo').should.equal('le bar')
})
it('loads the locale from disk prior to updating the map', function () {
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8')
var i18n = y18n({
locale: 'fr',
directory: path.join(__dirname, 'locales')
})
i18n.updateLocale({
foo: 'le bar'
})
i18n.__('meow').should.equal('le meow')
})
})
describe('getLocale', function () {
it('returns the configured locale', function () {
y18n().getLocale().should.equal('en')
})
})
// See: https://github.com/yargs/y18n/issues/96,
// https://github.com/yargs/y18n/pull/107
describe('prototype pollution', () => {
it('does not pollute prototype, with __proto__ locale', () => {
const y = y18n()
y.setLocale('__proto__')
y.updateLocale({ polluted: '👽' })
y.__('polluted').should.equal('👽')
;(typeof polluted).should.equal('undefined')
})
it('does not pollute prototype, when __ is used with __proto__ locale', () => {
const __ = y18n({ locale: '__proto__' }).__
__('hello')
;(typeof {}.hello).should.equal('undefined')
})
})
after(function () {
rimraf.sync('./test/locales/fr.json')
})
})