Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 26 matching lines...) Expand all Loading... | |
| 37 assertThrows(function () { n3.toJSON(); }, TypeError, 'result_not_primitive'); | 37 assertThrows(function () { n3.toJSON(); }, TypeError, 'result_not_primitive'); |
| 38 var n4 = new Constructor(value); | 38 var n4 = new Constructor(value); |
| 39 n4.valueOf = function () { | 39 n4.valueOf = function () { |
| 40 assertEquals(0, arguments.length); | 40 assertEquals(0, arguments.length); |
| 41 assertEquals(this, n4); | 41 assertEquals(this, n4); |
| 42 return null; | 42 return null; |
| 43 }; | 43 }; |
| 44 assertEquals(null, n4.toJSON()); | 44 assertEquals(null, n4.toJSON()); |
| 45 } | 45 } |
| 46 | 46 |
| 47 // Number toJSON | |
| 48 assertEquals(3, (3).toJSON()); | |
| 49 assertEquals(3, (3).toJSON(true)); | |
| 50 assertEquals(4, (new Number(4)).toJSON()); | |
| 51 GenericToJSONChecks(Number, 5, 6); | |
| 52 | |
| 53 // Boolean toJSON | |
| 54 assertEquals(true, (true).toJSON()); | |
| 55 assertEquals(true, (true).toJSON(false)); | |
| 56 assertEquals(false, (false).toJSON()); | |
| 57 assertEquals(true, (new Boolean(true)).toJSON()); | |
| 58 GenericToJSONChecks(Boolean, true, false); | |
| 59 GenericToJSONChecks(Boolean, false, true); | |
| 60 | |
| 61 // String toJSON | |
| 62 assertEquals("flot", "flot".toJSON()); | |
| 63 assertEquals("flot", "flot".toJSON(3)); | |
| 64 assertEquals("tolf", (new String("tolf")).toJSON()); | |
| 65 GenericToJSONChecks(String, "x", "y"); | |
| 66 | |
| 67 // Date toJSON | 47 // Date toJSON |
| 68 assertEquals("1970-01-01T00:00:00.000Z", new Date(0).toJSON()); | 48 assertEquals("1970-01-01T00:00:00.000Z", new Date(0).toJSON()); |
| 69 assertEquals("1979-01-11T08:00:00.000Z", new Date("1979-01-11 08:00 GMT").toJSON ()); | 49 assertEquals("1979-01-11T08:00:00.000Z", new Date("1979-01-11 08:00 GMT").toJSON ()); |
| 70 assertEquals("2005-05-05T05:05:05.000Z", new Date("2005-05-05 05:05:05 GMT").toJ SON()); | 50 assertEquals("2005-05-05T05:05:05.000Z", new Date("2005-05-05 05:05:05 GMT").toJ SON()); |
| 71 var n1 = new Date(10000); | 51 var n1 = new Date(10000); |
| 72 n1.toISOString = function () { return "foo"; }; | 52 n1.toISOString = function () { return "foo"; }; |
| 73 assertEquals("foo", n1.toJSON()); | 53 assertEquals("foo", n1.toJSON()); |
| 74 var n2 = new Date(10001); | 54 var n2 = new Date(10001); |
| 75 n2.toISOString = null; | 55 n2.toISOString = null; |
| 76 assertThrows(function () { n2.toJSON(); }, TypeError); | 56 assertThrows(function () { n2.toJSON(); }, TypeError); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 if (i < 16) { | 325 if (i < 16) { |
| 346 expected = "\\u000" + i.toString(16); | 326 expected = "\\u000" + i.toString(16); |
| 347 } else { | 327 } else { |
| 348 expected = "\\u00" + i.toString(16); | 328 expected = "\\u00" + i.toString(16); |
| 349 } | 329 } |
| 350 } else { | 330 } else { |
| 351 expected = string; | 331 expected = string; |
| 352 } | 332 } |
| 353 assertEquals('"' + expected + '"', encoded, "Codepoint " + i); | 333 assertEquals('"' + expected + '"', encoded, "Codepoint " + i); |
| 354 } | 334 } |
| 335 | |
| 336 | |
| 337 // Ensure that wrappers and callables are handled correctly. | |
| 338 var num37 = new Number(42); | |
| 339 num37.valueOf = function() { return 37; }; | |
| 340 | |
| 341 var numFoo = new Number(42); | |
| 342 numFoo.valueOf = "not callable"; | |
| 343 numFoo.toString = function() { return "foo"; }; | |
| 344 | |
| 345 var numTrue = new Number(42); | |
| 346 numTrue.valueOf = function() { return true; } | |
| 347 | |
| 348 var strFoo = new String("bar"); | |
| 349 strFoo.toString = function() { return "foo"; }; | |
| 350 | |
| 351 var str37 = new String("bar"); | |
| 352 str37.toString = "not callable"; | |
| 353 str37.valueOf = function() { return 37; }; | |
| 354 | |
| 355 var strTrue = new String("bar"); | |
| 356 strTrue.toString = function() { return true; } | |
| 357 | |
| 358 var func = function() { /* Is callable */ }; | |
| 359 | |
| 360 var funcJSON = function() { /* Is callable */ }; | |
| 361 funcJSON.toJSON = function() { return "has toJSON"; }; | |
| 362 | |
| 363 var re = /Is callable/; | |
| 364 | |
| 365 var reJSON = /Is callable/; | |
| 366 reJSON.toJSON = function() { return "has toJSON"; }; | |
| 367 | |
| 368 assertEquals('[37,null,1,"foo","37","true",null,"has toJSON",null,"has toJSON"]' , | |
|
Rico
2010/12/15 07:46:10
Long line + space after every comma
Lasse Reichstein
2010/12/15 08:59:54
It's a string literal, and the exact output of JSO
| |
| 369 JSON.stringify([num37, numFoo, numTrue, | |
| 370 strFoo, str37, strTrue, | |
| 371 func, funcJSON, re, reJSON])); | |
| 372 | |
| 373 | |
| 374 var oddball = Object(42); | |
| 375 oddball.__proto__ = { __proto__: null, toString: function() { return true; } }; | |
| 376 assertEquals('1', JSON.stringify(oddball)); | |
| 377 | |
| 378 var getCount = 0; | |
| 379 var callCount = 0; | |
| 380 var counter = { get toJSON() { getCount++; | |
| 381 return function() { callCount++; | |
| 382 return 42; }; } }; | |
| 383 assertEquals('42', JSON.stringify(counter)); | |
| 384 assertEquals(1, getCount); | |
| 385 assertEquals(1, callCount); | |
| 386 | |
| 387 var oddball2 = Object(42); | |
| 388 var oddball3 = Object("foo"); | |
| 389 oddball3.__proto__ = { __proto__: null, | |
| 390 toString: "not callable", | |
| 391 valueOf: function() { return true; } }; | |
| 392 oddball2.__proto__ = { __proto__: null, | |
| 393 toJSON: function () { return oddball3; } } | |
| 394 assertEquals('"true"', JSON.stringify(oddball2)); | |
| 395 | |
| 396 | |
| 397 var falseNum = Object("37"); | |
| 398 falseNum.__proto__ = Number.prototype; | |
| 399 falseNum.toString = function() { return 42; }; | |
| 400 assertEquals('"42"', JSON.stringify(falseNum)); | |
|
Rico
2010/12/15 07:46:10
We should also test the case where toJSON is set a
Lasse Reichstein
2010/12/15 08:59:54
Adding tests of Date.prototype.toJSON when called
| |
| OLD | NEW |