Skip to content

Commit b08a8e7

Browse files
committed
Refactor tests.
1 parent c052001 commit b08a8e7

File tree

4 files changed

+126
-129
lines changed

4 files changed

+126
-129
lines changed

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@
1818
],
1919
"author": "Isaac Z. Schlueter <[email protected]> (http://blog.izs.me)",
2020
"license": "BSD",
21-
"readmeFilename": "README.md"
21+
"readmeFilename": "README.md",
22+
23+
"devDependencies": {
24+
"mocha": ">= 2.1.0 < 3",
25+
"must": ">= 0.12 < 0.13",
26+
"sinon": ">= 1.12.2 < 2"
27+
}
2228
}

test.js

-128
This file was deleted.

test/mocha.opts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--recursive
2+
--require must

test/stringify_test.js

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
var Sinon = require("sinon")
2+
var stringify = require("..")
3+
4+
describe("Stringify", function() {
5+
it("must stringify circular objects", function() {
6+
var obj = {a: "b"}
7+
obj.self = obj
8+
obj.selves = [obj, obj]
9+
10+
stringify(obj, null, 2).must.equal(JSON.stringify({
11+
a: "b",
12+
self: "[Circular ~]",
13+
selves: ["[Circular ~]", "[Circular ~]"]
14+
}, null, 2))
15+
})
16+
17+
it("must stringify complex circular objects", function() {
18+
var a = {x: 1}; a.a = a
19+
var b = {x: 2}; b.a = a
20+
21+
var obj = {}
22+
obj.self = obj
23+
obj.list = [a, b, {a: a, b: b}]
24+
25+
stringify(obj, null, 2).must.equal(JSON.stringify({
26+
"self": "[Circular ~]",
27+
28+
"list": [
29+
{"x": 1, "a": "[Circular ~.list.0]"},
30+
{"x": 2, "a": "[Circular ~.list.0]"},
31+
{"a": "[Circular ~.list.0]", "b": "[Circular ~.list.1]"}
32+
]
33+
}, null, 2))
34+
})
35+
36+
it("must call given decycler and use its output", function() {
37+
var obj = {}
38+
obj.a = obj
39+
obj.b = obj
40+
41+
var decycle = Sinon.spy(function() { return decycle.callCount })
42+
var json = stringify(obj, null, 2, decycle)
43+
json.must.equal(JSON.stringify({a: 1, b: 2}, null, 2))
44+
45+
decycle.callCount.must.equal(2)
46+
decycle.args[0][0].must.equal("a")
47+
decycle.args[0][1].must.equal(obj)
48+
decycle.args[1][0].must.equal("b")
49+
decycle.args[1][1].must.equal(obj)
50+
})
51+
52+
it("must call given decycler and use its output for nested objects",
53+
function() {
54+
var obj = {}
55+
obj.a = obj
56+
obj.b = {self: obj}
57+
58+
var decycle = Sinon.spy(function() { return decycle.callCount })
59+
var json = stringify(obj, null, 2, decycle)
60+
json.must.equal(JSON.stringify({a: 1, b: {self: 2}}, null, 2))
61+
62+
decycle.callCount.must.equal(2)
63+
decycle.args[0][0].must.equal("a")
64+
decycle.args[0][1].must.equal(obj)
65+
decycle.args[1][0].must.equal("self")
66+
decycle.args[1][1].must.equal(obj)
67+
})
68+
69+
it("must use decycler's output when it returned null", function() {
70+
var obj = {a: "b"}
71+
obj.self = obj
72+
obj.selves = [obj, obj]
73+
74+
function decycle() { return null }
75+
stringify(obj, null, 2, decycle).must.equal(JSON.stringify({
76+
a: "b",
77+
self: null,
78+
selves: [null, null]
79+
}, null, 2))
80+
})
81+
82+
it("must use decycler's output when it returned undefined", function() {
83+
var obj = {a: "b"}
84+
obj.self = obj
85+
obj.selves = [obj, obj]
86+
87+
function decycle() {}
88+
stringify(obj, null, 2, decycle).must.equal(JSON.stringify({
89+
a: "b",
90+
selves: [null, null]
91+
}, null, 2))
92+
})
93+
94+
it("must throw given a decycler that returns a cycle", function() {
95+
var obj = {}
96+
obj.self = obj
97+
var err
98+
function identity(key, value) { return value }
99+
try { stringify(obj, null, 2, identity) } catch (ex) { err = ex }
100+
err.must.be.an.instanceof(TypeError)
101+
})
102+
103+
describe(".getSerialize", function() {
104+
it("must stringify circular objects", function() {
105+
var obj = {a: "b"}
106+
obj.circularRef = obj
107+
obj.list = [obj, obj]
108+
109+
var json = JSON.stringify(obj, stringify.getSerialize(), 2)
110+
json.must.equal(JSON.stringify({
111+
"a": "b",
112+
"circularRef": "[Circular ~]",
113+
"list": ["[Circular ~]", "[Circular ~]"]
114+
}, null, 2))
115+
})
116+
})
117+
})

0 commit comments

Comments
 (0)