8000 Initial drop of tests extracted from express-state · imclab/serialize-javascript@0bfce74 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bfce74

Browse files
committed
Initial drop of tests extracted from express-state
1 parent d8c3342 commit 0bfce74

File tree

4 files changed

+221
-2
lines changed

4 files changed

+221
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
artifacts/
2+
coverage/
13
node_modules/

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Serialize JavaScript to a superset of JSON that includes regular expressions and functions.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"benchmark": "node test/benchmark/serialize.js",
8+
"test": "istanbul cover -- ./node_modules/mocha/bin/_mocha test/unit/ --reporter spec"
89
},
910
"repository": {
1011
"type": "git",
@@ -22,5 +23,12 @@
2223
"bugs": {
2324
"url": "https://github.com/yahoo/serialize-javascript/issues"
2425
},
25-
"homepage": "https://github.com/yahoo/serialize-javascript"
26+
"homepage": "https://github.com/yahoo/serialize-javascript",
27+
"devDependencies": {
28+
"benchmark": "^1.0.0",
29+
"chai": "^1.9.1",
30+
"istanbul": "^0.3.2",
31+
"mocha": "^1.21.4",
32+
"xunit-file": "0.0.5"
33+
}
2634
}

test/benchmark/serialize.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
var Benchmark = require('benchmark'),
4+
serialize = require('../../');
5+
6+
var suiteConfig = {
7+
onStart: function (e) {
8+
console.log(e.currentTarget.name + ':');
9+
},
10+
11+
onCycle: function (e) {
12+
console.log(String(e.target));
13+
},
14+
15+
onComplete: function () {
16+
console.log('');
17+
}
18+
};
19+
20+
// -- simpleOjb ----------------------------------------------------------------
21+
22+
var simpleObj = {
23+
foo: 'foo',
24+
bar: false,
25+
num: 100,
26+
arr: [1, 2, 3, 4],
27+
obj: {baz: 'baz'}
28+
};
29+
30+
new Benchmark.Suite('simpleObj', suiteConfig)
31+
.add('JSON.stringify( simpleObj )', function () {
32+
JSON.stringify(simpleObj);
33+
})
34+
.add('serialize( simpleObj )', function () {
35+
serialize(simpleObj);
36+
})
37+
.run();

test/unit/serialize.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/* global describe, it, beforeEach */
2+
'use strict';
3+
4+
var serialize = require('../../'),
5+
expect = require('chai').expect;
6+
7+
describe('serialize( obj )', function () {
8+
it('should be a function', function () {
9+
expect(serialize).to.be.a('function');
10+
});
11+
12+
describe('undefined', function () {
13+
it('should serialize `undefined` to a string', function () {
14+
expect(serialize()).to.be.a('string').equal('undefined');
15+
expect(serialize(undefined)).to.be.a('string').equal('undefined');
16+
});
17+
18+
it('should deserialize "undefined" to `undefined`', function () {
19+
expect(eval(serialize())).to.equal(undefined);
20+
expect(eval(serialize(undefined))).to.equal(undefined);
21+
});
22+
});
23+
24+
describe('null', function () {
25+
it('should serialize `null` to a string', function () {
26+
expect(serialize(null)).to.be.a('string').equal('null');
27+
});
28+
29+
it('should deserialize "null" to `null`', function () {
30+
expect(eval(serialize(null))).to.equal(null);
31+
});
32+
});
33+
34+
describe('JSON', function () {
35+
var data;
36+
37+
beforeEach(function () {
38+
data = {
39+
str : 'string',
40+
num : 0,
41+
obj : {foo: 'foo'},
42+
arr : [1, 2, 3],
43+
bool: true,
44+
nil : null
45+
};
46+
});
47+
48+
it('should serialize JSON to a JSON string', function () {
49+
expect(serialize(data)).to.equal(JSON.stringify(data));
50+
});
51+
52+
it('should deserialize a JSON string to a JSON object', function () {
53+
expect(JSON.parse(serialize(data))).to.deep.equal(data);
54+
});
55+
56+
it('should serialize weird whitespace characters correctly', function () {
57+
var ws = String.fromCharCode(8232);
58+
expect(eval(serialize(ws))).to.equal(ws);
59+
});
60+
});
61+
62+
describe('functions', function () {
63+
it('should serialize annonymous functions', function () {
64+
var fn = function () {};
65+
expect(serialize(fn)).to.be.a('string').equal('function () {}');
66+
});
67+
68+
it('should deserialize annonymous functions', function () {
69+
var fn; eval('fn = ' + serialize(function () {}));
70+
expect(fn).to.be.a('function');
71+
});
72+
73+
it('should serialize named functions', function () {
74+
function fn() {}
75+
expect(serialize(fn)).to.be.a('string').equal('function fn() {}');
76+
});
77+
78+
it('should deserialize named functions', function () {
79+
var fn; eval('fn = ' + serialize(function fn() {}));
80+
expect(fn).to.be.a('function');
81+
expect(fn.name).to.equal('fn');
82+
});
83+
84+
it('should serialize functions with arguments', function () {
85+
function fn(arg1, arg2) {}
86+
expect(serialize(fn)).to.equal('function fn(arg1, arg2) {}');
87+
});
88+
89+
it('should deserialize functions with arguments', function () {
90+
var fn; eval('fn = ' + serialize(function (arg1, arg2) {}));
91+
expect(fn).to.be.a('function');
92+
expect(fn.length).to.equal(2);
93+
});
94+
95+
it('should serialize functions with bodies', function () {
96+
function fn() { return true; }
97+
expect(serialize(fn)).to.equal('function fn() { return true; }');
98+
});
99+
100+
it('should deserialize functions with bodies', function () {
101+
var fn; eval('fn = ' + serialize(function () { return true; }));
102+
expect(fn).to.be.a('function');
103+
expect(fn()).to.equal(true);
104+
});
105+
106+
it('should throw a TypeError when serializing native built-ins', function () {
107+
var err;
108+
expect(Number.toString()).to.equal('function Number() { [native code] }');
109+
try { serialize(Number); } catch (e) { err = e; }
110+
expect(err).to.be.an.instanceOf(TypeError);
111+
});
112+
});
113+
114+
describe('regexps', function () {
115+
it('should serialize constructed regexps', function () {
116+
var re = new RegExp('asdf');
117+
expect(serialize(re)).to.be.a('string').equal('/asdf/');
118+
});
119+
120+
it('should deserialize constructed regexps', function () {
121+
var re = eval(serialize(new RegExp('asdf')));
122+
expect(re).to.be.a('RegExp');
123+
expect(re.source).to.equal('asdf');
124+
});
125+
126+
it('should serialize literal regexps', function () {
127+
var re = /asdf/;
128+
expect(serialize(re)).to.be.a('string').equal('/asdf/');
129+
});
130+
131+
it('should deserialize literal regexps', function () {
132+
var re = eval(serialize(/asdf/));
133+
expect(re).to.be.a('RegExp');
134+
expect(re.source).to.equal('asdf');
135+
});
136+
137+
it('should serialize regexps with flags', function () {
138+
var re = /^asdf$/gi;
139+
expect(serialize(re)).to.equal('/^asdf$/gi');
140+
});
141+
142+
it('should deserialize regexps with flags', function () {
143+
var re = eval(serialize(/^asdf$/gi));
144+
expect(re).to.be.a('RegExp');
145+
expect(re.global).to.equal(true);
146+
expect(re.ignoreCase).to.equal(true);
147+
expect(re.multiline).to.equal(false);
148+
});
149+
150+
it('should serialize regexps with escaped chars', function () {
151+
expect(serialize(/\..*/)).to.equal('/\\..*/');
152+
expect(serialize(new RegExp('\\..*'))).to.equal('/\\..*/');
153+
});
154+
155+
it('should deserialize regexps with escaped chars', function () {
156+
97AE var re = eval(serialize(/\..*/));
157+
expect(re).to.be.a('RegExp');
158+
expect(re.source).to.equal('\\..*');
159+
re = eval(serialize(new RegExp('\\..*')));
160+
expect(re).to.be.a('RegExp');
161+
expect(re.source).to.equal('\\..*');
162+
});
163+
});
164+
165+
describe('XSS', function () {
166+
it('should encode unsafe HTML chars to Unicode', function () {
167+
expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"');
168+
expect(JSON.parse(serialize('</script>'))).to.equal('</script>');
169+
expect(eval(serialize('</script>'))).to.equal('</script>');
170+
});
171+
});
172+
});

0 commit comments

Comments
 (0)
0