1 | describe('JSON', function () {
|
2 | var functionsHaveNames = (function foo() {}).name === 'foo';
|
3 | var ifFunctionsHaveNamesIt = functionsHaveNames ? it : xit;
|
4 | var hasSymbols = typeof Symbol === 'function' && typeof Symbol() === 'symbol';
|
5 | var ifSymbolsIt = hasSymbols ? it : xit;
|
6 | var ifSymbolsDescribe = hasSymbols ? describe : xit;
|
7 |
|
8 | describe('.stringify()', function () {
|
9 | ifFunctionsHaveNamesIt('has the right name', function () {
|
10 | expect(JSON.stringify.name).to.equal('stringify');
|
11 | });
|
12 |
|
13 | ifSymbolsIt('serializes a Symbol to undefined', function () {
|
14 | expect(JSON.stringify(Symbol())).to.equal(undefined);
|
15 | });
|
16 |
|
17 | ifSymbolsIt('serializes a Symbol object to {}', function () {
|
18 | expect(function stringifyObjectSymbol() { JSON.stringify(Object(Symbol())); }).not.to['throw']();
|
19 | expect(JSON.stringify(Object(Symbol()))).to.equal('{}');
|
20 | });
|
21 |
|
22 | ifSymbolsIt('serializes Symbols in an Array to null', function () {
|
23 | expect(JSON.stringify([Symbol('foo')])).to.equal('[null]');
|
24 | });
|
25 |
|
26 | ifSymbolsIt('serializes Symbol objects in an Array to {}', function () {
|
27 | expect(function stringifyObjectSymbolInArray() { JSON.stringify([Object(Symbol('foo'))]); }).not.to['throw']();
|
28 | expect(JSON.stringify([Object(Symbol('foo'))])).to.equal('[{}]');
|
29 | });
|
30 |
|
31 | ifSymbolsDescribe('skips symbol properties and values in an object', function () {
|
32 | var enumSym = Symbol('enumerable');
|
33 | var nonenum = Symbol('non-enumerable');
|
34 | var createObject = function () {
|
35 | var obj = { a: 1 };
|
36 | obj[enumSym] = true;
|
37 | obj.sym = enumSym;
|
38 | Object.defineProperty(obj, nonenum, { enumerable: false, value: true });
|
39 | expect(Object.getOwnPropertySymbols(obj)).to.eql([enumSym, nonenum]);
|
40 | return obj;
|
41 | };
|
42 |
|
43 | it('works with no replacer', function () {
|
44 | var obj = createObject();
|
45 | expect(JSON.stringify(obj)).to.equal('{"a":1}');
|
46 | expect(JSON.stringify(obj, null, '|')).to.equal('{\n|"a": 1\n}');
|
47 | });
|
48 |
|
49 | it('works with a replacer function', function () {
|
50 | var tuples = [];
|
51 | var replacer = function (key, value) {
|
52 | tuples.push([this, key, value]);
|
53 | return value;
|
54 | };
|
55 | var obj = createObject();
|
56 | expect(JSON.stringify(obj, replacer, '|')).to.equal('{\n|"a": 1\n}');
|
57 | expect(tuples).to.eql([
|
58 | [{ '': obj }, '', obj],
|
59 | [obj, 'a', 1],
|
60 | [obj, 'sym', enumSym]
|
61 | ]);
|
62 | });
|
63 |
|
64 | it('works with a replacer array', function () {
|
65 | var obj = createObject();
|
66 | obj.foo = 'bar';
|
67 | obj[Symbol.prototype.toString.call(enumSym)] = 'tricksy';
|
68 | expect(JSON.stringify(obj, ['a', enumSym])).to.equal('{"a":1}');
|
69 | expect(JSON.stringify(obj, ['a', enumSym], '|')).to.equal('{\n|"a": 1\n}');
|
70 | });
|
71 |
|
72 | it('ignores a non-array non-callable replacer object', function () {
|
73 | expect(JSON.stringify('z', { test: null })).to.equal('"z"');
|
74 | });
|
75 | });
|
76 | });
|
77 | });
|