1 | define(["doh/main", "../json", "../has"], function(doh, JSON, has){ |
---|
2 | |
---|
3 | var mustThrow = function(json){ |
---|
4 | try{ |
---|
5 | JSON.parse(json, true); |
---|
6 | }catch(e){ |
---|
7 | return; |
---|
8 | } |
---|
9 | throw new Error("Invalid JSON " + json + " should have been rejected"); |
---|
10 | }; |
---|
11 | |
---|
12 | doh.register("tests.json", [ |
---|
13 | // all tests below are taken from #4.2 of the CSS3 Color Module |
---|
14 | function simpleString(t){ t.is("bar", JSON.parse('{"foo":"bar"}').foo); }, |
---|
15 | function simpleTrue(t){ t.is(true, JSON.parse('{"foo":true}').foo); }, |
---|
16 | function simpleFalse(t){ t.is(false, JSON.parse('{"foo":false}').foo); }, |
---|
17 | function simpleNull(t){ t.is(null, JSON.parse('{"foo":null}').foo); }, |
---|
18 | function simpleNumber(t){ t.is(3.3, JSON.parse('{"foo":3.3}', true).foo); }, |
---|
19 | function strictString(t){ t.is("bar", JSON.parse('{"foo":"bar"}', true).foo); }, |
---|
20 | function strictEmptyString(t){ t.is("", JSON.parse('{"foo":""}', true).foo); }, |
---|
21 | function strictStringEsc(t){ t.is("b\n\t\"ar()", JSON.parse('{"foo":"b\\n\\t\\"ar()"}', true).foo); }, |
---|
22 | function strictTrue(t){ t.is(true, JSON.parse('{"foo":true}', true).foo); }, |
---|
23 | function strictFalse(t){ t.is(false, JSON.parse('{"foo":false}', true).foo); }, |
---|
24 | function strictNull(t){ t.is(null, JSON.parse('{"foo":null}', true).foo); }, |
---|
25 | function strictNumber(t){ t.is(3.3, JSON.parse('{"foo":3.3}', true).foo); }, |
---|
26 | function strictNumberNeg(t){ t.is(-3.3, JSON.parse('{"foo":-3.3}', true).foo); }, |
---|
27 | function exponentNegative(t){ t.is(3.3e-33, JSON.parse('{"foo":3.3e-33}', true).foo); }, |
---|
28 | function exponent(t){ t.is(3.3e33, JSON.parse('{"foo":3.3e33}', true).foo); }, |
---|
29 | function array(t){ t.is(3, JSON.parse('{"foo":[3,true,[]]}', true).foo[0]); }, |
---|
30 | function badCall(t){ mustThrow('{"foo":alert()}'); }, |
---|
31 | function badMath(t){ mustThrow('{"foo":3+4}'); }, |
---|
32 | function badIndex(t){ mustThrow('{"foo":"bar"}[3]'); }, |
---|
33 | function badKey(t){ mustThrow('{foo:"bar"}'); }, |
---|
34 | //function badKey2(t){ mustThrow('{2:"bar"}')}, |
---|
35 | function badUnbalanced(t){ mustThrow('['); }, |
---|
36 | function badUnbalanced2(t){ mustThrow('}'); }, |
---|
37 | function badType(t){ mustThrow('["foo":"bar"]'); }, |
---|
38 | function badUnbalanced2(t){ mustThrow('}'); }, |
---|
39 | function serializeString(t){ t.is('{"foo":"bar"}', JSON.stringify({"foo":"bar"})); }, |
---|
40 | function serializeNull(t){ t.is('{"foo":null}', JSON.stringify({"foo":null})); }, |
---|
41 | function serializeFunction(t){ t.is('{}', JSON.stringify({"foo":function(){}})); }, |
---|
42 | function serializeNaN(t){ t.is('{"foo":null}', JSON.stringify({"foo":NaN})); }, |
---|
43 | function serializeInfinity(t){ t.is('{"foo":null}', JSON.stringify({"foo":Infinity})); }, |
---|
44 | // there is differences in how many decimals of accuracies in seconds in how Dates are serialized between browsers |
---|
45 | function serializeDate(t){ t.t(/1970-01-01T00:00:00.*Z/.test(JSON.parse(JSON.stringify({"foo":new Date(1)})).foo)); }, |
---|
46 | function serializeInherited(t){ |
---|
47 | function FooBar() { this.foo = "foo"; } |
---|
48 | FooBar.prototype.bar = "bar"; |
---|
49 | t.is('{"foo":"foo"}', JSON.stringify(new FooBar())); |
---|
50 | }, |
---|
51 | /*Apparently Firefox doesn't pass the key to the toJSON method*/ |
---|
52 | function serializeToJSON(t){ t.is('{"foo":{"name":"value"}}', JSON.stringify({foo:{toJSON:function(key){return {name:"value"}; }}})); } |
---|
53 | ]); |
---|
54 | |
---|
55 | if(!has("host-rhino")){ |
---|
56 | doh.register("tests.json.circular", [ |
---|
57 | function serializeCircular(t){ |
---|
58 | try{ |
---|
59 | var a = {}; |
---|
60 | a.a = a; |
---|
61 | console.log("circular: " + JSON.stringify(a)); |
---|
62 | }catch(e){ |
---|
63 | return; |
---|
64 | } |
---|
65 | throw new Error("stringify must throw for circular references"); |
---|
66 | |
---|
67 | } |
---|
68 | ]); |
---|
69 | } |
---|
70 | |
---|
71 | var smallDataSet = { |
---|
72 | prop1: null, |
---|
73 | prop2: true, |
---|
74 | prop3: [], |
---|
75 | prop4: 3.4325222223332266, |
---|
76 | prop5: 10003, |
---|
77 | prop6: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean semper", |
---|
78 | prop7: "sagittis velit. Cras in mi. Duis porta mauris ut ligula. Proin porta rutrum", |
---|
79 | prop8: "lacus. Etiam consequat scelerisque quam. Nulla facilisi. Maecenas luctus", |
---|
80 | prop9: "venenatis nulla. In sit amet dui non mi semper iaculis. Sed molestie", |
---|
81 | prop10: "tortor at ipsum. Morbi dictum rutrum magna. Sed vitae risus." + |
---|
82 | "Aliquam vitae enim. Duis scelerisque metus auctor est venenatis imperdiet." + |
---|
83 | "Fusce dignissim porta augue. Nulla vestibulum. Integer lorem nunc," + |
---|
84 | "ullamcorper a, commodo ac, malesuada sed, dolor. Aenean id mi in massa" + |
---|
85 | "bibendum suscipit. Integer eros. Nullam suscipit mauris. In pellentesque." + |
---|
86 | "Mauris ipsum est, pharetra semper, pharetra in, viverra quis, tellus. Etiam" + |
---|
87 | "purus. Quisque egestas, tortor ac cursus lacinia, felis leo adipiscing" + |
---|
88 | "nisi, et rhoncus elit dolor eget eros. Fusce ut quam. Suspendisse eleifend" + |
---|
89 | "leo vitae ligula. Nulla facilisi." |
---|
90 | }; |
---|
91 | var smallJson = JSON.stringify(smallDataSet); |
---|
92 | |
---|
93 | var i, mediumDataSet = []; |
---|
94 | for(i = 0; i < 20; i++){ |
---|
95 | mediumDataSet.push({ |
---|
96 | prop1: null, |
---|
97 | prop2: true, |
---|
98 | prop3: false, |
---|
99 | prop4: 3.4325222223332266 - i, |
---|
100 | prop5: 10003 + i, |
---|
101 | prop6: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean semper", |
---|
102 | prop7: "sagittis velit. Cras in mi. Duis porta mauris ut ligula. Proin porta rutrum", |
---|
103 | prop8: "lacus. Etiam consequat scelerisque quam. Nulla facilisi. Maecenas luctus", |
---|
104 | prop9: "venenatis nulla. In sit amet dui non mi semper iaculis. Sed molestie", |
---|
105 | prop10: "tortor at ipsum. Morbi dictum rutrum magna. Sed vitae risus." + |
---|
106 | "Aliquam vitae enim." |
---|
107 | }); |
---|
108 | } |
---|
109 | var mediumJson = JSON.stringify(mediumDataSet); |
---|
110 | |
---|
111 | var largeDataSet = []; |
---|
112 | for(i = 0; i < 100; i++){ |
---|
113 | largeDataSet.push({ |
---|
114 | prop1: null, |
---|
115 | prop2: true, |
---|
116 | prop3: false, |
---|
117 | prop4: 3.4325222223332266 - i, |
---|
118 | prop5: ["Mauris ipsum est, pharetra semper, pharetra in, viverra quis, tellus. Etiam" + |
---|
119 | "purus. Quisque egestas, tortor ac cursus lacinia, felis leo adipiscing", |
---|
120 | "nisi, et rhoncus elit dolor eget eros. Fusce ut quam. Suspendisse eleifend" + |
---|
121 | "leo vitae ligula. Nulla facilisi." |
---|
122 | ], |
---|
123 | prop6: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean semper", |
---|
124 | prop7: "sagittis velit. Cras in mi. Duis porta mauris ut ligula. Proin porta rutrum", |
---|
125 | prop8: "lacus. Etiam consequat scelerisque quam. Nulla facilisi. Maecenas luctus", |
---|
126 | prop9: "venenatis nulla. In sit amet dui non mi semper iaculis. Sed molestie", |
---|
127 | prop10: "tortor at ipsum. Morbi dictum rutrum magna. Sed vitae risus." + |
---|
128 | "Aliquam vitae enim. Duis scelerisque metus auctor est venenatis imperdiet." + |
---|
129 | "Fusce dignissim porta augue. Nulla vestibulum. Integer lorem nunc," + |
---|
130 | "ullamcorper a, commodo ac, malesuada sed, dolor. Aenean id mi in massa" + |
---|
131 | "bibendum suscipit. Integer eros. Nullam suscipit mauris. In pellentesque." |
---|
132 | }); |
---|
133 | } |
---|
134 | var largeJson = JSON.stringify(largeDataSet); |
---|
135 | |
---|
136 | doh.register("tests.json.performance", [ |
---|
137 | // all tests below are taken from #4.2 of the CSS3 Color Module |
---|
138 | function small(){ |
---|
139 | var i = 10000; |
---|
140 | while(i-->0){ |
---|
141 | var result = JSON.parse(smallJson); |
---|
142 | } |
---|
143 | }, |
---|
144 | function strictSmall(){ |
---|
145 | var i = 10000; |
---|
146 | while(i-->0){ |
---|
147 | var result = JSON.parse(smallJson, true); |
---|
148 | } |
---|
149 | }, |
---|
150 | function medium(){ |
---|
151 | var i = 1000; |
---|
152 | while(i-->0){ |
---|
153 | var result = JSON.parse(mediumJson); |
---|
154 | } |
---|
155 | }, |
---|
156 | function strictMedium(){ |
---|
157 | var i = 1000; |
---|
158 | while(i-->0){ |
---|
159 | var result = JSON.parse(mediumJson, true); |
---|
160 | } |
---|
161 | }, |
---|
162 | function large(){ |
---|
163 | var i = 100; |
---|
164 | while(i-->0){ |
---|
165 | var result = JSON.parse(largeJson); |
---|
166 | } |
---|
167 | }, |
---|
168 | function strictLarge(){ |
---|
169 | var i = 100; |
---|
170 | while(i-->0){ |
---|
171 | var result = JSON.parse(largeJson, true); |
---|
172 | } |
---|
173 | } |
---|
174 | ]); |
---|
175 | |
---|
176 | }); |
---|
177 | |
---|