Skip to content

Commit

Permalink
Added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Feb 28, 2015
1 parent e7cfd2c commit 86e15f7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"bugs": "https://github.com/josdejong/jsoneditor/issues",
"scripts": {
"build": "gulp",
"build-assets": "gulp build-assets"
"test": "mocha test"
},
"dependencies": {
"brace": "^0.4.1",
Expand All @@ -31,6 +31,7 @@
"gulp-shell": "^0.3.0",
"gulp-util": "^3.0.3",
"mkdirp": "^0.5.0",
"mocha": "^2.1.0",
"uglify-js": "^2.4.16",
"webpack": "^1.5.3"
}
Expand Down
5 changes: 3 additions & 2 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ exports.sanitize = function (jsString) {
chars.push(c);
i++;
}

var jsonString = chars.join('');

// replace unescaped single quotes with double quotes,
Expand All @@ -71,11 +72,11 @@ exports.sanitize = function (jsString) {
jsonString = jsonString.replace(/\/\*(.|[\r\n])*?\*\//g,'');//Remove all code comments

//If JSON starts with a function (Carachters/digist/"_-"), remove this function.
//This is usefull for "stripping" JSONP objects to become JSON
//This is useful for "stripping" JSONP objects to become JSON
//For example: function_12321321 ( [{"a":"b"}] ); => [{"a":"b"}]
var match = jsonString.match(/^\s*[\dA-z_$]+\s*\(([\s\S]*)\)\s*;?\s*$/);
if (match) {
var jsonString = match[1];
jsonString = match[1];
}

return jsonString;
Expand Down
43 changes: 43 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var assert = require('assert');
var util = require('../src/js/util');

describe('util', function () {

describe('sanitize', function () {

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"}');

// handle escape characters
assert.equal(util.sanitize('{a:"foo\'bar"}'), '{"a":"foo\'bar"}');
assert.equal(util.sanitize('{a:"foo\\"bar"}'), '{"a":"foo\\"bar"}');
assert.equal(util.sanitize('{a:\'foo"bar\'}'), '{"a":"foo\\"bar"}');
assert.equal(util.sanitize('{a:"foo\\\'bar"}'), '{"a":"foo\'bar"}');
});

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 ( {} )'), ' {} ');

// 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
});

0 comments on commit 86e15f7

Please sign in to comment.