blob: 5be3d5045c87e8eecc8398b275b5669e5dea38d6 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Array's toString should call the object's own join method, if one exists and
29// is callable. Otherwise, just use the original Object.toString function.
30
31var success = "[test success]";
32var expectedThis;
33function testJoin() {
34 assertEquals(0, arguments.length);
35 assertSame(expectedThis, this);
36 return success;
37}
38
39
40// On an Array object.
41
42// Default case.
43var a1 = [1, 2, 3];
44assertEquals(a1.join(), a1.toString());
45
46// Non-standard "join" function is called correctly.
47var a2 = [1, 2, 3];
48a2.join = testJoin;
49expectedThis = a2;
50assertEquals(success, a2.toString());
51
52// Non-callable join function is ignored and Object.prototype.toString is
53// used instead.
54var a3 = [1, 2, 3];
55a3.join = "not callable";
56assertEquals("[object Array]", a3.toString());
57
58// Non-existing join function is treated same as non-callable.
59var a4 = [1, 2, 3];
60a4.__proto__ = { toString: Array.prototype.toString };
61// No join on Array.
62assertEquals("[object Array]", a4.toString());
63
64
65// On a non-Array object.
66
67// Default looks-like-an-array case.
68var o1 = {length: 3, 0: 1, 1: 2, 2: 3,
69 toString: Array.prototype.toString,
70 join: Array.prototype.join};
71assertEquals(o1.join(), o1.toString());
72
73
74// Non-standard join is called correctly.
75// Check that we don't read, e.g., length before calling join.
76var o2 = {toString : Array.prototype.toString,
77 join: testJoin,
78 get length() { assertUnreachable(); },
79 get 0() { assertUnreachable(); }};
80expectedThis = o2;
81assertEquals(success, o2.toString());
82
83// Non-standard join is called even if it looks like an array.
84var o3 = {length: 3, 0: 1, 1: 2, 2: 3,
85 toString: Array.prototype.toString,
86 join: testJoin};
87expectedThis = o3;
88assertEquals(success, o3.toString());
89
90// Non-callable join works same as for Array.
91var o4 = {length: 3, 0: 1, 1: 2, 2: 3,
92 toString: Array.prototype.toString,
93 join: "not callable"};
94assertEquals("[object Object]", o4.toString());
95
96
97// Non-existing join works same as for Array.
98var o5 = {length: 3, 0: 1, 1: 2, 2: 3,
99 toString: Array.prototype.toString
100 /* no join */};
101assertEquals("[object Object]", o5.toString());
102
103
104// Test that ToObject is called before getting "join", so the instance
105// that "join" is read from is the same one passed as receiver later.
106var called_before = false;
107expectedThis = null;
108Object.defineProperty(Number.prototype, "join", {get: function() {
109 assertFalse(called_before);
110 called_before = true;
111 expectedThis = this;
112 return testJoin;
113 }});
114Number.prototype.arrayToString = Array.prototype.toString;
115assertEquals(success, (42).arrayToString());
116
117// ----------------------------------------------------------
118// Testing Array.prototype.toLocaleString
119
120// Ensure that it never uses Array.prototype.toString for anything.
121Array.prototype.toString = function() { assertUnreachable(); };
122
123// Default case.
124var la1 = [1, [2, 3], 4];
125assertEquals("1,2,3,4", la1.toLocaleString());
126
127// Used on a string (which looks like an array of characters).
128String.prototype.toLocaleString = Array.prototype.toLocaleString;
129assertEquals("1,2,3,4", "1234".toLocaleString());
130
131// If toLocaleString of element is not callable, throw a TypeError.
132var la2 = [1, {toLocaleString: "not callable"}, 3];
133assertThrows(function() { la2.toLocaleString(); }, TypeError);
134
135// If toLocaleString of element is callable, call it.
136var la3 = [1, {toLocaleString: function() { return "XX";}}, 3];
137assertEquals("1,XX,3", la3.toLocaleString());
138
139// Omitted elements, as well as undefined and null, become empty string.
140var la4 = [1, null, 3, undefined, 5,, 7];
141assertEquals("1,,3,,5,,7", la4.toLocaleString());
142
143
144// ToObject is called first and the same object is being used for the
145// rest of the operations.
146Object.defineProperty(Number.prototype, "length", {
147 get: function() {
148 exptectedThis = this;
149 return 3;
150 }});
151for (var i = 0; i < 3; i++) {
152 Object.defineProperty(Number.prototype, i, {
153 get: function() {
154 assertEquals(expectedThis, this);
155 return +this;
156 }});
157}
158Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159assertEquals("42,42,42", (42).arrayToLocaleString());