This repository was archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.spec.js
596 lines (460 loc) · 20.2 KB
/
api.spec.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
'use strict';
process.env.NODE_ENV = 'test';
var assert = require('chai').assert;
var qual = require('../lib/index.js');
describe('Validation on real projects', function() {
var errors = [];
var cb = function(msg) {
errors.push(msg);
};
beforeEach(function() {
errors.length = 0;
});
it('should detect missing keys', function() {
var options = {
loc_i18n: __dirname + '/resources/missing_keys',
loc_html: __dirname + '/resources/missing_keys',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], 'Key present in en.json is missing in fr.json. Key: KEY_2');
assert.equal(errors[1], 'Extra key present in fr.json. It was not found in en.json. Key: KEY_5');
});
it('should validate files with the same keys', function() {
var options = {
loc_i18n: __dirname + '/resources/same_keys',
loc_html: __dirname + '/resources/same_keys',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 0);
});
it('should detect when values do not have the same number of mark-ups', function() {
var options = {
loc_i18n: __dirname + '/resources/same_keys_different_markups',
loc_html: __dirname + '/resources/same_keys_different_markups',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], 'A mark-up was expected for KEY_2 in fr.json. See en.json. Mark-up: strong');
assert.equal(errors[1], 'A mark-up was found for KEY_3 in fr.json but it was not found in en.json. Mark-up: strong');
});
it('should detect when the HTML views mix « translate » directives and filters', function() {
var options = {
loc_i18n: __dirname + '/resources/same_keys_with_confused_html',
loc_html: __dirname + '/resources/same_keys_with_confused_html',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 3);
assert.equal(errors[0], 'Do NOT mix the translate directive and the translate filter. File in error: view_fail_1.html');
assert.equal(errors[1], 'Do NOT mix the translate directive and the translate filter. File in error: view_fail_2.html');
assert.equal(errors[2], 'Do NOT mix the translate directive and the translate filter. File in error: view_fail_3.html');
});
it('should detect when HTML views reference invalid keys', function() {
var options = {
loc_i18n: __dirname + '/resources/same_keys_with_html_and_invalid_references',
loc_html: __dirname + '/resources/same_keys_with_html_and_invalid_references',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 3);
assert.equal(errors[0], 'An unknown i18n key is referenced in view.html. Key name: KEY_54');
assert.equal(errors[1], 'An unknown i18n key is referenced in view.html. Key name: MY_KEY');
assert.equal(errors[2], 'An unknown i18n key is referenced in view.html. Key name: MY_OTHER_KEY');
});
it('should detect invalid key definitions', function() {
var options = {
loc_i18n: __dirname + '/resources/invalid_key_definitions',
loc_html: __dirname + '/resources/invalid_key_definitions',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.include(errors[0], 'i18n keys must all be in upper case (with only letters, numbers and underscores). Key: inv_key');
assert.include(errors[1], 'i18n keys must be sorted alphabetically. Key inv_key breaks this rule.');
});
it('should validate a valid project', function() {
var options = {
loc_i18n: __dirname + '/resources/all_valid',
loc_html: __dirname + '/resources/all_valid',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 0);
});
it('should find non-translated ALT attributes in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/html_alt',
loc_html: __dirname + '/resources/html_alt',
cb: cb,
check_html: true
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 2);
assert.equal(errors[0], '[ WARNING ] Non-translated text in view.html: alt="this was not translated"');
assert.equal(errors[1], '[ WARNING ] Non-translated text in view.html: aLT="this was not translated too"');
});
it('should consider exclusions while searching non-translated ALT attributes in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/html_alt',
loc_html: __dirname + '/resources/html_alt',
cb: cb,
check_html: true,
exclusions: ['this was not translated']
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 1);
assert.equal(errors[0], '[ WARNING ] Non-translated text in view.html: aLT="this was not translated too"');
});
it('should find non-translated TITLE attributes in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/html_title',
loc_html: __dirname + '/resources/html_title',
cb: cb,
check_html: true
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 4);
assert.equal(errors[0], '[ WARNING ] Non-translated text in view.html: alt="this was not translated"');
assert.equal(errors[1], '[ WARNING ] Non-translated text in view.html: title="todo"');
assert.equal(errors[2], '[ WARNING ] Non-translated text in view.html: TITLE="todo 2"');
assert.equal(errors[3], '[ WARNING ] Non-translated text in view.html: title="{{ \'KEY_2\' | translate }} not totally translated"');
});
it('should consider exclusions while searching non-translated TITLE attributes in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/html_title',
loc_html: __dirname + '/resources/html_title',
cb: cb,
check_html: true,
exclusions: ['todo 2']
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 3);
assert.equal(errors[0], '[ WARNING ] Non-translated text in view.html: alt="this was not translated"');
assert.equal(errors[1], '[ WARNING ] Non-translated text in view.html: title="todo"');
assert.equal(errors[2], '[ WARNING ] Non-translated text in view.html: title="{{ \'KEY_2\' | translate }} not totally translated"');
});
it('should find non-translated text between mark-ups in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/html_markups',
loc_html: __dirname + '/resources/html_markups',
cb: cb,
check_html: true
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 3);
assert.equal(errors[0], '[ WARNING ] Non-translated text between mark-ups in view.html: "This is not translated {{ \'KEY_1\' | translate }}."');
assert.equal(errors[1], '[ WARNING ] Non-translated text between mark-ups in view.html: "Not done yet"');
assert.equal(errors[2], '[ WARNING ] Non-translated text between mark-ups in view.html: "This either"');
});
it('should consider exclusions while searching non-translated text between mark-ups in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/html_markups',
loc_html: __dirname + '/resources/html_markups',
cb: cb,
check_html: true,
exclusions: ['This either', 'Not done yet']
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 1);
assert.equal(errors[0], '[ WARNING ] Non-translated text between mark-ups in view.html: "This is not translated {{ \'KEY_1\' | translate }}."');
});
it('should find keys that are not used in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/keys_are_not_used',
loc_html: __dirname + '/resources/keys_are_not_used',
cb: cb,
check_html: true
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 2);
assert.equal(errors[0], '[ WARNING ] Key KEY_2 is not used in any HTML file.');
assert.equal(errors[1], '[ WARNING ] Key KEY_4 is not used in any HTML file.');
});
it('should use the callback for keys that are not used in HTML files (no message and warnings)', function() {
var options = {
loc_i18n: __dirname + '/resources/keys_are_not_used',
loc_html: __dirname + '/resources/keys_are_not_used',
cb: cb,
check_html: true,
external_keys_cb: function(errorCallback, notFoundKeys) {
return false;
}
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 0);
});
it('should use the callback for keys that are not used in HTML files (no message and errors)', function() {
var options = {
loc_i18n: __dirname + '/resources/keys_are_not_used',
loc_html: __dirname + '/resources/keys_are_not_used',
cb: cb,
check_html: true,
external_keys_cb: function(errorCallback, notFoundKeys) {
return true;
}
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 0);
});
it('should use the callback for keys that are not used in HTML files (messages and errors)', function() {
var options = {
loc_i18n: __dirname + '/resources/keys_are_not_used',
loc_html: __dirname + '/resources/keys_are_not_used',
cb: cb,
check_html: true,
external_keys_cb: function(errorCallback, notFoundKeys) {
notFoundKeys.forEach(function(key) {
errorCallback('Key ' + key + ' was not found.');
});
return true;
}
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], 'Key KEY_2 was not found.');
assert.equal(errors[1], 'Key KEY_4 was not found.');
});
it('should find non-translated Angular text in HTML files', function() {
var options = {
loc_i18n: __dirname + '/resources/html_angular_text',
loc_html: __dirname + '/resources/html_angular_text',
cb: cb,
check_html: true
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 2);
assert.equal(errors[0], '[ WARNING ] Non-translated text might have been forgotten in view.html: "KEY_2"');
assert.equal(errors[1], '[ WARNING ] Non-translated text might have been forgotten in view.html: "This either"');
});
it('should consider the result as invalid when fail_on_warning is true (non-translated text)', function() {
var options = {
loc_i18n: __dirname + '/resources/html_angular_text',
loc_html: __dirname + '/resources/html_angular_text',
cb: cb,
check_html: true,
fail_on_warning: true
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], '[ WARNING ] Non-translated text might have been forgotten in view.html: "KEY_2"');
assert.equal(errors[1], '[ WARNING ] Non-translated text might have been forgotten in view.html: "This either"');
});
it('should consider the result as invalid when fail_on_warning is true (keys not used in HTML files)', function() {
var options = {
loc_i18n: __dirname + '/resources/keys_are_not_used',
loc_html: __dirname + '/resources/keys_are_not_used',
cb: cb,
check_html: true,
fail_on_warning: true
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], '[ WARNING ] Key KEY_2 is not used in any HTML file.');
assert.equal(errors[1], '[ WARNING ] Key KEY_4 is not used in any HTML file.');
});
it('should find forbidden patterns in values', function() {
var options = {
loc_i18n: __dirname + '/resources/forbidden_patterns',
loc_html: __dirname + '/resources/forbidden_patterns',
cb: cb,
forbidden_patterns: {}
};
// Make options readable.
// Nothing for the "it.json" file.
options.forbidden_patterns.en = [
{regex: '\\s+:', msg: 'Colons cannot be preceded by a white space character.'},
{regex: 'banned', sensitive: true, msg: '"banned" is a forbidden key word.'}
];
options.forbidden_patterns.fr = [
{regex: '\\s,', msg: 'Une virgule s\'écrit sans espace avant.'},
{regex: ',([^ ]| {2,})', msg: 'Une virgule s\'écrit avec un seul espace après.'},
{regex: '^[a-z]', sensitive: true, msg: 'Une phrase commence avec une majuscule.'}
];
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 7);
assert.equal(errors[0], '3: Colons cannot be preceded by a white space character.');
assert.equal(errors[1], '4: Colons cannot be preceded by a white space character.');
assert.equal(errors[2], '5: "banned" is a forbidden key word.');
assert.equal(errors[3], '3: Une virgule s\'écrit sans espace avant.');
assert.equal(errors[4], '3: Une virgule s\'écrit avec un seul espace après.');
assert.equal(errors[5], '4: Une phrase commence avec une majuscule.');
assert.equal(errors[6], '5: Une virgule s\'écrit avec un seul espace après.');
});
it('should find forbidden patterns but also consider the callback handler (1)', function() {
var options = {
loc_i18n: __dirname + '/resources/forbidden_patterns',
loc_html: __dirname + '/resources/forbidden_patterns',
cb: cb,
forbidden_patterns: {}
};
// Define a custom interceptor to manage forbidden keys errors.
options.forbidden_patterns_cb = function(errorCallback, key, value, errorMsg, lineNumber, filename) {
// Ignore the keys defined here, but only for English language
var keysToIgnoreForEnglish = ['KEY_2'];
var invalid = false;
if (filename !== 'en' || keysToIgnoreForEnglish.indexOf(key) === -1) {
errorCallback(lineNumber + ': ' + errorMsg);
invalid = true;
}
return invalid;
};
// Make options readable.
// Nothing for the "it.json" file.
options.forbidden_patterns.en = [
{regex: '\\s+:', msg: 'Colons cannot be preceded by a white space character.'},
{regex: 'banned', sensitive: true, msg: '"banned" is a forbidden key word.'}
];
options.forbidden_patterns.fr = [
{regex: '\\s,', msg: 'Une virgule s\'écrit sans espace avant.'},
{regex: ',([^ ]| {2,})', msg: 'Une virgule s\'écrit avec un seul espace après.'},
{regex: '^[a-z]', sensitive: true, msg: 'Une phrase commence avec une majuscule.'}
];
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 6);
assert.equal(errors[0], '4: Colons cannot be preceded by a white space character.');
assert.equal(errors[1], '5: "banned" is a forbidden key word.');
assert.equal(errors[2], '3: Une virgule s\'écrit sans espace avant.');
assert.equal(errors[3], '3: Une virgule s\'écrit avec un seul espace après.');
assert.equal(errors[4], '4: Une phrase commence avec une majuscule.');
assert.equal(errors[5], '5: Une virgule s\'écrit avec un seul espace après.');
});
it('should find forbidden patterns but also consider the callback handler (2)', function() {
var options = {
loc_i18n: __dirname + '/resources/forbidden_patterns',
loc_html: __dirname + '/resources/forbidden_patterns',
cb: cb,
forbidden_patterns: {}
};
// Define a custom interceptor to manage forbidden keys errors.
options.forbidden_patterns_cb = function(errorCallback, key, value, errorMsg, lineNumber, filename) {
// Do nothing, ignore all these errors
return false;
};
var result = qual.validate(options);
assert.equal(result, true);
assert.equal(errors.length, 0);
});
it('should result in an error when forbidden patterns keys do not refer to any file', function() {
var options = {
loc_i18n: __dirname + '/resources/forbidden_patterns',
loc_html: __dirname + '/resources/forbidden_patterns',
cb: cb,
forbidden_patterns: {}
};
options.forbidden_patterns.de = [
{regex: '\\s+:', msg: 'Colons cannot be preceded by a white space character.'}
];
options.forbidden_patterns.fr = [
{regex: '\\s,', msg: 'Une virgule s\'écrit sans espace avant.'}
];
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], '3: Une virgule s\'écrit sans espace avant.');
assert.equal(errors[1], '"de.json" was not found. The forbidden patterns key "de" is invalid.');
});
it('should detect malformed filters', function() {
var options = {
loc_i18n: __dirname + '/resources/malformed_filter',
loc_html: __dirname + '/resources/malformed_filter',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], 'Malformed declaration with the translate filter in view.html. The key must be surrounded with quotes. Key name: KEY_1');
assert.equal(errors[1], 'Malformed declaration with the translate filter in view.html. The key must be surrounded with quotes. Key name: KEY_2');
});
it('should detect values that are not trimmed', function() {
var options = {
loc_i18n: __dirname + '/resources/values_not_trimmed',
loc_html: __dirname + '/resources/values_not_trimmed',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 3);
assert.equal(errors[0], '2: Values should be trimmed (no white space character at the beginning and the end). Key: KEY_1');
assert.equal(errors[1], '3: Values should be trimmed (no white space character at the beginning and the end). Key: KEY_2');
assert.equal(errors[2], '4: Values should be trimmed (no white space character at the beginning and the end). Key: KEY_3');
});
it('should detect invalid tabs in keys or values', function() {
var options = {
loc_i18n: __dirname + '/resources/invalid_tabs',
loc_html: __dirname + '/resources/invalid_tabs',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 2);
assert.equal(errors[0], '2: Values should not contain tabs (this is invalid json, escape them using \t). Key: BAZ');
assert.equal(errors[1], '3: i18n keys must all be in upper case (with only letters, numbers and underscores). Key: FOO\tBAR');
});
it('should allow setting the indent', function() {
var options = {
loc_i18n: __dirname + '/resources/space_indent',
loc_html: __dirname + '/resources/space_indent',
indent: ' ',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, true);
});
it('should allow ignoring the order', function() {
var options = {
loc_i18n: __dirname + '/resources/invalid_order',
loc_html: __dirname + '/resources/invalid_order',
ignore_order: true,
cb: cb
};
var result = qual.validate(options);
assert.equal(result, true);
});
it('should detect empty values', function() {
var options = {
loc_i18n: __dirname + '/resources/empty_values',
loc_html: __dirname + '/resources/empty_values',
cb: cb
};
var result = qual.validate(options);
assert.equal(result, false);
assert.equal(errors.length, 1);
assert.equal(errors[0], '2: Values should not be empty. Key: FOO');
});
it('should allow ignoring empty values', function() {
var options = {
loc_i18n: __dirname + '/resources/empty_values',
loc_html: __dirname + '/resources/empty_values',
ignore_empty_values: true,
cb: cb
};
var result = qual.validate(options);
assert.equal(result, true);
});
});