OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-templates | |
6 | |
7 (function testStringRawArity() { | |
8 assertEquals(1, String.raw.length); | |
9 })(); | |
10 | |
11 | |
12 (function testStringRawCallSiteToObject() { | |
13 assertThrows("String.raw()", TypeError); | |
14 })(); | |
15 | |
16 | |
17 (function testStringRawCallSiteRawToObject() { | |
18 assertThrows("String.raw([])", TypeError); | |
19 })(); | |
20 | |
21 | |
22 (function testStringRawUndefinedLength() { | |
23 var callSiteObj = []; | |
24 callSiteObj.raw = {}; | |
25 assertEquals("", String.raw(callSiteObj)); | |
26 | |
27 callSiteObj.raw = { lengt: 0 }; | |
28 assertEquals("", String.raw(callSiteObj)); | |
29 })(); | |
30 | |
31 | |
32 (function testStringRawZeroLength() { | |
33 var callSiteObj = []; | |
34 callSiteObj.raw = { length: 0 }; | |
35 assertEquals("", String.raw(callSiteObj)); | |
36 assertEquals("", String.raw(callSiteObj, "a", "b", "c")); | |
37 | |
38 callSiteObj.raw = []; | |
39 assertEquals("", String.raw(callSiteObj)); | |
40 assertEquals("", String.raw(callSiteObj, "a", "b", "c")); | |
41 })(); | |
42 | |
43 | |
44 (function testStringRawNegativeLength() { | |
45 var callSiteObj = []; | |
46 callSiteObj.raw = { length: -85 }; | |
47 assertEquals("", String.raw(callSiteObj)); | |
48 assertEquals("", String.raw(callSiteObj, "a", "b", "c")); | |
49 | |
50 callSiteObj.raw = []; | |
51 assertEquals("", String.raw(callSiteObj)); | |
52 assertEquals("", String.raw(callSiteObj, "a", "b", "c")); | |
53 })(); | |
54 | |
55 | |
56 (function testStringRawNaNLength() { | |
57 var callSiteObj = []; | |
58 callSiteObj.raw = { length: NaN }; | |
59 assertEquals("", String.raw(callSiteObj)); | |
60 assertEquals("", String.raw(callSiteObj, "a", "b", "c")); | |
61 | |
62 callSiteObj.raw = []; | |
63 assertEquals("", String.raw(callSiteObj)); | |
64 assertEquals("", String.raw(callSiteObj, "a", "b", "c")); | |
65 })(); | |
66 | |
67 | |
68 (function testStringRawBasic() { | |
69 var callSiteObj = []; | |
70 callSiteObj.raw = ["a"]; | |
71 assertEquals("a", String.raw(callSiteObj)); | |
72 })(); | |
73 | |
74 | |
75 (function testStringRawNoSubst() { | |
76 var callSiteObj = []; | |
77 callSiteObj.raw = ["a", "b"]; | |
78 assertEquals("ab", String.raw(callSiteObj)); | |
79 })(); | |
80 | |
81 | |
82 (function testStringRawSubst() { | |
83 var callSiteObj = []; | |
84 callSiteObj.raw = ["a", "b"]; | |
85 assertEquals("a!b", String.raw(callSiteObj, "!")); | |
86 | |
87 callSiteObj.raw = ["a", "b", "c"]; | |
88 assertEquals("abc", String.raw(callSiteObj)); | |
89 | |
90 callSiteObj.raw = ["a", "b", "c"]; | |
91 assertEquals("a!bc", String.raw(callSiteObj, "!")); | |
92 | |
93 callSiteObj.raw = ["a", "b", "c"]; | |
94 assertEquals("a!b?c", String.raw(callSiteObj, "!", "?")); | |
95 | |
96 callSiteObj.raw = ["\n", "\r\n", "\r"]; | |
97 assertEquals("\nx\r\ny\r", String.raw(callSiteObj, "x", "y")); | |
98 | |
99 callSiteObj.raw = ["\n", "\r\n", "\r"]; | |
100 assertEquals("\n\r\r\r\n\n\r", String.raw(callSiteObj, "\r\r", "\n")); | |
101 })(); | |
102 | |
103 | |
104 (function testStringRawArrayLikeSubst() { | |
105 var callSiteObj = []; | |
106 callSiteObj.raw = {"length": 2, "0": "a", "1": "b", "2": "c"}; | |
107 assertEquals("axb", String.raw(callSiteObj, "x", "y")); | |
108 | |
109 callSiteObj.raw = {"length": 4, "0": "a", "1": "b", "2": "c"}; | |
110 assertEquals("axbycundefined", String.raw(callSiteObj, "x", "y")); | |
111 })(); | |
112 | |
113 | |
114 (function testStringRawAccessors() { | |
115 var callSiteObj = {}; | |
116 callSiteObj.raw = {}; | |
117 Object.defineProperties(callSiteObj, { | |
118 "length": { | |
119 get: function() { assertUnreachable(); }, | |
120 set: function(v) { assertUnreachable(); } | |
121 }, | |
122 "0": { | |
123 get: function() { assertUnreachable(); }, | |
124 set: function(v) { assertUnreachable(); } | |
125 }, | |
126 "1": { | |
127 get: function() { assertUnreachable(); }, | |
128 set: function(v) { assertUnreachable(); } | |
129 } | |
130 }); | |
131 Object.defineProperties(callSiteObj.raw, { | |
132 "length": { | |
133 get: function() { return 2; }, | |
134 set: function(v) { assertUnreachable(); } | |
135 }, | |
136 "0": { | |
137 get: function() { return "getter values"; }, | |
138 set: function(v) { assertUnreachable(); } | |
139 }, | |
140 "1": { | |
141 get: function() { return "are nice"; }, | |
142 set: function(v) { assertUnreachable(); } | |
143 } | |
144 }); | |
145 assertEquals("getter values are nice", String.raw(callSiteObj, " ")); | |
146 })(); | |
147 | |
148 | |
149 (function testStringRawHoleyArray() { | |
150 var callSiteObj = []; | |
151 callSiteObj.raw = ["1."]; | |
152 callSiteObj.raw[2] = ".2"; | |
153 assertEquals("1.undefined.2", String.raw(callSiteObj)); | |
154 })(); | |
155 | |
156 | |
157 (function testStringRawAccessorThrows() { | |
158 var callSiteObj = []; | |
159 callSiteObj.raw = [1]; | |
160 function MyError() {} | |
161 Object.defineProperty(callSiteObj.raw, "0", { | |
162 get: function() { throw new MyError(); } | |
163 }); | |
164 assertThrows(function() { String.raw(callSiteObj); }, MyError); | |
165 })(); | |
166 | |
167 | |
168 (function testStringRawToStringSafe() { | |
169 var callSiteObj = []; | |
170 callSiteObj.raw = [null, undefined, 1, "str", true, false, NaN, Infinity, {}]; | |
171 assertEquals("nullundefined1strtruefalseNaNInfinity[object Object]", | |
172 String.raw(callSiteObj)); | |
173 | |
174 callSiteObj.raw = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; | |
175 assertEquals("0null1undefined213str4true5false6NaN7Infinity8[object Object]9", | |
176 String.raw(callSiteObj, null, void 0, 1, "str", true, false, | |
177 NaN, Infinity, {})); | |
178 })(); | |
179 | |
180 | |
181 (function testStringRawToStringSymbolThrows() { | |
182 var callSiteObj = []; | |
183 callSiteObj.raw = [Symbol("foo")]; | |
184 assertThrows(function() { | |
185 String.raw(callSiteObj); | |
186 }, TypeError); | |
187 | |
188 callSiteObj.raw = ["1", "2"]; | |
189 assertThrows(function() { | |
190 String.raw(callSiteObj, Symbol("foo")); | |
191 }, TypeError); | |
192 })(); | |
193 | |
194 | |
195 (function testStringRawToStringThrows() { | |
196 var callSiteObj = []; | |
197 var thrower = {}; | |
198 function MyError() {} | |
199 thrower.toString = function() { | |
200 throw new MyError(); | |
201 } | |
202 | |
203 callSiteObj.raw = [thrower]; | |
204 assertThrows(function() { | |
205 String.raw(callSiteObj); | |
206 }, MyError); | |
207 | |
208 callSiteObj.raw = ["1", "2"]; | |
209 assertThrows(function() { | |
210 String.raw(callSiteObj, thrower); | |
211 }, MyError); | |
212 })(); | |
213 | |
214 | |
215 (function testStringRawToStringValueOfThrows() { | |
216 var callSiteObj = []; | |
217 var thrower = {}; | |
218 function MyError() {} | |
219 thrower.toString = null; | |
220 thrower.valueOf = function() { | |
221 throw new MyError(); | |
222 } | |
223 | |
224 callSiteObj.raw = [thrower]; | |
225 assertThrows(function() { | |
226 String.raw(callSiteObj); | |
227 }, MyError); | |
228 | |
229 callSiteObj.raw = ["1", "2"]; | |
230 assertThrows(function() { | |
231 String.raw(callSiteObj, thrower); | |
232 }, MyError); | |
233 })(); | |
234 | |
235 | |
236 (function testStringRawOrder() { | |
237 var order = []; | |
238 var callSiteObj = []; | |
239 callSiteObj.raw = {}; | |
240 function arg(v) { | |
241 var result = {}; | |
242 result.toString = null; | |
243 result.valueOf = function() { order.push("arg" + v); return v; } | |
244 return result; | |
245 } | |
246 | |
247 Object.defineProperty(callSiteObj.raw, "length", { | |
248 get: function() { order.push("length"); return 3; } | |
249 }); | |
250 [1, 3, 5].forEach(function(v, i) { | |
251 Object.defineProperty(callSiteObj.raw, i, { | |
252 get: function() { order.push("raw" + v); return v; } | |
253 }); | |
254 }); | |
255 | |
256 assertEquals("12345", String.raw(callSiteObj, arg(2), arg(4), arg(6))); | |
257 assertEquals(["length", "raw1", "arg2", "raw3", "arg4", "raw5"], order); | |
258 })(); | |
OLD | NEW |