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 pathsingle-json.spec.js
238 lines (168 loc) · 8.31 KB
/
single-json.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
'use strict';
process.env.NODE_ENV = 'test';
var assert = require('chai').assert;
var qual = require('../lib/index.js');
describe('Validation of a single JSon file', function() {
var errors = [];
var cb = function(msg) {
errors.push(msg);
};
var options = qual.fix_options({cb: cb});
beforeEach(function() {
errors = [];
});
it('should ignore files without content', function() {
qual.verify_i18n_one(options, '');
assert.deepEqual(errors, []);
qual.verify_i18n_one(options, '\n\n\n');
assert.deepEqual(errors, []);
});
it('should detect trailing spaces', function() {
qual.verify_i18n_one(options, ' ');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Trailing spaces '), -1);
errors = [];
qual.verify_i18n_one(options, '\n\n\t\n');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Trailing spaces '), -1);
errors = [];
qual.verify_i18n_one(options, '\n\n\t\n ');
assert.equal(errors.length, 2);
assert.notEqual(errors[0].indexOf('Trailing spaces '), -1);
assert.notEqual(errors[1].indexOf('Trailing spaces '), -1);
});
it('should verify the corretness of the JSon structure', function() {
qual.verify_i18n_one(options, '"KEY": "value"');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY": "value",');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '\t"KEY": "value",');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '\t\t"KEY": "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Lines must match the following pattern:'), -1);
errors = [];
qual.verify_i18n_one(options, '"KEY": "value" ');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Trailing spaces '), -1);
errors = [];
qual.verify_i18n_one(options, '\'KEY\': \'value\'');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Lines must match the following pattern:'), -1);
errors = [];
qual.verify_i18n_one(options, '"KEY" : "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Lines must match the following pattern:'), -1);
errors = [];
qual.verify_i18n_one(options, 'KEY: value');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Lines must match the following pattern:'), -1);
errors = [];
qual.verify_i18n_one(options, '\n"KEY": "value"\n');
assert.equal(errors.length, 0);
errors = [];
qual.verify_i18n_one(options, '"KEY" "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Lines must match the following pattern:'), -1);
});
it('should verify keys are in upper case', function() {
qual.verify_i18n_one(options, '"KEY": "value"');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"key": "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('i18n keys must all be in upper case'), -1);
});
it('should verify only letters, numbers and underscores are accepted in keys', function() {
qual.verify_i18n_one(options, '"KEY_ONE": "value"');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"1_ST_KEY": "value"');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY-1": "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('i18n keys must all be in upper case'), -1);
errors = [];
qual.verify_i18n_one(options, '"KEY_@": "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('i18n keys must all be in upper case'), -1);
});
it('should detect duplicate keys', function() {
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value"\n"KEY_3": "value"\n"KEY_4": "value"');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value"\n"KEY_1": "value"\n"KEY_4": "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('Duplicate key: KEY_1'), -1);
errors = [];
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value"\n"KEY_1": "value"\n"KEY_2": "value"');
assert.equal(errors.length, 2);
assert.notEqual(errors[0].indexOf('Duplicate key: KEY_1'), -1);
assert.notEqual(errors[1].indexOf('Duplicate key: KEY_2'), -1);
});
it('should verify keys are sorted alpĥabetically', function() {
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value"\n"KEY_3": "value"\n"KEY_4": "value"');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_3": "value"\n"KEY_2": "value"\n"KEY_4": "value"');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('i18n keys must be sorted alphabetically. Key KEY_2'), -1);
errors = [];
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_4": "value"\n"KEY_3": "value"\n"KEY_2": "value"');
assert.equal(errors.length, 2);
assert.notEqual(errors[0].indexOf('i18n keys must be sorted alphabetically. Key KEY_3'), -1);
assert.notEqual(errors[1].indexOf('i18n keys must be sorted alphabetically. Key KEY_2'), -1);
});
it('should detect missing closing tags (HTML in values)', function() {
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is <strong>there</strong>."\n');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is <strong>there</strong><br />."\n');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is here<br />."\n');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is <strong>there<strong>."\n');
assert.equal(errors.length, 2);
assert.notEqual(errors[0].indexOf('A HTML tag is not closed correctly. Tag: strong. Key: KEY_2'), -1);
assert.notEqual(errors[1].indexOf('A HTML tag is not closed correctly. Tag: strong. Key: KEY_2'), -1);
errors = [];
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is <strong>here</strong> and also <strong>there."\n');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('A HTML tag is not closed correctly. Tag: strong. Key: KEY_2'), -1);
});
it('should detect missing opening tags (HTML in values)', function() {
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is <strong>there</strong>."\n');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is <strong>there</strong><br />."\n');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is here<br />."\n');
assert.equal(errors.length, 0);
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is </strong>there</strong>."\n');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('A HTML tag has no opening match. Tag: strong. Key: KEY_2'), -1);
errors = [];
qual.verify_i18n_one(options, '"KEY_1": "value"\n"KEY_2": "value is <strong>here</strong> and also there</strong>."\n');
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('A HTML tag has no opening match. Tag: strong. Key: KEY_2'), -1);
});
it('should return the right result)', function() {
var res = qual.verify_i18n_one(options, '');
assert.equal(errors.length, 0);
assert.isFalse(res.failure);
assert.deepEqual(res.keys, []);
assert.deepEqual(res.all, {});
res = qual.verify_i18n_one(options, '"KEY_1": "value"');
assert.equal(errors.length, 0);
assert.isFalse(res.failure);
assert.deepEqual(res.keys, ['KEY_1']);
assert.deepEqual(res.all, {KEY_1: 'value'});
res = qual.verify_i18n_one(options, '"key": "value"');
assert.isTrue(res.failure);
assert.equal(errors.length, 1);
assert.notEqual(errors[0].indexOf('i18n keys must all be in upper case'), -1);
});
it('should ignore curly brackets)', function() {
var res = qual.verify_i18n_one(options, '{\n"KEY_1": "value"\n}');
assert.equal(errors.length, 0);
assert.isFalse(res.failure);
assert.deepEqual(res.keys, ['KEY_1']);
assert.deepEqual(res.all, {KEY_1: 'value'});
});
});