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 = function() { | 128 String.prototype.toLocaleString = Array.prototype.toLocaleString; |
129 return (this.length == 1) ? this : Array.prototype.toLocaleString.call(this); | |
130 } | |
131 assertEquals("1,2,3,4", "1234".toLocaleString()); | 129 assertEquals("1,2,3,4", "1234".toLocaleString()); |
132 | 130 |
133 // If toLocaleString of element is not callable, throw a TypeError. | 131 // If toLocaleString of element is not callable, throw a TypeError. |
134 var la2 = [1, {toLocaleString: "not callable"}, 3]; | 132 var la2 = [1, {toLocaleString: "not callable"}, 3]; |
135 assertThrows(function() { la2.toLocaleString(); }, TypeError); | 133 assertThrows(function() { la2.toLocaleString(); }, TypeError); |
136 | 134 |
137 // If toLocaleString of element is callable, call it. | 135 // If toLocaleString of element is callable, call it. |
138 var la3 = [1, {toLocaleString: function() { return "XX";}}, 3]; | 136 var la3 = [1, {toLocaleString: function() { return "XX";}}, 3]; |
139 assertEquals("1,XX,3", la3.toLocaleString()); | 137 assertEquals("1,XX,3", la3.toLocaleString()); |
140 | 138 |
(...skipping 11 matching lines...) Expand all Loading... |
152 }}); | 150 }}); |
153 for (var i = 0; i < 3; i++) { | 151 for (var i = 0; i < 3; i++) { |
154 Object.defineProperty(Number.prototype, i, { | 152 Object.defineProperty(Number.prototype, i, { |
155 get: function() { | 153 get: function() { |
156 assertEquals(expectedThis, this); | 154 assertEquals(expectedThis, this); |
157 return +this; | 155 return +this; |
158 }}); | 156 }}); |
159 } | 157 } |
160 Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString; | 158 Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString; |
161 assertEquals("42,42,42", (42).arrayToLocaleString()); | 159 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 |