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

Unified Diff: test/mjsunit/array-tostring.js

Issue 8124025: Fix issue 1361 - Implement ES5 Array.prototype.toString. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/array.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/array-tostring.js
diff --git a/test/mjsunit/array-tostring.js b/test/mjsunit/array-tostring.js
new file mode 100644
index 0000000000000000000000000000000000000000..704248356cbc8e3775b3953d73dbf0db42b2d5b8
--- /dev/null
+++ b/test/mjsunit/array-tostring.js
@@ -0,0 +1,110 @@
+// Copyright 2011 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Array's toString should call the object's own join method, if one exists and
+// is callable. Otherwise, just use the original Object.toString function.
+
+var success = "[test success]";
+var expectedThis;
+function testJoin() {
+ assertEquals(0, arguments.length);
+ assertSame(expectedThis, this);
+ return success;
+}
+
+
+// On an Array object.
+
+var a1 = [1, 2, 3];
+assertEquals(a1.join(), a1.toString());
+
+
+var a2 = [1, 2, 3];
+a2.join = testJoin;
+expectedThis = a2;
+assertEquals(success, a2.toString());
+
+
+var a3 = [1, 2, 3];
+a3.join = "not callable";
+assertEquals("[object Array]", a3.toString());
+
+
+var a4 = [1, 2, 3];
+a4.__proto__ = { toString: Array.prototype.toString };
+// No join on Array.
+assertEquals("[object Array]", a4.toString());
+
+
+// On a non-Array object.
+var o1 = {length: 3, 0: 1, 1: 2, 2: 3,
+ toString: Array.prototype.toString,
+ join: Array.prototype.join};
+assertEquals(o1.join(), o1.toString());
+
+
+// Check that we don't read, e.g., length before calling join.
+var o2 = {toString : Array.prototype.toString,
+ join: testJoin,
+ get length() { assertUnreachable(); },
+ get 0() { assertUnreachable(); }};
+expectedThis = o2;
+assertEquals(success, o2.toString());
+
+
Rico 2011/10/04 12:10:45 Add comment on what we are testing, e.g., // Check
+var o3 = {length: 3, 0: 1, 1: 2, 2: 3,
+ toString: Array.prototype.toString,
+ join: testJoin};
+expectedThis = o3;
+assertEquals(success, o3.toString());
+
+
Rico 2011/10/04 12:10:45 One more comment?: // Check that when join is not
+var o4 = {length: 3, 0: 1, 1: 2, 2: 3,
+ toString: Array.prototype.toString,
+ join: "not callable"};
+assertEquals("[object Object]", o4.toString());
+
+
+var o5 = {length: 3, 0: 1, 1: 2, 2: 3,
+ toString: Array.prototype.toString
+ /* no join */};
+assertEquals("[object Object]", o5.toString());
+
+
+// Test that to-object is called before getting join, so the instance
+// that "join" is read from is the same one passed as receiver.
+
+var called_before = false;
+expectedThis = null;
+Object.defineProperty(Number.prototype, "join", {get: function() {
+ assertFalse(called_before);
Rico 2011/10/04 12:10:45 indention here seems random
Lasse Reichstein 2011/10/04 12:56:33 Pretty much, yes. I blame Emacs.
+ called_before = true;
+ expectedThis = this;
+ return testJoin;
+ }});
+Number.prototype.arrayToString = Array.prototype.toString;
+assertEquals(success, 42..arrayToString());
Rico 2011/10/04 12:10:45 Please add parenthesis around 42 to increase reada
Lasse Reichstein 2011/10/04 12:56:33 Done.
« no previous file with comments | « src/array.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698