forked from josdejong/jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.test.js
61 lines (49 loc) · 2.48 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
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('{a:\'foo\'}'), '{"a":"foo"}');
// 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('remove comments', function () {
assert.equal(util.sanitize('/* foo */ {}'), ' {}');
// 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({}');
});
});
// TODO: thoroughly test all util methods
});