OLD | NEW |
---|---|
(Empty) | |
1 // 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 | |
31 var success = "[test success]"; | |
32 var expectedThis; | |
33 function testJoin() { | |
34 assertEquals(0, arguments.length); | |
35 assertSame(expectedThis, this); | |
36 return success; | |
37 } | |
38 | |
39 | |
40 // On an Array object. | |
41 | |
42 var a1 = [1, 2, 3]; | |
43 assertEquals(a1.join(), a1.toString()); | |
44 | |
45 | |
46 var a2 = [1, 2, 3]; | |
47 a2.join = testJoin; | |
48 expectedThis = a2; | |
49 assertEquals(success, a2.toString()); | |
50 | |
51 | |
52 var a3 = [1, 2, 3]; | |
53 a3.join = "not callable"; | |
54 assertEquals("[object Array]", a3.toString()); | |
55 | |
56 | |
57 var a4 = [1, 2, 3]; | |
58 a4.__proto__ = { toString: Array.prototype.toString }; | |
59 // No join on Array. | |
60 assertEquals("[object Array]", a4.toString()); | |
61 | |
62 | |
63 // On a non-Array object. | |
64 var o1 = {length: 3, 0: 1, 1: 2, 2: 3, | |
65 toString: Array.prototype.toString, | |
66 join: Array.prototype.join}; | |
67 assertEquals(o1.join(), o1.toString()); | |
68 | |
69 | |
70 // Check that we don't read, e.g., length before calling join. | |
71 var o2 = {toString : Array.prototype.toString, | |
72 join: testJoin, | |
73 get length() { assertUnreachable(); }, | |
74 get 0() { assertUnreachable(); }}; | |
75 expectedThis = o2; | |
76 assertEquals(success, o2.toString()); | |
77 | |
78 | |
Rico
2011/10/04 12:10:45
Add comment on what we are testing, e.g.,
// Check
| |
79 var o3 = {length: 3, 0: 1, 1: 2, 2: 3, | |
80 toString: Array.prototype.toString, | |
81 join: testJoin}; | |
82 expectedThis = o3; | |
83 assertEquals(success, o3.toString()); | |
84 | |
85 | |
Rico
2011/10/04 12:10:45
One more comment?:
// Check that when join is not
| |
86 var o4 = {length: 3, 0: 1, 1: 2, 2: 3, | |
87 toString: Array.prototype.toString, | |
88 join: "not callable"}; | |
89 assertEquals("[object Object]", o4.toString()); | |
90 | |
91 | |
92 var o5 = {length: 3, 0: 1, 1: 2, 2: 3, | |
93 toString: Array.prototype.toString | |
94 /* no join */}; | |
95 assertEquals("[object Object]", o5.toString()); | |
96 | |
97 | |
98 // Test that to-object is called before getting join, so the instance | |
99 // that "join" is read from is the same one passed as receiver. | |
100 | |
101 var called_before = false; | |
102 expectedThis = null; | |
103 Object.defineProperty(Number.prototype, "join", {get: function() { | |
104 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.
| |
105 called_before = true; | |
106 expectedThis = this; | |
107 return testJoin; | |
108 }}); | |
109 Number.prototype.arrayToString = Array.prototype.toString; | |
110 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.
| |
OLD | NEW |