Added some unit tests

This commit is contained in:
jos 2015-02-28 15:04:46 +01:00
parent e7cfd2c05d
commit 86e15f74e2
3 changed files with 48 additions and 3 deletions

View File

@ -18,7 +18,7 @@
"bugs": "https://github.com/josdejong/jsoneditor/issues", "bugs": "https://github.com/josdejong/jsoneditor/issues",
"scripts": { "scripts": {
"build": "gulp", "build": "gulp",
"build-assets": "gulp build-assets" "test": "mocha test"
}, },
"dependencies": { "dependencies": {
"brace": "^0.4.1", "brace": "^0.4.1",
@ -31,6 +31,7 @@
"gulp-shell": "^0.3.0", "gulp-shell": "^0.3.0",
"gulp-util": "^3.0.3", "gulp-util": "^3.0.3",
"mkdirp": "^0.5.0", "mkdirp": "^0.5.0",
"mocha": "^2.1.0",
"uglify-js": "^2.4.16", "uglify-js": "^2.4.16",
"webpack": "^1.5.3" "webpack": "^1.5.3"
} }

View File

@ -54,6 +54,7 @@ exports.sanitize = function (jsString) {
chars.push(c); chars.push(c);
i++; i++;
} }
var jsonString = chars.join(''); var jsonString = chars.join('');
// replace unescaped single quotes with double quotes, // replace unescaped single quotes with double quotes,
@ -71,11 +72,11 @@ exports.sanitize = function (jsString) {
jsonString = jsonString.replace(/\/\*(.|[\r\n])*?\*\//g,'');//Remove all code comments jsonString = jsonString.replace(/\/\*(.|[\r\n])*?\*\//g,'');//Remove all code comments
//If JSON starts with a function (Carachters/digist/"_-"), remove this function. //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"}] //For example: function_12321321 ( [{"a":"b"}] ); => [{"a":"b"}]
var match = jsonString.match(/^\s*[\dA-z_$]+\s*\(([\s\S]*)\)\s*;?\s*$/); var match = jsonString.match(/^\s*[\dA-z_$]+\s*\(([\s\S]*)\)\s*;?\s*$/);
if (match) { if (match) {
var jsonString = match[1]; jsonString = match[1];
} }
return jsonString; return jsonString;

43
test/util.test.js Normal file
View File

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