forked from flowersinthesand/stringifyJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
93 lines (86 loc) · 2.65 KB
/
test.js
File metadata and controls
93 lines (86 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function setup() {
var i;
this.JSON = window.JSON;
window.JSON = undefined;
this.toJSON = {
Boolean: Boolean.prototype.toJSON,
Number: Number.prototype.toJSON,
String: String.prototype.toJSON,
Function: Function.prototype.toJSON,
Array: Array.prototype.toJSON,
Date: Date.prototype.toJSON,
RegExp: RegExp.prototype.toJSON,
Object: Object.prototype.toJSON
};
for (i in this.toJSON) {
if (Object.prototype.hasOwnProperty.call(this.toJSON, i)) {
window[i].prototype.toJSON = undefined;
}
}
}
function teardown() {
var i;
window.JSON = this.JSON;
for (i in this.toJSON) {
if (Object.prototype.hasOwnProperty.call(this.toJSON, i)) {
window[i].prototype.toJSON = this.toJSON[i];
}
}
}
module("stringifyJSON", {setup: setup, teardown: teardown});
$.each({
chars: {
value: ["안녕", "Güete Tag", "你好", "Hello"],
answer: ['"안녕"', '"Güete Tag"', '"你好"', '"Hello"']
},
char: {
value: ['\"', '\\', '\/', '\b', '\f', '\n', '\r', '\t'],
answer: ['"\\""', '"\\\\"', '"/"', '"\\b"', '"\\f"', '"\\n"', '"\\r"', '"\\t"']
},
nonfinite: {
value: [NaN, Infinity, -NaN, -Infinity],
answer: ["null", "null", "null", "null"]
},
zero: {
value: [0],
answer: ["0"]
},
integer: {
value: [1, -2, 3, -4, 5, -6, 7, -8, 9],
answer: ["1", "-2", "3", "-4", "5", "-6", "7", "-8", "9"]
},
fractional: {
value: [0.1, -0.2, 0.3, -0.4, 0.5, -0.6, 0.7, -0.8, 0.9],
answer: ["0.1", "-0.2", "0.3", "-0.4", "0.5", "-0.6", "0.7", "-0.8", "0.9"]
},
exponential: {
value: [1e+20, -1E+20, 1e+21, -1E+21, 1e21, -1E21, 1e-6, -1E-6, 1e-7, -1E-7],
answer: ["100000000000000000000", "-100000000000000000000", "1e+21", "-1e+21", "1e+21", "-1e+21", "0.000001", "-0.000001", "1e-7", "-1e-7"]
},
boolean: {
value: [true, false],
answer: ["true", "false"]
},
date: {
value: [new Date(1322990436623)],
answer: ['"2011-12-04T09:20:36Z"']
},
misc: {
value: [null, undefined, $.noop, /regexp/, {toJSON: function() {return "?";}}],
answer: ["null", undefined, undefined, "{}", '"?"']
},
array: {
value: [[], ["array", 'a', NaN, 0, true, null, undefined, []]],
answer: ["[]", '["array","a",null,0,true,null,null,[]]']
},
object: {
value: [{}, {string: "array", "NaN": NaN, zero: 0, boolean: true, "null": null, "undefined": undefined, array: [], nested: {}, fn: $.noop}],
answer: ["{}", '{"string":"array","NaN":null,"zero":0,"boolean":true,"null":null,"array":[],"nested":{}}']
}
}, function(testName, data) {
test(testName, function() {
$.each(data.value, function(i, val) {
deepEqual(stringifyJSON(val), data.answer[i]);
});
});
});