OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // Testing Array.prototype.toLocaleString | 118 // Testing Array.prototype.toLocaleString |
119 | 119 |
120 // Ensure that it never uses Array.prototype.toString for anything. | 120 // Ensure that it never uses Array.prototype.toString for anything. |
121 Array.prototype.toString = function() { assertUnreachable(); }; | 121 Array.prototype.toString = function() { assertUnreachable(); }; |
122 | 122 |
123 // Default case. | 123 // Default case. |
124 var la1 = [1, [2, 3], 4]; | 124 var la1 = [1, [2, 3], 4]; |
125 assertEquals("1,2,3,4", la1.toLocaleString()); | 125 assertEquals("1,2,3,4", la1.toLocaleString()); |
126 | 126 |
127 // Used on a string (which looks like an array of characters). | 127 // Used on a string (which looks like an array of characters). |
128 String.prototype.toLocaleString = Array.prototype.toLocaleString; | 128 String.prototype.toLocaleString = function() { |
| 129 return (this.length == 1) ? this : Array.prototype.toLocaleString.call(this); |
| 130 } |
129 assertEquals("1,2,3,4", "1234".toLocaleString()); | 131 assertEquals("1,2,3,4", "1234".toLocaleString()); |
130 | 132 |
131 // If toLocaleString of element is not callable, throw a TypeError. | 133 // If toLocaleString of element is not callable, throw a TypeError. |
132 var la2 = [1, {toLocaleString: "not callable"}, 3]; | 134 var la2 = [1, {toLocaleString: "not callable"}, 3]; |
133 assertThrows(function() { la2.toLocaleString(); }, TypeError); | 135 assertThrows(function() { la2.toLocaleString(); }, TypeError); |
134 | 136 |
135 // If toLocaleString of element is callable, call it. | 137 // If toLocaleString of element is callable, call it. |
136 var la3 = [1, {toLocaleString: function() { return "XX";}}, 3]; | 138 var la3 = [1, {toLocaleString: function() { return "XX";}}, 3]; |
137 assertEquals("1,XX,3", la3.toLocaleString()); | 139 assertEquals("1,XX,3", la3.toLocaleString()); |
138 | 140 |
(...skipping 11 matching lines...) Expand all Loading... |
150 }}); | 152 }}); |
151 for (var i = 0; i < 3; i++) { | 153 for (var i = 0; i < 3; i++) { |
152 Object.defineProperty(Number.prototype, i, { | 154 Object.defineProperty(Number.prototype, i, { |
153 get: function() { | 155 get: function() { |
154 assertEquals(expectedThis, this); | 156 assertEquals(expectedThis, this); |
155 return +this; | 157 return +this; |
156 }}); | 158 }}); |
157 } | 159 } |
158 Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString; | 160 Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString; |
159 assertEquals("42,42,42", (42).arrayToLocaleString()); | 161 assertEquals("42,42,42", (42).arrayToLocaleString()); |
| 162 |
| 163 |
| 164 (function TestToLocaleStringCalls() { |
| 165 let log = []; |
| 166 let pushArgs = (label) => (...args) => log.push(label, args); |
| 167 |
| 168 let NumberToLocaleString = Number.prototype.toLocaleString; |
| 169 let StringToLocaleString = String.prototype.toLocaleString; |
| 170 let ObjectToLocaleString = Object.prototype.toLocaleString; |
| 171 Number.prototype.toLocaleString = pushArgs("Number"); |
| 172 String.prototype.toLocaleString = pushArgs("String"); |
| 173 Object.prototype.toLocaleString = pushArgs("Object"); |
| 174 |
| 175 [42, "foo", {}].toLocaleString(); |
| 176 assertEquals(["Number", [], "String", [], "Object", []], log); |
| 177 |
| 178 Number.prototype.toLocaleString = NumberToLocaleString; |
| 179 String.prototype.toLocaleString = StringToLocaleString; |
| 180 Object.prototype.toLocaleString = ObjectToLocaleString; |
| 181 })(); |
OLD | NEW |