forked from josdejong/jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.test.js
299 lines (252 loc) · 12.9 KB
/
util.test.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
var assert = require('assert');
var util = require('../src/js/util');
describe('util', function () {
describe('sanitize', function () {
it('should leave valid JSON as is', function () {
assert.equal(util.sanitize('{"a":2}'), '{"a":2}');
});
it('should replace JavaScript with JSON', function () {
assert.equal(util.sanitize('{a:2}'), '{"a":2}');
assert.equal(util.sanitize('{a: 2}'), '{"a": 2}');
assert.equal(util.sanitize('{\n a: 2\n}'), '{\n "a": 2\n}');
assert.equal(util.sanitize('{\'a\':2}'), '{"a":2}');
assert.equal(util.sanitize('{a:\'foo\'}'), '{"a":"foo"}');
assert.equal(util.sanitize('{a:\'foo\',b:\'bar\'}'), '{"a":"foo","b":"bar"}');
// should leave string content untouched
assert.equal(util.sanitize('"{a:b}"'), '"{a:b}"');
});
it('should add/remove escape characters', function () {
assert.equal(util.sanitize('"foo\'bar"'), '"foo\'bar"');
assert.equal(util.sanitize('"foo\\"bar"'), '"foo\\"bar"');
assert.equal(util.sanitize('\'foo"bar\''), '"foo\\"bar"');
assert.equal(util.sanitize('\'foo\\\'bar\''), '"foo\'bar"');
assert.equal(util.sanitize('"foo\\\'bar"'), '"foo\'bar"');
});
it('should replace special white characters', function () {
assert.equal(util.sanitize('{"a":\u00a0"foo\u00a0bar"}'), '{"a": "foo\u00a0bar"}');
assert.equal(util.sanitize('{"a":\u2009"foo"}'), '{"a": "foo"}');
});
it('should escape unescaped control characters', function () {
assert.equal(util.sanitize('"hello\bworld"'), '"hello\\bworld"')
assert.equal(util.sanitize('"hello\fworld"'), '"hello\\fworld"')
assert.equal(util.sanitize('"hello\nworld"'), '"hello\\nworld"')
assert.equal(util.sanitize('"hello\rworld"'), '"hello\\rworld"')
assert.equal(util.sanitize('"hello\tworld"'), '"hello\\tworld"')
assert.equal(util.sanitize('{"value\n": "dc=hcm,dc=com"}'), '{"value\\n": "dc=hcm,dc=com"}')
})
it('should replace left/right quotes', function () {
assert.equal(util.sanitize('\u2018foo\u2019'), '"foo"')
assert.equal(util.sanitize('\u201Cfoo\u201D'), '"foo"')
assert.equal(util.sanitize('\u0060foo\u00B4'), '"foo"')
});
it('remove comments', function () {
assert.equal(util.sanitize('/* foo */ {}'), ' {}');
assert.equal(util.sanitize('/* foo */ {}'), ' {}');
assert.equal(util.sanitize('{a:\'foo\',/*hello*/b:\'bar\'}'), '{"a":"foo","b":"bar"}');
assert.equal(util.sanitize('{\na:\'foo\',//hello\nb:\'bar\'\n}'), '{\n"a":"foo",\n"b":"bar"\n}');
// should not remove comments in string
assert.equal(util.sanitize('{"str":"/* foo */"}'), '{"str":"/* foo */"}');
});
it('should strip JSONP notation', function () {
// matching
assert.equal(util.sanitize('callback_123({});'), '{}');
assert.equal(util.sanitize('callback_123([]);'), '[]');
assert.equal(util.sanitize('callback_123(2);'), '2');
assert.equal(util.sanitize('callback_123("foo");'), '"foo"');
assert.equal(util.sanitize('callback_123(null);'), 'null');
assert.equal(util.sanitize('callback_123(true);'), 'true');
assert.equal(util.sanitize('callback_123(false);'), 'false');
assert.equal(util.sanitize('/* foo bar */ callback_123 ({})'), '{}');
assert.equal(util.sanitize('/* foo bar */ callback_123 ({})'), '{}');
assert.equal(util.sanitize('/* foo bar */\ncallback_123({})'), '{}');
assert.equal(util.sanitize('/* foo bar */ callback_123 ( {} )'), ' {} ');
assert.equal(util.sanitize(' /* foo bar */ callback_123 ({}); '), '{}');
assert.equal(util.sanitize('\n/* foo\nbar */\ncallback_123 ({});\n\n'), '{}');
// non-matching
assert.equal(util.sanitize('callback abc({});'), 'callback abc({});');
assert.equal(util.sanitize('callback {}'), 'callback {}');
assert.equal(util.sanitize('callback({}'), 'callback({}');
});
it('should strip trailing zeros', function () {
// matching
assert.equal(util.sanitize('[1,2,3,]'), '[1,2,3]');
assert.equal(util.sanitize('[1,2,3,\n]'), '[1,2,3\n]');
assert.equal(util.sanitize('[1,2,3, \n ]'), '[1,2,3 \n ]');
assert.equal(util.sanitize('{"a":2,}'), '{"a":2}');
// not matching
assert.equal(util.sanitize('"[1,2,3,]"'), '"[1,2,3,]"');
assert.equal(util.sanitize('"{a:2,}"'), '"{a:2,}"');
});
});
describe('jsonPath', function () {
it('should stringify an array of paths', function() {
assert.deepStrictEqual(util.stringifyPath([]), '');
assert.deepStrictEqual(util.stringifyPath(['foo']), '.foo');
assert.deepStrictEqual(util.stringifyPath(['foo', 'bar']), '.foo.bar');
assert.deepStrictEqual(util.stringifyPath(['foo', 2]), '.foo[2]');
assert.deepStrictEqual(util.stringifyPath(['foo', 2, 'bar']), '.foo[2].bar');
assert.deepStrictEqual(util.stringifyPath(['foo', 2, 'bar_baz']), '.foo[2].bar_baz');
assert.deepStrictEqual(util.stringifyPath([2]), '[2]');
assert.deepStrictEqual(util.stringifyPath(['foo', 'prop-with-hyphens']), '.foo["prop-with-hyphens"]');
assert.deepStrictEqual(util.stringifyPath(['foo', 'prop with spaces']), '.foo["prop with spaces"]');
})
it ('should parse a json path', function () {
assert.deepStrictEqual(util.parsePath(''), []);
assert.deepStrictEqual(util.parsePath('.foo'), ['foo']);
assert.deepStrictEqual(util.parsePath('.foo.bar'), ['foo', 'bar']);
assert.deepStrictEqual(util.parsePath('.foo[2]'), ['foo', 2]);
assert.deepStrictEqual(util.parsePath('.foo[2].bar'), ['foo', 2, 'bar']);
assert.deepStrictEqual(util.parsePath('.foo["prop with spaces"]'), ['foo', 'prop with spaces']);
assert.deepStrictEqual(util.parsePath('.foo[\'prop with single quotes as outputted by ajv library\']'), ['foo', 'prop with single quotes as outputted by ajv library']);
assert.deepStrictEqual(util.parsePath('.foo["prop with . dot"]'), ['foo', 'prop with . dot']);
assert.deepStrictEqual(util.parsePath('.foo["prop with ] character"]'), ['foo', 'prop with ] character']);
assert.deepStrictEqual(util.parsePath('.foo[*].bar'), ['foo', '*', 'bar']);
assert.deepStrictEqual(util.parsePath('[2]'), [2]);
});
it ('should throw an exception in case of an invalid path', function () {
assert.throws(function () {util.parsePath('.')}, /Invalid JSON path: property name expected at index 1/);
assert.throws(function () {util.parsePath('[')}, /Invalid JSON path: unexpected end, character ] expected/);
assert.throws(function () {util.parsePath('[]')}, /Invalid JSON path: array value expected at index 1/);
assert.throws(function () {util.parsePath('.foo[ ]')}, /Invalid JSON path: array value expected at index 7/);
assert.throws(function () {util.parsePath('.[]')}, /Invalid JSON path: property name expected at index 1/);
assert.throws(function () {util.parsePath('["23]')}, /Invalid JSON path: unexpected end, character " expected/);
assert.throws(function () {util.parsePath('.foo bar')}, /Invalid JSON path: unexpected character " " at index 4/);
});
});
describe('getIndexForPosition', function () {
var el = {
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
};
it('happy flows - row and column in range', function () {
assert.equal(util.getIndexForPosition(el, 1, 1), 0);
assert.equal(util.getIndexForPosition(el, 2, 1), 124);
assert.equal(util.getIndexForPosition(el, 3, 8), 239);
assert.equal(util.getIndexForPosition(el, 4, 22), 356);
});
it('if range exceeds it should be considered as if it is last row or column length', function () {
assert.equal(util.getIndexForPosition(el, 1, 100000), 123);
assert.equal(util.getIndexForPosition(el, 100000, 1), 335);
assert.equal(util.getIndexForPosition(el, 100000, 100000), 445);
});
it('missing or wrong input sould return -1', function () {
assert.equal(util.getIndexForPosition(el), -1);
assert.equal(util.getIndexForPosition(el, undefined, 1), -1);
assert.equal(util.getIndexForPosition(el, 1, undefined), -1);
assert.equal(util.getIndexForPosition(el, -2, -2), -1);
});
});
describe('get', function () {
it('should get a nested property from an object', function () {
var obj = {
a: {
b: 2
},
c: 3,
d: null,
e: undefined
}
assert.strictEqual(util.get(obj, ['a', 'b']), 2);
assert.strictEqual(util.get(obj, ['c']), 3);
assert.deepStrictEqual(util.get(obj, ['a']), { b: 2});
assert.strictEqual(util.get(obj, ['a', 'foo']), undefined);
assert.strictEqual(util.get(obj, ['a', 'foo', 'bar']), undefined);
assert.strictEqual(util.get(obj, ['d']), null);
assert.strictEqual(util.get(obj, ['d', 'foo', 'bar']), null);
assert.strictEqual(util.get(obj, ['e']), undefined);
})
});
describe('makeFieldTooltip', function () {
it('should return empty string when the schema is missing all relevant fields', function () {
assert.strictEqual(util.makeFieldTooltip({}), '')
assert.strictEqual(util.makeFieldTooltip({additionalProperties: false}), '')
assert.strictEqual(util.makeFieldTooltip(), '')
});
it('should make tooltips with only title', function () {
assert.strictEqual(util.makeFieldTooltip({title: 'foo'}), 'foo');
});
it('should make tooltips with only description', function () {
assert.strictEqual(util.makeFieldTooltip({description: 'foo'}), 'foo');
});
it('should make tooltips with only default', function () {
assert.strictEqual(util.makeFieldTooltip({default: 'foo'}), 'Default\n"foo"');
});
it('should make tooltips with only examples', function () {
assert.strictEqual(util.makeFieldTooltip({examples: ['foo', 'bar']}), 'Examples\n"foo"\n"bar"');
});
it('should make tooltips with title and description', function () {
assert.strictEqual(util.makeFieldTooltip({title: 'foo', description: 'bar'}), 'foo\nbar');
var longTitle = 'Lorem Ipsum Dolor';
var longDescription = 'Duis id elit non ante gravida vestibulum non nec est. ' +
'Proin vitae ligula at elit dapibus tempor. ' +
'Etiam lacinia augue vel condimentum interdum. ';
assert.strictEqual(
util.makeFieldTooltip({title: longTitle, description: longDescription}),
longTitle + '\n' + longDescription
);
});
it('should make tooltips with title, description, and examples', function () {
assert.strictEqual(
util.makeFieldTooltip({title: 'foo', description: 'bar', examples: ['baz']}),
'foo\nbar\n\nExamples\n"baz"',
);
});
it('should make tooltips with title, description, default, and examples', function () {
assert.strictEqual(
util.makeFieldTooltip({title: 'foo', description: 'bar', default: 'bat', examples: ['baz']}),
'foo\nbar\n\nDefault\n"bat"\n\nExamples\n"baz"',
);
});
it('should handle empty fields', function () {
assert.strictEqual(util.makeFieldTooltip({title: '', description: 'bar'}), 'bar');
assert.strictEqual(util.makeFieldTooltip({title: 'foo', description: ''}), 'foo');
assert.strictEqual(util.makeFieldTooltip({description: 'bar', examples: []}), 'bar');
assert.strictEqual(util.makeFieldTooltip({description: 'bar', examples: ['']}), 'bar\n\nExamples\n""');
});
it('should internationalize "Defaults" correctly', function () {
assert.strictEqual(util.makeFieldTooltip({default: 'foo'}, 'pt-BR'), 'Revelia\n"foo"');
});
it('should internationalize "Examples" correctly', function () {
assert.strictEqual(util.makeFieldTooltip({examples: ['foo']}, 'pt-BR'), 'Exemplos\n"foo"');
});
});
it('should find a unique name', function () {
assert.strictEqual(util.findUniqueName('other', [
'a',
'b',
'c'
]), 'other')
assert.strictEqual(util.findUniqueName('b', [
'a',
'b',
'c'
]), 'b (copy)')
assert.strictEqual(util.findUniqueName('b', [
'a',
'b',
'c',
'b (copy)'
]), 'b (copy 2)')
assert.strictEqual(util.findUniqueName('b', [
'a',
'b',
'c',
'b (copy)',
'b (copy 2)'
]), 'b (copy 3)')
assert.strictEqual(util.findUniqueName('b (copy)', [
'a',
'b',
'b (copy)',
'b (copy 2)',
'c'
]), 'b (copy 3)')
assert.strictEqual(util.findUniqueName('b (copy 2)', [
'a',
'b',
'b (copy)',
'b (copy 2)',
'c'
]), 'b (copy 3)')
})
// TODO: thoroughly test all util methods
});