-
Notifications
You must be signed in to change notification settings - Fork 289
/
localization_tests.js
414 lines (326 loc) · 15.5 KB
/
localization_tests.js
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//depedencies
var path = require('path');
var should = require('should');
var Configuration = require('../../include/config.js');
var Lib = require('../../lib');
describe('Localization', function() {
//set the dummy data for
var dummyLocale = 'pl-pl';
var dummyDisplay = 'Polski (Polska)';
var dummyLocalizations = { key: 'value' };
var pb = null;
var Localization = null;
before('Initialize the Environment with the default configuration', function(next) {
pb = new Lib(Configuration.getBaseConfig());
Localization = pb.Localization;
Localization.init(next);
});
describe('Localization.getLocalizationPackage', function() {
var acceptable = ['pl-PL', 'pl-pl'];
acceptable.forEach(function(locale) {
it('should return a valid package when provided '+locale, function() {
var pkg = Localization.getLocalizationPackage(locale);
pkg.generic.LOCALE_DISPLAY.should.eql(dummyDisplay);
});
});
var unacceptable = [null, undefined];
unacceptable.forEach(function(locale) {
it('should return null when provided '+locale, function() {
var pkg = Localization.getLocalizationPackage(locale);
should(pkg === null).be.ok;
});
});
var unRegistered = ['en-GB', 'ar-SY'];
unRegistered.forEach(function(locale) {
it('should default to English whe provided '+locale, function() {
var pkg = Localization.getLocalizationPackage(locale);
pkg.generic.LOCALE_DISPLAY.should.eql('English (United States)');
});
});
});
describe('Localization.isSupported', function() {
var acceptable = ['pl-PL', 'en-us', 'es-es', 'ro-RO', 'fr-fr', 'pt-br'];
acceptable.forEach(function(locale) {
it('should return true when provided '+locale, function() {
var supported = Localization.isSupported(locale);
supported.should.eql(true);
});
});
var unacceptable = ['en-GB', 'ar-SY', undefined, false, 0, 1, 2.2, null, ''];
unacceptable.forEach(function(locale) {
it('should return false when provided '+locale, function() {
var supported = Localization.isSupported(locale);
supported.should.not.be.ok;
});
});
});
describe('Localization.g', function() {
var badKeys = [1, 25.5, true, {}, [], null, undefined];
badKeys.forEach(function(key) {
it('should throw when a non-string value [' + key + '] is passed as the key', function() {
var loc = new Localization(dummyLocale);
loc.g.bind(loc, key, {}).should.throwError();
});
});
var badOptions = [1, 25.5, true];
badOptions.forEach(function(options) {
it('should throw when a non-object value [' + options + '] is passed as the options parameter', function() {
var loc = new Localization(dummyLocale);
loc.g.bind(loc, 'generic.LOCALE_DISPLAY', options).should.throwError();
});
});
var defaultOptions = [null, undefined, {}];
defaultOptions.forEach(function(options) {
it('should return a value when passed a defaulting options parameter: ' + options, function() {
var loc = new Localization(dummyLocale);
loc.g('generic.LOCALE_DISPLAY', options).should.eql(dummyDisplay);
});
});
var badParams = [1, 25.5, true];
badParams.forEach(function(params) {
it('should throw when a non-object value [' + params + '] is passed as the options parameter', function() {
var opts = {
params: params
};
var loc = new Localization(dummyLocale);
loc.g.bind(loc, 'generic.LOCALE_DISPLAY', opts).should.throwError();
});
});
it('should replace parameters in the value of the provided key with the supplied parameters', function() {
var plugin = 'test';
var key = 'generic.about.pencilblue';
var val = "PencilBlue is an {adjective} {noun}. It is quite comprehensive and provides a lot of {keyWord}.";
var opts = {
params: {
adjective: 'amazing',
noun: 'CMS',
keyWord: 'features'
},
plugin: plugin
};
var registrationResult = Localization.registerLocalization(dummyLocale, 'generic.about.pencilblue', val, opts);
registrationResult.should.be.ok;
var loc = new Localization(dummyLocale);
var result = loc.g(key, opts);
result.should.eql("PencilBlue is an amazing CMS. It is quite comprehensive and provides a lot of features.");
});
});
describe('Localization.localize', function() {
it('should localize all valid keys in a given text provided the sets to inspect plus generic', function() {
var val = 'Hello world, ^loc_PENCILBLUE^ is a CMS. It has a built in wysiwyg that supports ^loc_NORMAL_TEXT^. This test should not lookup the localization ^loc_MANAGE_COMMENTS^ correctly.';
var expected = 'Hello world, PencilBlue is a CMS. It has a built in wysiwyg that supports Normal text. This test should not lookup the localization ^loc_MANAGE_COMMENTS^ correctly.';
var ls = new Localization('en-US');
ls.localize(['wysiwyg'], val, 'localhost:8080').should.eql(expected);
});
});
describe('Localization.replaceParameters', function() {
var badValues = [1, 2.1, true, {}, [], null, undefined];
badValues.forEach(function(value) {
it('should throw an error when an invalid value [' + value + '] is provided', function() {
Localization.replaceParameters.bind(null, value, {}).should.throwError();
});
});
var badParams = [1, 2.1, true, "", null, undefined];
badParams.forEach(function(params) {
it('should throw an error when an invalid params parameter [' + params + '] is provided', function() {
Localization.replaceParameters.bind(null, "hello {world}", params).should.throwError();
});
});
it('should replace parameters in the value with the supplied parameters', function() {
var val = "PencilBlue is an {adjective} {noun}. It is quite comprehensive and provides a lot of {keyWord}.";
var params = {
adjective: 'amazing',
noun: 'CMS',
keyWord: 'features'
};
var result = Localization.replaceParameters(val, params);
result.should.eql("PencilBlue is an amazing CMS. It is quite comprehensive and provides a lot of features.");
});
});
describe('Localization.formatLocale', function() {
[1, 2.2, false, {}, [], null, undefined].forEach(function(param) {
it('should throw an error when a non-string language parameter ' + param + ' is provided', function() {
Localization.formatLocale.bind(null, param).should.throwError();
});
});
[1, 2.2, false, {}, []].forEach(function(param) {
it('should throw an error when a non-string countryCode parameter ' + param + ' is provided', function() {
Localization.formatLocale.bind(null, 'en', param).should.throwError();
});
});
it('should format the locale with no country code when provided just the language', function() {
var language = 'en';
var result = Localization.formatLocale(language);
result.should.eql(language);
});
it('should format the locale with a country code when provided the language & country code', function() {
var language = 'EN';
var countryCode = 'us';
var result = Localization.formatLocale(language, countryCode);
result.should.eql('en-US');
});
});
describe('Localization.parseLocaleStr', function() {
[
['en', 'en', null],
['PO', 'po', null],
['Ro', 'ro', null],
['eS', 'es', null],
['/unix/file/path/en.json', 'en', null],
['C:\\windows\\file\\path\\en.js', 'en', null],
['en-US', 'en', 'US'],
['PO-us', 'po', 'US'],
['Ro-Ro', 'ro', 'RO'],
['eS-Es', 'es', 'ES'],
['/unix/file/path/en-US.json', 'en', 'US'],
['C:\\windows\\file\\path\\EN-us.js', 'en', 'US']
].forEach(function(testCaseParams) {
it('should parse the locale as language lower case and country code upper case (when provided)', function() {
var result = Localization.parseLocaleStr(testCaseParams[0]);
result.language.should.eql(testCaseParams[1]);
should.strictEqual(result.countryCode, testCaseParams[2]);
});
});
it('should return the locale object when passed in', function() {
var locale = {
language: 'en',
countryCode: 'US'
};
var result = Localization.parseLocaleStr(locale);
result.should.eql(locale);
});
[
1,
2.1,
true,
[],
{},
null,
undefined,
{ language: 1, countryCode: 'US' },
{ language: 1.2, countryCode: 'US' },
{ language: false, countryCode: 'US' },
{ language: [], countryCode: 'US' },
{ language: {}, countryCode: 'US' },
{ language: null, countryCode: 'US' },
{ language: "en", countryCode: 1 },
{ language: "en", countryCode: 2.2 },
{ language: "en", countryCode: false },
{ language: "en", countryCode: [] },
{ language: "en", countryCode: {} },
].forEach(function(locale) {
it('should throw when provided an invalid locale '+ JSON.stringify(locale), function() {
Localization.parseLocaleStr.bind(null, locale).should.throwError();
});
});
});
describe('Localization.getSupportedWithDisplay', function() {
it('should return an array with the same number of items as locale files', function(done) {
getLocalizationFiles(function(err, files) {
should.not.exist(err);
var result = Localization.getSupportedWithDisplay();
files.length.should.eql(result.length);
done();
});
});
});
describe('Localization.getSupported', function() {
it('should return an array with the same number of items as locale files', function(done) {
getLocalizationFiles(function(err, files) {
should.not.exist(err);
var result = Localization.getSupported();
files.length.should.eql(result.length);
done();
});
});
});
describe('Localization.containsParameters', function() {
[1, 2.2, false, [], {}, null, undefined].forEach(function(val) {
it('should throw an error when provided an invalid value '+ val, function() {
Localization.containsParameters.bind(null, val).should.throwError();
});
});
[
["", false],
["hello.world", false],
["hello this is just a simple test", false],
["hello {this is my", false],
["} hello world", false],
["}{ what about", false],
["{what about}", true],
["This is {param1} good {param2}", true]
].forEach(function(testCaseParams) {
it('should inspect the value and determine if parameters are found within it', function() {
var result = Localization.containsParameters(testCaseParams[0]);
result.should.eql(testCaseParams[1]);
});
});
});
describe('Localization.unregisterLocale', function() {
it('should return true and no longer be supported when a valid locale is unregistered', function() {
var locale = 'nl-BE';
Localization.unregisterLocale(locale).should.eql(true);
Localization.isSupported(locale).should.eql(false);
});
it('should return false when an unregistered locale is passed', function() {
Localization.unregisterLocale('we-WE').should.eql(false);
});
[null, undefined, ''].forEach(function(val) {
it('should throw when passed an invalid locale: '+val, function() {
Localization.unregisterLocale.bind(null, [val]).should.throwError();
});
});
});
describe('Localization.registerLocale', function() {
//TODO
});
describe('Localization.registerLocalization', function() {
//TODO
});
describe('Localization.unregisterLocalization', function() {
it('should throw when not passed a locale', function() {
Localization.unregisterLocalization.bind(null, [null]).should.throwError();
});
it('should throw when not passed a key', function() {
Localization.unregisterLocalization.bind(null, ['en-US', 'general.SUCCESS']).should.throwError();
});
it('should return false when the key is not found', function() {
Localization.unregisterLocalization('en-US', 'general.non-existing').should.eql(false);
});
it('should return false when provided an existing key and country code with a plugin that has not registered the key', function() {
Localization.unregisterLocalization('en-US', 'general.TEMPLATES', {plugin: 'non-existing'}).should.eql(false);
});
//TODO should return true when provided an existing key and country code with a plugin that has registered the key
});
describe('Localization.get', function() {
[null, undefined, '', 1, false, true, [], {}].forEach(function(key) {
it('should return null when an invalid key ' + key + ' is provided', function () {
var ls = new Localization('en-US');
should(ls.get(key) === null).eql(true);
});
});
it('should return the key when a non-existing localization key is passed', function() {
var key = 'SOME_NON_EXISTING_KEY';
var ls = new Localization('en-US');
ls.get(key).should.eql(key);
});
it('should lookup the key prefix and then translate the key', function() {
var key = 'LEFT';
var ls = new Localization('en-US');
ls.get(key).should.eql('Left');
});
it('should replace the key and insert the argument', function() {
var key = 'INSTALL_FAILED';
var ls = new Localization('en-US');
ls.get(key, 'test-value').should.eql('The attempt to install plugin [test-value] failed');
});
});
function getLocalizationFiles(cb) {
var options = {
recursive: false,
filter: function(filePath) { return filePath.indexOf('.js') === filePath.length - '.js'.length; }
};
var dir = path.join(pb.config.docRoot, 'public/localization');
pb.util.getFiles(dir, options, cb);
}
});