Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: test/mjsunit/json.js

Issue 562034: Updated JSON.stringify to newest version of ES5. (Closed)
Patch Set: Created 10 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/json-delay.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 TestInvalid('"\\a invalid escape"'); 193 TestInvalid('"\\a invalid escape"');
194 TestInvalid('"\\v invalid escape"'); // Valid JavaScript 194 TestInvalid('"\\v invalid escape"'); // Valid JavaScript
195 TestInvalid('"\\\' invalid escape"'); // Valid JavaScript 195 TestInvalid('"\\\' invalid escape"'); // Valid JavaScript
196 TestInvalid('"\\x42 invalid escape"'); // Valid JavaScript 196 TestInvalid('"\\x42 invalid escape"'); // Valid JavaScript
197 TestInvalid('"\\u202 invalid escape"'); 197 TestInvalid('"\\u202 invalid escape"');
198 TestInvalid('"\\012 invalid escape"'); 198 TestInvalid('"\\012 invalid escape"');
199 TestInvalid('"Unterminated string'); 199 TestInvalid('"Unterminated string');
200 TestInvalid('"Unterminated string\\"'); 200 TestInvalid('"Unterminated string\\"');
201 TestInvalid('"Unterminated string\\\\\\"'); 201 TestInvalid('"Unterminated string\\\\\\"');
202 202
203 // JavaScript RegExp literals not valid in JSON.
204 TestInvalid('/true/');
205
203 // Test bad JSON that would be good JavaScript (ES5). 206 // Test bad JSON that would be good JavaScript (ES5).
204
205 TestInvalid("{true:42}"); 207 TestInvalid("{true:42}");
206 TestInvalid("{false:42}"); 208 TestInvalid("{false:42}");
207 TestInvalid("{null:42}"); 209 TestInvalid("{null:42}");
208 TestInvalid("{'foo':42}"); 210 TestInvalid("{'foo':42}");
209 TestInvalid("{42:42}"); 211 TestInvalid("{42:42}");
210 TestInvalid("{0:42}"); 212 TestInvalid("{0:42}");
211 TestInvalid("{-1:42}"); 213 TestInvalid("{-1:42}");
212 214
213 // Test for trailing garbage detection. 215 // Test for trailing garbage detection.
214
215 TestInvalid('42 px'); 216 TestInvalid('42 px');
216 TestInvalid('42 .2'); 217 TestInvalid('42 .2');
217 TestInvalid('42 2'); 218 TestInvalid('42 2');
218 TestInvalid('42 e1'); 219 TestInvalid('42 e1');
219 TestInvalid('"42" ""'); 220 TestInvalid('"42" ""');
220 TestInvalid('"42" ""'); 221 TestInvalid('"42" ""');
221 TestInvalid('"" ""'); 222 TestInvalid('"" ""');
222 TestInvalid('true ""'); 223 TestInvalid('true ""');
223 TestInvalid('false ""'); 224 TestInvalid('false ""');
224 TestInvalid('null ""'); 225 TestInvalid('null ""');
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 var singleton = []; 271 var singleton = [];
271 var multiOccurrence = [singleton, singleton, singleton]; 272 var multiOccurrence = [singleton, singleton, singleton];
272 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); 273 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence));
273 274
274 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); 275 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6}));
275 assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x'])); 276 assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x']));
276 assertEquals('{\n "a": "b",\n "c": "d"\n}', 277 assertEquals('{\n "a": "b",\n "c": "d"\n}',
277 JSON.stringify({a:"b",c:"d"}, null, 1)); 278 JSON.stringify({a:"b",c:"d"}, null, 1));
278 assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x'])); 279 assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x']));
279 280
281 // The gap is capped at ten characters if specified as string.
282 assertEquals('{\n "a": "b",\n "c": "d"\n}',
283 JSON.stringify({a:"b",c:"d"}, null,
284 " /*characters after 10th*/"));
285
286 //The gap is capped at ten characters if specified as number.
287 assertEquals('{\n "a": "b",\n "c": "d"\n}',
288 JSON.stringify({a:"b",c:"d"}, null, 15));
289
290 // Replaced wrapped primitives are unwrapped.
291 function newx(k, v) { return (k == "x") ? new v(42) : v; }
292 assertEquals('{"x":"42"}', JSON.stringify({x: String}, newx));
293 assertEquals('{"x":42}', JSON.stringify({x: Number}, newx));
294 assertEquals('{"x":true}', JSON.stringify({x: Boolean}, newx));
295
280 assertEquals(undefined, JSON.stringify(undefined)); 296 assertEquals(undefined, JSON.stringify(undefined));
281 assertEquals(undefined, JSON.stringify(function () { })); 297 assertEquals(undefined, JSON.stringify(function () { }));
282 298
Rico 2010/02/03 12:57:23 Maybe check for stringify on an array with undefin
Lasse Reichstein 2010/02/03 13:18:30 Check added for behavior of undefined and function
299 assertThrows("var a = [];a[0] = a;JSON.stringify(a);", TypeError);
Rico 2010/02/03 12:57:23 Don't we check the same in line 269
Lasse Reichstein 2010/02/03 13:18:30 We do. Removed.
300
283 TestInvalid('1); throw "foo"; (1'); 301 TestInvalid('1); throw "foo"; (1');
284 302
285 var x = 0; 303 var x = 0;
286 eval("(1); x++; (1)"); 304 eval("(1); x++; (1)");
287 TestInvalid('1); x++; (1'); 305 TestInvalid('1); x++; (1');
OLDNEW
« no previous file with comments | « src/json-delay.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698