OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 load("test/mjsunit/harmony/typesystem/testgen.js"); | 6 load("test/mjsunit/harmony/typesystem/testgen.js"); |
7 | 7 |
8 | 8 |
9 // In the rest, for each NonTerminal symbol in the parser grammar that we | 9 // In the rest, for each NonTerminal symbol in the parser grammar that we |
10 // care to test, there are two generator functions (ValidNonTerminal and | 10 // care to test, there are two generator functions (ValidNonTerminal and |
(...skipping 13 matching lines...) Expand all Loading... |
24 new TestGen(1, ValidTypes, [ | 24 new TestGen(1, ValidTypes, [ |
25 t => "(" + t + ")" | 25 t => "(" + t + ")" |
26 ]), | 26 ]), |
27 new TestGen(1, ValidPrimaryTypes, [ | 27 new TestGen(1, ValidPrimaryTypes, [ |
28 t => t + "[]", | 28 t => t + "[]", |
29 t => t + "[][]", | 29 t => t + "[][]", |
30 t => "(" + t + "[])", | 30 t => "(" + t + "[])", |
31 t => "(" + t + "[])[]", | 31 t => "(" + t + "[])[]", |
32 ]), | 32 ]), |
33 new TestGen(1, ValidTupleTypes, [t => t]), | 33 new TestGen(1, ValidTupleTypes, [t => t]), |
| 34 new TestGen(1, ValidObjectTypes, [t => t]), |
34 proper && "number", | 35 proper && "number", |
35 proper && "boolean", | 36 proper && "boolean", |
36 proper && "string", | 37 proper && "string", |
37 proper && "symbol", | 38 proper && "symbol", |
38 // Type references | 39 // Type references |
39 "A", | 40 "A", |
40 "Tree<number>", | 41 "Tree<number>", |
41 "Map<string, number>", | 42 "Map<string, number>", |
42 // Type queries | 43 // Type queries |
43 "typeof x", | 44 "typeof x", |
(...skipping 10 matching lines...) Expand all Loading... |
54 new TestGen(1, InvalidTypes, [ | 55 new TestGen(1, InvalidTypes, [ |
55 t => "(" + t + ")" | 56 t => "(" + t + ")" |
56 ]), | 57 ]), |
57 new TestGen(1, InvalidPrimaryTypes, [ | 58 new TestGen(1, InvalidPrimaryTypes, [ |
58 t => t + "[]", | 59 t => t + "[]", |
59 t => t + "[][]", | 60 t => t + "[][]", |
60 t => "(" + t + "[])", | 61 t => "(" + t + "[])", |
61 t => "(" + t + "[])[]", | 62 t => "(" + t + "[])[]", |
62 ]), | 63 ]), |
63 new TestGen(1, InvalidTupleTypes, [t => t]), | 64 new TestGen(1, InvalidTupleTypes, [t => t]), |
| 65 new TestGen(1, InvalidObjectTypes, [t => t]), |
64 // Line terminator in arrays. | 66 // Line terminator in arrays. |
65 new TestGen(1, ValidTypes, [ | 67 new TestGen(1, ValidTypes, [ |
66 t => "(" + t + "\n[])" | 68 t => "(" + t + "\n[])" |
67 ]), | 69 ]), |
68 // Type references | 70 // Type references |
69 "Map<string, (number, void)>", | 71 "Map<string, (number, void)>", |
70 "number<string>", | 72 "number<string>", |
71 "Foo<>", | 73 "Foo<>", |
72 // Type queries | 74 // Type queries |
73 "typeof", | 75 "typeof", |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 new TestGen(1, ValidTypes, [ | 149 new TestGen(1, ValidTypes, [ |
148 t => c + "() => " + t, | 150 t => c + "() => " + t, |
149 t => c + "(a: " + t + ") => " + t, | 151 t => c + "(a: " + t + ") => " + t, |
150 t => c + "(a:" + t + ", b?:" + t + ") => " + t, | 152 t => c + "(a:" + t + ", b?:" + t + ") => " + t, |
151 t => c + "(a:" + t + ", b?:" + t + ", c) => " + t, | 153 t => c + "(a:" + t + ", b?:" + t + ", c) => " + t, |
152 t => c + "(a:" + t + ", b?:" + t + ", c?) => " + t, | 154 t => c + "(a:" + t + ", b?:" + t + ", c?) => " + t, |
153 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, | 155 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
154 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, | 156 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
155 t => c + "(a:" + t + ", b:" + t + ", c, ...d: string[]) => " + t | 157 t => c + "(a:" + t + ", b:" + t + ", c, ...d: string[]) => " + t |
156 ]), | 158 ]), |
157 // Parametric function types | 159 // Parametric function types. |
158 "<A> (x: A) => A", | 160 "<A> (x: A) => A", |
159 "<A extends string> (x: A) => A", | 161 "<A extends string> (x: A) => A", |
160 "<A, B> (x: A, y: B) => A", | 162 "<A, B> (x: A, y: B) => A", |
161 "<A, B extends number[], C> (x: A, y: B) => C", | 163 "<A, B extends number[], C> (x: A, y: B) => C", |
162 "<A, B, C, D> (x: A, y: B, z: C[], d: (e: D) => D) => A", | 164 "<A, B, C, D> (x: A, y: B, z: C[], d: (e: D) => D) => A", |
163 // String literal types | 165 // String literal types. |
164 "(cmd: 'add', x: number, y: number) => number", | 166 "(cmd: 'add', x: number, y: number) => number", |
165 '(cmd: "sum", a: number[]) => number', | 167 '(cmd: "sum", a: number[]) => number', |
166 "(x: number, cmd: 'one', ...rest) => any", | 168 "(x: number, cmd: 'one', ...rest) => any", |
167 "(x: string, y: number, cmd: 'two', ...rest) => any", | 169 "(x: string, y: number, cmd: 'two', ...rest) => any", |
168 "(x: number, cmd?: 'two', ...rest) => string" | 170 "(x: number, cmd?: 'two', ...rest) => string", |
| 171 // With binding patterns. |
| 172 "([]: number[]) => boolean", |
| 173 "([x, y]: number[]) => boolean", |
| 174 "([x, ...rest]: number[]) => boolean", |
| 175 "([,x, y]: number[]) => boolean", |
| 176 "([x, y,]: number[]) => boolean", |
| 177 "([x, y,,]: number[]) => boolean", |
| 178 "([x, [a, b, c], z]: [number, any[], string]) => boolean", |
| 179 "({x: a, y: b}: {x: number, y: string}) => boolean", |
| 180 "({x: a, y: [b, c, ...rest]}: {x: number, y: string[]}) => boolean", |
| 181 "({x: {a, b}, y: c}: {x: {a, b}, y: string}) => boolean" |
169 ]); | 182 ]); |
170 } | 183 } |
171 | 184 |
172 function InvalidFunctionTypes(size, constr) { | 185 function InvalidFunctionTypes(size, constr) { |
173 let c = constr ? "new " : ""; | 186 let c = constr ? "new " : ""; |
174 return Generate(size, [ | 187 return Generate(size, [ |
175 // Illegal types in legal places. | 188 // Illegal types in legal places. |
176 new TestGen(1, InvalidTypes, [ | 189 new TestGen(1, InvalidTypes, [ |
177 t => c + "() => " + t, | 190 t => c + "() => " + t, |
178 t => c + "(a: " + t + ") => " + t, | 191 t => c + "(a: " + t + ") => " + t, |
179 t => c + "(a:" + t + ", b?:" + t + ") => " + t, | 192 t => c + "(a:" + t + ", b?:" + t + ") => " + t, |
180 t => c + "(a:" + t + ", b?:" + t + ", c) => " + t, | 193 t => c + "(a:" + t + ", b?:" + t + ", c) => " + t, |
181 t => c + "(a:" + t + ", b?:" + t + ", c?) => " + t, | 194 t => c + "(a:" + t + ", b?:" + t + ", c?) => " + t, |
182 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, | 195 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
183 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, | 196 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
184 t => c + "(a:" + t + ", b:" + t + ", c, ...d: string[]) => " + t, | 197 t => c + "(a:" + t + ", b:" + t + ", c, ...d: string[]) => " + t, |
185 ]), | 198 ]), |
186 // Parametric function types | 199 // Parametric function types. |
187 "<A> A[]", | 200 "<A> A[]", |
188 "<> (x: number) => number", | 201 "<> (x: number) => number", |
189 "<A extends ()> (x: A) => A" | 202 "<A extends ()> (x: A) => A", |
| 203 // With illegal binding patterns. |
| 204 "({x: {a; b}, y: c}: {x: {a; b}, y: string}) => boolean" |
190 ]); | 205 ]); |
191 } | 206 } |
192 | 207 |
193 | 208 |
194 // Tuple types. | 209 // Tuple types. |
195 | 210 |
196 function ValidTupleTypes(size) { | 211 function ValidTupleTypes(size) { |
197 return Generate(size, [ | 212 return Generate(size, [ |
198 new TestGen(1, ValidTypes, [ | 213 new TestGen(1, ValidTypes, [ |
199 t => "[" + t + "]", | 214 t => "[" + t + "]", |
(...skipping 17 matching lines...) Expand all Loading... |
217 new TestGen(1, ValidTypes, [t => "[" + t + ",,]"]), | 232 new TestGen(1, ValidTypes, [t => "[" + t + ",,]"]), |
218 new TestGen(1, ValidTypes, [t => "[," + t + "]"]), | 233 new TestGen(1, ValidTypes, [t => "[," + t + "]"]), |
219 new TestGen(1, ValidTypes, [t => "[,," + t + "]"]), | 234 new TestGen(1, ValidTypes, [t => "[,," + t + "]"]), |
220 new TestGen(1, ValidTypes, [t => "[" + t + ",," + t + "]"]), | 235 new TestGen(1, ValidTypes, [t => "[" + t + ",," + t + "]"]), |
221 new TestGen(1, ValidTypes, [t => "[..." + t + "]"]), | 236 new TestGen(1, ValidTypes, [t => "[..." + t + "]"]), |
222 new TestGen(1, ValidTypes, [t => "[" + t + ", ..." + t + "]"]), | 237 new TestGen(1, ValidTypes, [t => "[" + t + ", ..." + t + "]"]), |
223 ]); | 238 ]); |
224 } | 239 } |
225 | 240 |
226 | 241 |
| 242 // Object types. |
| 243 |
| 244 function ValidObjectTypes(size) { |
| 245 return Generate(size, [ |
| 246 // Empty object type. |
| 247 "{}", |
| 248 // Property signatures. |
| 249 "{a}", |
| 250 "{a, b}", |
| 251 "{a, b?}", |
| 252 "{a?, b}", |
| 253 new TestGen(1, ValidTypes, [ |
| 254 t => "{a: " + t + "}", |
| 255 t => "{a: " + t + ", b: " + t + "}", |
| 256 t => "{a?: " + t + "}", |
| 257 t => "{a: " + t + ", b?: " + t + "}", |
| 258 ]), |
| 259 // Method signatures. |
| 260 "{f()}", |
| 261 "{f?()}", |
| 262 "{f(a)}", |
| 263 "{f?(a)}", |
| 264 "{f(a, b)}", |
| 265 "{f?(a, b)}", |
| 266 new TestGen(1, ValidTypes, [ |
| 267 t => "{f() : " + t + "}", |
| 268 t => "{f?() : " + t + "}", |
| 269 t => "{f(a: " + t + ")}", |
| 270 t => "{f?(a: " + t + ")}", |
| 271 t => "{f(a: " + t + ") : " + t + "}", |
| 272 t => "{f?(a: " + t + ") : " + t + "}", |
| 273 t => "{f<A>(a: " + t + ")}", |
| 274 t => "{f?<A>(a: " + t + ")}", |
| 275 t => "{f<A, B>(a: A, b: " + t + ") : B}", |
| 276 t => "{f?<A, B>(a: A, b: " + t + ") : B}", |
| 277 t => "{f<A extends " + t + ">(a: " + t + ")}", |
| 278 t => "{f?<A extends " + t + ">(a: " + t + ")}", |
| 279 t => "{f<A extends " + t + ", B>(a: A, b: " + t + ") : B}", |
| 280 t => "{f?<A extends " + t + ", B>(a: A, b: " + t + ") : B}", |
| 281 t => "{f<A extends " + t + ">(a: " + t + ") : " + t + "}", |
| 282 t => "{f?<A extends " + t + ">(a: " + t + ") : " + t + "}" |
| 283 ]), |
| 284 // Call signatures. |
| 285 "{()}", |
| 286 "{(a)}", |
| 287 "{(a, b)}", |
| 288 new TestGen(1, ValidTypes, [ |
| 289 t => "{() : " + t + "}", |
| 290 t => "{(a: " + t + ")}", |
| 291 t => "{(a: " + t + ") : " + t + "}", |
| 292 t => "{<A extends " + t + ">(a: " + t + ")}", |
| 293 t => "{<A extends " + t + ">(a: " + t + ") : " + t + "}" |
| 294 ]), |
| 295 // Constructor signatures. |
| 296 "{new ()}", |
| 297 "{new (a)}", |
| 298 "{new (a, b)}", |
| 299 new TestGen(1, ValidTypes, [ |
| 300 t => "{new () : " + t + "}", |
| 301 t => "{new (a: " + t + ")}", |
| 302 t => "{new (a: " + t + ") : " + t + "}", |
| 303 t => "{new <A extends " + t + ">(a: " + t + ")}", |
| 304 t => "{new <A extends " + t + ">(a: " + t + ") : " + t + "}" |
| 305 ]), |
| 306 // Index signatures. |
| 307 "{[a: number]}", |
| 308 "{[a: string]}", |
| 309 new TestGen(1, ValidTypes, [ |
| 310 t => "{[a: number] : " + t + "}", |
| 311 t => "{[a: string] : " + t + "}" |
| 312 ]) |
| 313 ]); |
| 314 } |
| 315 |
| 316 function InvalidObjectTypes(size) { |
| 317 return Generate(size, [ |
| 318 // Illegal types in legal places. |
| 319 new TestGen(1, InvalidTypes, [ |
| 320 t => "{a: " + t + "}", |
| 321 t => "{a: " + t + ", b?: " + t + "}", |
| 322 t => "{f() : " + t + "}", |
| 323 t => "{f(a: " + t + ")}", |
| 324 t => "{f<A extends " + t + ">()}", |
| 325 t => "{(a: " + t + ")}", |
| 326 t => "{new () : " + t + "}" |
| 327 ]), |
| 328 // Valid binding patterns that are not tuple types. |
| 329 "{a: []}", |
| 330 "{a: [...rest]}", |
| 331 // Invalid index signatures. |
| 332 "{[a: any]}", |
| 333 "{[a: any] : number}" |
| 334 ]); |
| 335 } |
| 336 |
| 337 |
227 // All types: simple, type references, type parametric, type queries | 338 // All types: simple, type references, type parametric, type queries |
228 // and tuples. | 339 // and tuples. |
229 | 340 |
230 function ValidTypes(size) { | 341 function ValidTypes(size) { |
231 return Generate(size, [ | 342 return Generate(size, [ |
232 new TestGen(1, ValidUnionTypes, [t => t]), | 343 new TestGen(1, ValidUnionTypes, [t => t]), |
233 new TestGen(1, ValidFunctionTypes, [t => t], false), | 344 new TestGen(1, ValidFunctionTypes, [t => t], false), |
234 ]); | 345 ]); |
235 } | 346 } |
236 | 347 |
237 function InvalidTypes(size) { | 348 function InvalidTypes(size) { |
238 return Generate(size, [ | 349 return Generate(size, [ |
239 new TestGen(1, InvalidUnionTypes, [t => t]), | 350 new TestGen(1, InvalidUnionTypes, [t => t]), |
240 new TestGen(1, InvalidFunctionTypes, [t => t], false), | 351 new TestGen(1, InvalidFunctionTypes, [t => t], false), |
241 ]); | 352 ]); |
242 } | 353 } |
OLD | NEW |