OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 |
| 6 load("test/mjsunit/harmony/typesystem/testgen.js"); |
| 7 |
| 8 |
| 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 |
| 11 // InvalidNonTerminal) yielding valid and non valid terms for this symbol. |
| 12 // These functions are of the form to be passed to Generate. |
| 13 // |
| 14 // Actual tests are in separate test files. |
| 15 |
| 16 |
| 17 // Primary types. |
| 18 |
| 19 function ValidPrimaryTypes(size, proper=false) { |
| 20 return Generate(size, [ |
| 21 "any", |
| 22 "void", |
| 23 "this", |
| 24 new TestGen(1, ValidTypes, [ |
| 25 t => "(" + t + ")" |
| 26 ]), |
| 27 new TestGen(1, ValidPrimaryTypes, [ |
| 28 t => t + "[]", |
| 29 t => t + "[][]", |
| 30 t => "(" + t + "[])", |
| 31 t => "(" + t + "[])[]", |
| 32 ]), |
| 33 new TestGen(1, ValidTupleTypes, [t => t]), |
| 34 proper && "number", |
| 35 proper && "boolean", |
| 36 proper && "string", |
| 37 proper && "symbol", |
| 38 // Type references |
| 39 "A", |
| 40 "Tree<number>", |
| 41 "Map<string, number>", |
| 42 // Type queries |
| 43 "typeof x", |
| 44 "typeof x.a", |
| 45 "typeof x.a.b.c" |
| 46 ]); |
| 47 } |
| 48 |
| 49 function InvalidPrimaryTypes(size, proper=false) { |
| 50 return Generate(size, [ |
| 51 // Legal parenthesized parameter lists that are not types. |
| 52 "()", "(a: number, b: string)", "(x, y, z)", |
| 53 // Illegal types in legal places. |
| 54 new TestGen(1, InvalidTypes, [ |
| 55 t => "(" + t + ")" |
| 56 ]), |
| 57 new TestGen(1, InvalidPrimaryTypes, [ |
| 58 t => t + "[]", |
| 59 t => t + "[][]", |
| 60 t => "(" + t + "[])", |
| 61 t => "(" + t + "[])[]", |
| 62 ]), |
| 63 new TestGen(1, InvalidTupleTypes, [t => t]), |
| 64 // Line terminator in arrays. |
| 65 new TestGen(1, ValidTypes, [ |
| 66 t => "(" + t + "\n[])" |
| 67 ]), |
| 68 // Type references |
| 69 "Map<string, (number, void)>", |
| 70 "number<string>", |
| 71 "Foo<>", |
| 72 // Type queries |
| 73 "typeof", |
| 74 "typeof x.", |
| 75 "typeof x => any" |
| 76 ]); |
| 77 } |
| 78 |
| 79 |
| 80 // Intersection types. |
| 81 |
| 82 function ValidIntersectionTypes(size, proper=false) { |
| 83 return Generate(size, [ |
| 84 new TestGen(1, ValidPrimaryTypes, [ |
| 85 !proper && (t => t), |
| 86 t => t + " & " + t, |
| 87 t => "(" + t + " & " + t + ") & " + t, |
| 88 t => t + " & (" + t + " & " + t + ")", |
| 89 t => t + " & " + t + " & " + t |
| 90 ]) |
| 91 ]); |
| 92 } |
| 93 |
| 94 function InvalidIntersectionTypes(size, proper=false) { |
| 95 return Generate(size, [ |
| 96 // Illegal types in legal places. |
| 97 new TestGen(4, InvalidPrimaryTypes, [ |
| 98 !proper && (t => t), |
| 99 t => t + " & " + t, |
| 100 t => "(" + t + " & " + t + ") & " + t, |
| 101 t => t + " & (" + t + " & " + t + ")", |
| 102 t => t + " & " + t + " & " + t |
| 103 ]), |
| 104 // Right hand side is a function or constructor type. |
| 105 new TestGen(1, ValidFunctionTypes, [t => "any & " + t], false), |
| 106 new TestGen(1, ValidFunctionTypes, [t => "any & " + t], true) |
| 107 ]); |
| 108 } |
| 109 |
| 110 |
| 111 // Union types. |
| 112 |
| 113 function ValidUnionTypes(size, proper=false) { |
| 114 return Generate(size, [ |
| 115 new TestGen(1, ValidIntersectionTypes, [ |
| 116 !proper && (t => t), |
| 117 t => t + " | " + t, |
| 118 t => "(" + t + " | " + t + ") | " + t, |
| 119 t => t + " | (" + t + " | " + t + ")", |
| 120 t => t + " | " + t + " | " + t |
| 121 ]) |
| 122 ]); |
| 123 } |
| 124 |
| 125 function InvalidUnionTypes(size, proper=false) { |
| 126 return Generate(size, [ |
| 127 // Illegal types in legal places. |
| 128 new TestGen(1, InvalidIntersectionTypes, [ |
| 129 !proper && (t => t), |
| 130 t => t + " | " + t, |
| 131 t => "(" + t + " | " + t + ") | " + t, |
| 132 t => t + " | (" + t + " | " + t + ")", |
| 133 t => t + " | " + t + " | " + t |
| 134 ]), |
| 135 // Right hand side is a function or constructor type. |
| 136 new TestGen(1, ValidFunctionTypes, [t => "any | " + t], false), |
| 137 new TestGen(1, ValidFunctionTypes, [t => "any | " + t], true) |
| 138 ]); |
| 139 } |
| 140 |
| 141 |
| 142 // Function and constructor types. |
| 143 |
| 144 function ValidFunctionTypes(size, constr) { |
| 145 let c = constr ? "new " : ""; |
| 146 return Generate(size, [ |
| 147 new TestGen(1, ValidTypes, [ |
| 148 t => c + "() => " + t, |
| 149 t => c + "(a: " + t + ") => " + t, |
| 150 t => c + "(a:" + t + ", b?:" + t + ") => " + t, |
| 151 t => c + "(a:" + t + ", b?:" + t + ", c) => " + t, |
| 152 t => c + "(a:" + t + ", b?:" + t + ", c?) => " + t, |
| 153 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
| 154 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
| 155 t => c + "(a:" + t + ", b:" + t + ", c, ...d: string[]) => " + t |
| 156 ]), |
| 157 // Parametric function types |
| 158 "<A> (x: A) => A", |
| 159 "<A extends string> (x: A) => A", |
| 160 "<A, B> (x: A, y: B) => A", |
| 161 "<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", |
| 163 // String literal types |
| 164 "(cmd: 'add', x: number, y: number) => number", |
| 165 '(cmd: "sum", a: number[]) => number', |
| 166 "(x: number, cmd: 'one', ...rest) => any", |
| 167 "(x: string, y: number, cmd: 'two', ...rest) => any", |
| 168 "(x: number, cmd?: 'two', ...rest) => string" |
| 169 ]); |
| 170 } |
| 171 |
| 172 function InvalidFunctionTypes(size, constr) { |
| 173 let c = constr ? "new " : ""; |
| 174 return Generate(size, [ |
| 175 // Illegal types in legal places. |
| 176 new TestGen(1, InvalidTypes, [ |
| 177 t => c + "() => " + t, |
| 178 t => c + "(a: " + t + ") => " + t, |
| 179 t => c + "(a:" + t + ", b?:" + t + ") => " + t, |
| 180 t => c + "(a:" + t + ", b?:" + t + ", c) => " + t, |
| 181 t => c + "(a:" + t + ", b?:" + t + ", c?) => " + t, |
| 182 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
| 183 t => c + "(a:" + t + ", b:" + t + ", c, ...d) => " + t, |
| 184 t => c + "(a:" + t + ", b:" + t + ", c, ...d: string[]) => " + t, |
| 185 ]), |
| 186 // Parametric function types |
| 187 "<A> A[]", |
| 188 "<> (x: number) => number", |
| 189 "<A extends ()> (x: A) => A" |
| 190 ]); |
| 191 } |
| 192 |
| 193 |
| 194 // Tuple types. |
| 195 |
| 196 function ValidTupleTypes(size) { |
| 197 return Generate(size, [ |
| 198 new TestGen(1, ValidTypes, [ |
| 199 t => "[" + t + "]", |
| 200 t => "[" + t + ", " + t + "]", |
| 201 t => "[" + t + ", " + t + ", " + t + "]" |
| 202 ]) |
| 203 ]); |
| 204 } |
| 205 |
| 206 function InvalidTupleTypes(size) { |
| 207 return Generate(size, [ |
| 208 // Illegal types in legal places. |
| 209 new TestGen(1, InvalidTypes, [ |
| 210 t => "[" + t + "]", |
| 211 t => "[" + t + ", " + t + "]", |
| 212 t => "[" + t + ", " + t + ", " + t + "]" |
| 213 ]), |
| 214 // Valid binding patterns that are not tuple types. |
| 215 "[]", |
| 216 new TestGen(1, ValidTypes, [t => "[" + t + ",]"]), |
| 217 new TestGen(1, ValidTypes, [t => "[" + t + ",,]"]), |
| 218 new TestGen(1, ValidTypes, [t => "[," + t + "]"]), |
| 219 new TestGen(1, ValidTypes, [t => "[,," + t + "]"]), |
| 220 new TestGen(1, ValidTypes, [t => "[" + t + ",," + t + "]"]), |
| 221 new TestGen(1, ValidTypes, [t => "[..." + t + "]"]), |
| 222 new TestGen(1, ValidTypes, [t => "[" + t + ", ..." + t + "]"]), |
| 223 ]); |
| 224 } |
| 225 |
| 226 |
| 227 // All types: simple, type references, type parametric, type queries |
| 228 // and tuples. |
| 229 |
| 230 function ValidTypes(size) { |
| 231 return Generate(size, [ |
| 232 new TestGen(1, ValidUnionTypes, [t => t]), |
| 233 new TestGen(1, ValidFunctionTypes, [t => t], false), |
| 234 ]); |
| 235 } |
| 236 |
| 237 function InvalidTypes(size) { |
| 238 return Generate(size, [ |
| 239 new TestGen(1, InvalidUnionTypes, [t => t]), |
| 240 new TestGen(1, InvalidFunctionTypes, [t => t], false), |
| 241 ]); |
| 242 } |
OLD | NEW |