forked from es-shims/es5-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for
JSON#parse
and JSON#stringify
.
- Loading branch information
Xotic750
committed
Nov 18, 2015
1 parent
2a00600
commit 4dd8703
Showing
3 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* global describe, it, expect */ | ||
|
||
describe('JSON.parse', function () { | ||
it('should throw correct Error types', function () { | ||
try { | ||
expect(function () { | ||
JSON.parse(); | ||
}).toThrow(); | ||
} catch (e) { | ||
expect(e instanceof SyntaxError).toBe(true); | ||
} | ||
try { | ||
expect(function () { | ||
JSON.parse(undefined); | ||
}).toThrow(); | ||
} catch (e) { | ||
expect(e instanceof SyntaxError).toBe(true); | ||
} | ||
try { | ||
expect(function () { | ||
JSON.parse(''); | ||
}).toThrow(); | ||
} catch (e) { | ||
expect(e instanceof SyntaxError).toBe(true); | ||
} | ||
try { | ||
expect(function () { | ||
JSON.parse('{"A": undefined}'); | ||
}).toThrow(); | ||
} catch (e) { | ||
expect(e instanceof SyntaxError).toBe(true); | ||
} | ||
}); | ||
it('work with basic primatives', function () { | ||
expect(JSON.parse(null)).toBe(null); | ||
expect(JSON.parse('-1')).toBe(-1); | ||
expect(JSON.parse('0')).toBe(0); | ||
expect(JSON.parse('1')).toBe(1); | ||
expect(JSON.parse(false)).toBe(false); | ||
expect(JSON.parse(true)).toBe(true); | ||
expect(JSON.parse('null')).toBe(null); | ||
}); | ||
it('work with complex object', function () { | ||
var expected = { A: [1, true, false, null, '\u0000\b\n\f\r\t'] }; | ||
var actual = JSON.parse('{"A":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}'); | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('JSON.stringify', function () { | ||
it('cyclic should throw a TypeError', function () { | ||
var a = []; | ||
a[0] = a; | ||
try { | ||
expect(function () { | ||
JSON.stringify(a); | ||
}).to.throwException(function (e) { | ||
expect(e).toBe.a(TypeError); | ||
}); | ||
} catch (e) { | ||
expect(e instanceof TypeError).toBe(true); | ||
} | ||
}); | ||
it('basic', function () { | ||
expect(JSON.stringify(null)).toBe('null'); | ||
expect(JSON.stringify(-1)).toBe('-1'); | ||
expect(JSON.stringify(0)).toBe('0'); | ||
expect(JSON.stringify(1)).toBe('1'); | ||
expect(JSON.stringify(Number(1))).toBe('1'); | ||
expect(JSON.stringify(false)).toBe('false'); | ||
expect(JSON.stringify(true)).toBe('true'); | ||
expect(JSON.stringify(Boolean(true))).toBe('true'); | ||
expect(JSON.stringify()).toBe(undefined); | ||
expect(JSON.stringify(undefined)).toBe(undefined); | ||
expect(JSON.stringify('')).toBe('""'); | ||
expect(JSON.stringify('abc')).toBe('"abc"'); | ||
expect(JSON.stringify(String('abc'))).toBe('"abc"'); | ||
}); | ||
it('known issues', function () { | ||
var noop = function () {}; | ||
var customJSON = function () { | ||
return 1; | ||
}; | ||
customJSON.toJSON = customJSON; | ||
// Firefox 3.1b1 and b2 serialize string, number, and boolean | ||
// primitives as object literals. | ||
expect(JSON.stringify(0)).toBe('0'); | ||
// FF 3.1b1, b2, and JSON 2 serialize wrapped primitives as object | ||
// literals. | ||
expect(JSON.stringify(Object(0))).toBe('0'); | ||
expect(JSON.stringify(Object(''))).toBe('""'); | ||
// FF 3.1b1, 2 throw an error if the toJSON is `null`, `undefined`, or | ||
// does not define a canonical JSON representation (this applies to | ||
// objects with `toJSON` properties as well, *unless* they are nested | ||
// within an object or array). | ||
expect(function () { | ||
JSON.stringify(noop); | ||
}).not.toThrow(); | ||
expect(typeof JSON.stringify(noop)).toBe('undefined'); | ||
// IE 8 serializes `undefined` as `"undefined"`. Safari 5.1.7 and FF | ||
// 3.1b3 pass this test. | ||
expect(typeof JSON.stringify(undefined)).toBe('undefined'); | ||
// Safari <= 5.1.7 and FF 3.1b3 throw `Error`s and `TypeError`s, | ||
// respectively, if the toSON is omitted entirely. | ||
expect(function () { | ||
JSON.stringify(); | ||
}).not.toThrow(); | ||
expect(typeof JSON.stringify()).toBe('undefined'); | ||
// FF 3.1b1, 2 throw an error if the given testTemp.a is not a number, | ||
// string, array, object, Boolean, or `null` literal. This applies to | ||
// objects with custom `toJSON` methods as well, unless they are nested | ||
// inside object or array literals. YUI 3.0.0b1 ignores custom `toJSON` | ||
// methods entirely. | ||
expect(function () { | ||
JSON.stringify(customJSON); | ||
JSON.stringify([customJSON]); | ||
}).not.toThrow(); | ||
expect(JSON.stringify(customJSON)).toBe('1'); | ||
expect(JSON.stringify([customJSON])).toBe('[1]'); | ||
// Prototype <= 1.6.1 serializes `[undefined]` as `"[]"` instead of | ||
// `"[null]"`. | ||
expect(JSON.stringify([undefined])).toBe('[null]'); | ||
// YUI 3.0.0b1 fails to serialize `null` literals. | ||
expect(JSON.stringify(null)).toBe('null'); | ||
// FF 3.1b1, 2 halts serialization if an array contains a function: | ||
// `[1, true, noop, 1]` serializes as "[1,true,],". These versions | ||
// of Firefox also allow trailing commas in JSON objects and arrays. | ||
// FF 3.1b3 elides non-JSON values from objects and arrays, unless they | ||
// define custom `toJSON` methods. | ||
expect(JSON.stringify([undefined, noop, null])).toBe('[null,null,null]'); | ||
// Simple serialization test. FF 3.1b1 uses Unicode escape sequences | ||
// where character escape codes are expected (e.g., `\b` => `\u0008`). | ||
// Removed test for '\0' => '\\'u0000'as Chrome 10 fails in 'use strict' mode with | ||
// Error: Uncaught SyntaxError: Octal literals are not allowed in strict mode. | ||
expect(function () { | ||
JSON.stringify({ | ||
A: [customJSON, true, false, null, '\b\n\f\r\t'] | ||
}); | ||
}).not.toThrow(); | ||
expect(JSON.stringify({ | ||
A: [customJSON, true, false, null, '\b\n\f\r\t'] | ||
})).toBe('{"A":[1,true,false,null,"\\b\\n\\f\\r\\t"]}'); | ||
// FF 3.1b1 and b2 ignore the `filter` and `width` arguments. | ||
//JSON.stringify(null, testTemp.customJSON) === '"1"' && | ||
expect(JSON.stringify([1, 2], null, 1)).toBe('[\n 1,\n 2\n]'); | ||
}); | ||
it('extra', function () { | ||
expect(JSON.stringify({ | ||
A: undefined, | ||
B: null | ||
})).toBe('{"B":null}'); | ||
expect(JSON.stringify({ | ||
A: [1, true, false, null, '\u0000\b\n\f\r\t'] | ||
})).toBe('{"A":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}'); | ||
}); | ||
it('date toJSON', function () { | ||
var date1 = new Date(-8.64e15); | ||
var date2 = new Date(8.64e15); | ||
var date3 = new Date(-621987552e5); | ||
var date4 = new Date(-1); | ||
expect(JSON.stringify(date1)).toBe('"-271821-04-20T00:00:00.000Z"'); | ||
expect(JSON.stringify(date2)).toBe('"+275760-09-13T00:00:00.000Z"'); | ||
expect(JSON.stringify(date3)).toBe('"-000001-01-01T00:00:00.000Z"'); | ||
expect(JSON.stringify(date4)).toBe('"1969-12-31T23:59:59.999Z"'); | ||
date1.toJSON = undefined; | ||
expect(JSON.stringify(date1)).toBe('{}'); | ||
}); | ||
}); |