json.js 3.0 KB

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