| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // TODO(ahe): This is a copy of | |
| 6 // ../../../../tests/language/src/NamingTest.dart and should be | |
| 7 // deleted when the one TODO below is addressed. | |
| 8 | |
| 9 class A { | |
| 10 A() { NamingTest.count++; } | |
| 11 foo(a, b) { | |
| 12 Expect.equals(1, a); | |
| 13 Expect.equals(2, b); | |
| 14 } | |
| 15 } | |
| 16 | |
| 17 class MyException { | |
| 18 MyException() {} | |
| 19 } | |
| 20 | |
| 21 class debugger { | |
| 22 static final int __PROTO__ = 5; | |
| 23 | |
| 24 int x; | |
| 25 | |
| 26 factory debugger.F() { | |
| 27 return new debugger(1); | |
| 28 } | |
| 29 debugger(x) : this.x = x + 1 { } | |
| 30 debugger.C(x) : this.x = x + 2 { } | |
| 31 debugger.C$C(x) : this.x = x + 3 { } | |
| 32 debugger.C$I(x) : this.x = x + 4 { } | |
| 33 } | |
| 34 | |
| 35 class debugger$C { | |
| 36 int x; | |
| 37 | |
| 38 factory debugger$C.F() { | |
| 39 return new debugger$C(1); | |
| 40 } | |
| 41 debugger$C(x) : this.x = x + 5 { } | |
| 42 debugger$C.C(x) : this.x = x + 6 { } | |
| 43 debugger$C.C$C(x) : this.x = x + 7 { } | |
| 44 debugger$C.C$I(x) : this.x = x + 8 { } | |
| 45 } | |
| 46 | |
| 47 class debugger$C$C { | |
| 48 int x; | |
| 49 | |
| 50 factory debugger$C$C.F() { | |
| 51 return new debugger$C$C(1); | |
| 52 } | |
| 53 debugger$C$C(x) : this.x = x + 9 { } | |
| 54 debugger$C$C.C(x) : this.x = x + 10 { } | |
| 55 debugger$C$C.C$C(x) : this.x = x + 11 { } | |
| 56 debugger$C$C.C$I(x) : this.x = x + 12 { } | |
| 57 } | |
| 58 | |
| 59 class with extends debugger$C { | |
| 60 int y; | |
| 61 | |
| 62 factory with.F() { | |
| 63 return new with(1, 2); | |
| 64 } | |
| 65 with(x, y) : super(x), this.y = y + 1 { } | |
| 66 with.I(x, y) : super.C(x), this.y = y + 2 { } | |
| 67 with.C(x, y) : super.C$C(x), this.y = y + 3 { } | |
| 68 with.I$C(x, y) : super.C$I(x), this.y = y + 4 { } | |
| 69 with.C$C(x, y) : super(x), this.y = y + 5 { } | |
| 70 with.C$C$C(x, y) : super.C(x), this.y = y + 6 { } | |
| 71 with.$C$I(x, y) : super.C$C(x), this.y = y + 7 { } | |
| 72 with.$$I$C(x, y) : super.C$I(x), this.y = y + 8 { } | |
| 73 with.$(x, y) : super(x), this.y = y + 9 { } | |
| 74 with.$$(x, y) : super.C(x), this.y = y + 10 { } | |
| 75 } | |
| 76 | |
| 77 class with$I extends debugger$C { | |
| 78 int y; | |
| 79 | |
| 80 factory with$I.F() { | |
| 81 return new with$I(1, 2); | |
| 82 } | |
| 83 with$I(x, y) : super(x), this.y = y + 11 { } | |
| 84 with$I.I(x, y) : super.C(x), this.y = y + 12 { } | |
| 85 with$I.C(x, y) : super.C$C(x), this.y = y + 13 { } | |
| 86 with$I.I$C(x, y) : super.C$I(x), this.y = y + 14 { } | |
| 87 with$I.C$C(x, y) : super(x), this.y = y + 15 { } | |
| 88 with$I.C$C$C(x, y) : super.C(x), this.y = y + 16 { } | |
| 89 with$I.$C$I(x, y) : super.C$C(x), this.y = y + 17 { } | |
| 90 with$I.$$I$C(x, y) : super.C$I(x), this.y = y + 18 { } | |
| 91 with$I.$(x, y) : super(x), this.y = y + 19 { } | |
| 92 with$I.$$(x, y) : super.C(x), this.y = y + 20 { } | |
| 93 } | |
| 94 | |
| 95 class with$C extends debugger$C$C { | |
| 96 int y; | |
| 97 | |
| 98 factory with$C.F() { | |
| 99 return new with$C(1, 2); | |
| 100 } | |
| 101 with$C(x, y) : super(x), this.y = y + 21 { } | |
| 102 with$C.I(x, y) : super.C(x), this.y = y + 22 { } | |
| 103 with$C.C(x, y) : super.C$C(x), this.y = y + 23 { } | |
| 104 with$C.I$C(x, y) : super.C$I(x), this.y = y + 24 { } | |
| 105 with$C.C$C(x, y) : super(x), this.y = y + 25 { } | |
| 106 with$C.C$C$C(x, y) : super.C(x), this.y = y + 26 { } | |
| 107 with$C.$C$I(x, y) : super.C$C(x), this.y = y + 27 { } | |
| 108 with$C.$$I$C(x, y) : super.C$I(x), this.y = y + 28 { } | |
| 109 with$C.$(x, y) : super(x), this.y = y + 29 { } | |
| 110 with$C.$$(x, y) : super.C(x), this.y = y + 30 { } | |
| 111 } | |
| 112 | |
| 113 class with$I$C extends debugger$C$C { | |
| 114 int y; | |
| 115 | |
| 116 factory with$I$C.F() { | |
| 117 return new with$I$C(1, 2); | |
| 118 } | |
| 119 with$I$C(x, y) : super(x), this.y = y + 31 { } | |
| 120 with$I$C.I(x, y) : super.C(x), this.y = y + 32 { } | |
| 121 with$I$C.C(x, y) : super.C$C(x), this.y = y + 33 { } | |
| 122 with$I$C.I$C(x, y) : super.C$I(x), this.y = y + 34 { } | |
| 123 with$I$C.C$C(x, y) : super(x), this.y = y + 35 { } | |
| 124 with$I$C.C$C$C(x, y) : super.C(x), this.y = y + 36 { } | |
| 125 with$I$C.$C$I(x, y) : super.C$C(x), this.y = y + 37 { } | |
| 126 with$I$C.$$I$C(x, y) : super.C$I(x), this.y = y + 38 { } | |
| 127 with$I$C.$(x, y) : super(x), this.y = y + 39 { } | |
| 128 with$I$C.$$(x, y) : super.C(x), this.y = y + 40 { } | |
| 129 } | |
| 130 | |
| 131 class Tata { | |
| 132 var prototype; | |
| 133 | |
| 134 Tata() : this.prototype = 0 {} | |
| 135 | |
| 136 __PROTO__$() { return 12; } | |
| 137 } | |
| 138 | |
| 139 class Toto extends Tata { | |
| 140 var __PROTO__; | |
| 141 | |
| 142 Toto() : super(), this.__PROTO__ = 0 { } | |
| 143 | |
| 144 prototype$() { return 10; } | |
| 145 | |
| 146 titi() { | |
| 147 Expect.equals(0, prototype); | |
| 148 Expect.equals(0, __PROTO__); | |
| 149 prototype = 3; | |
| 150 __PROTO__ = 5; | |
| 151 Expect.equals(3, prototype); | |
| 152 Expect.equals(5, __PROTO__); | |
| 153 Expect.equals(10, prototype$()); | |
| 154 Expect.equals(12, __PROTO__$()); | |
| 155 Expect.equals(12, this.__PROTO__$()); | |
| 156 Expect.equals(10, this.prototype$()); | |
| 157 Expect.equals(12, __PROTO__$()); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 class Bug4082360 { | |
| 162 int x_; | |
| 163 Bug4082360() {} | |
| 164 | |
| 165 int get x() { return x_; } | |
| 166 void set x(int value) { x_ = value; } | |
| 167 | |
| 168 void indirectSet(int value) { x = value; } | |
| 169 | |
| 170 static void test() { | |
| 171 var bug = new Bug4082360(); | |
| 172 bug.indirectSet(42); | |
| 173 Expect.equals(42, bug.x_); | |
| 174 Expect.equals(42, bug.x); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 class Hoisting { | |
| 179 var f_; | |
| 180 Hoisting.negate(var x) { | |
| 181 f_ = () { return x; }; | |
| 182 } | |
| 183 | |
| 184 operator negate() { | |
| 185 var x = 3; | |
| 186 return () { return x + 1; }; | |
| 187 } | |
| 188 | |
| 189 operator[] (x) { | |
| 190 return () { return x + 3; }; | |
| 191 } | |
| 192 | |
| 193 static void test() { | |
| 194 var h = new Hoisting.negate(1); | |
| 195 Expect.equals(1, (h.f_)()); | |
| 196 var f = -h; | |
| 197 Expect.equals(4, f()); | |
| 198 // TODO(floitsch): Investigate why this fails: | |
| 199 // Expect.equals(4, h.negate()()); | |
| 200 Expect.equals(7, h[4]()); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 // It is not possible to make sure that the backend uses the hardcoded names | |
| 205 // we are testing against. This test might therefore become rapidly out of date | |
| 206 class NamingTest { | |
| 207 static int count; | |
| 208 | |
| 209 static testExceptionNaming() { | |
| 210 // Exceptions use a hardcoded "e" as exception name. If the namer works | |
| 211 // correctly then it will be renamed in case of clashes. | |
| 212 var e = 3; | |
| 213 var caught = false; | |
| 214 try { | |
| 215 throw new MyException(); | |
| 216 } catch (var exc) { | |
| 217 try { | |
| 218 throw new MyException(); | |
| 219 } catch (var exc2) { | |
| 220 exc = 9; | |
| 221 } | |
| 222 Expect.equals(9, exc); | |
| 223 caught = true; | |
| 224 } | |
| 225 Expect.equals(true, caught); | |
| 226 Expect.equals(3, e); | |
| 227 } | |
| 228 | |
| 229 static testTmpNaming() { | |
| 230 Expect.equals(0, count); | |
| 231 var tmp$0 = 1; | |
| 232 var tmp$1 = 2; | |
| 233 new A().foo(tmp$0, tmp$1++); | |
| 234 Expect.equals(1, count); | |
| 235 Expect.equals(3, tmp$1); | |
| 236 } | |
| 237 | |
| 238 static testScopeNaming() { | |
| 239 // Alias scopes use a hardcoded "dartc_scp$<depth>" as names. | |
| 240 var dartc_scp$1 = 5; | |
| 241 var foo = 8; | |
| 242 var f = () { | |
| 243 var dartc_scp$1 = 15; | |
| 244 return foo + dartc_scp$1; | |
| 245 }; | |
| 246 Expect.equals(5, dartc_scp$1); | |
| 247 Expect.equals(23, f()); | |
| 248 } | |
| 249 | |
| 250 static testGlobalMangling() { | |
| 251 var x; | |
| 252 x = new debugger(0); | |
| 253 Expect.equals(1, x.x); | |
| 254 x = new debugger.C(0); | |
| 255 Expect.equals(2, x.x); | |
| 256 x = new debugger.C$C(0); | |
| 257 Expect.equals(3, x.x); | |
| 258 x = new debugger.C$I(0); | |
| 259 Expect.equals(4, x.x); | |
| 260 x = new debugger$C(0); | |
| 261 Expect.equals(5, x.x); | |
| 262 x = new debugger$C.C(0); | |
| 263 Expect.equals(6, x.x); | |
| 264 x = new debugger$C.C$C(0); | |
| 265 Expect.equals(7, x.x); | |
| 266 x = new debugger$C.C$I(0); | |
| 267 Expect.equals(8, x.x); | |
| 268 x = new debugger$C$C(0); | |
| 269 Expect.equals(9, x.x); | |
| 270 x = new debugger$C$C.C(0); | |
| 271 Expect.equals(10, x.x); | |
| 272 x = new debugger$C$C.C$C(0); | |
| 273 Expect.equals(11, x.x); | |
| 274 x = new debugger$C$C.C$I(0); | |
| 275 Expect.equals(12, x.x); | |
| 276 x = new with(0, 0); | |
| 277 Expect.equals(5, x.x); | |
| 278 Expect.equals(1, x.y); | |
| 279 x = new with.I(0, 0); | |
| 280 Expect.equals(6, x.x); | |
| 281 Expect.equals(2, x.y); | |
| 282 x = new with.C(0, 0); | |
| 283 Expect.equals(7, x.x); | |
| 284 Expect.equals(3, x.y); | |
| 285 x = new with.I$C(0, 0); | |
| 286 Expect.equals(8, x.x); | |
| 287 Expect.equals(4, x.y); | |
| 288 x = new with.C$C(0, 0); | |
| 289 Expect.equals(5, x.x); | |
| 290 Expect.equals(5, x.y); | |
| 291 x = new with.C$C$C(0, 0); | |
| 292 Expect.equals(6, x.x); | |
| 293 Expect.equals(6, x.y); | |
| 294 x = new with.$C$I(0, 0); | |
| 295 Expect.equals(7, x.x); | |
| 296 Expect.equals(7, x.y); | |
| 297 x = new with.$$I$C(0, 0); | |
| 298 Expect.equals(8, x.x); | |
| 299 Expect.equals(8, x.y); | |
| 300 x = new with.$(0, 0); | |
| 301 Expect.equals(5, x.x); | |
| 302 Expect.equals(9, x.y); | |
| 303 x = new with.$$(0, 0); | |
| 304 Expect.equals(6, x.x); | |
| 305 Expect.equals(10, x.y); | |
| 306 x = new with$I(0, 0); | |
| 307 Expect.equals(5, x.x); | |
| 308 Expect.equals(11, x.y); | |
| 309 x = new with$I.I(0, 0); | |
| 310 Expect.equals(6, x.x); | |
| 311 Expect.equals(12, x.y); | |
| 312 x = new with$I.C(0, 0); | |
| 313 Expect.equals(7, x.x); | |
| 314 Expect.equals(13, x.y); | |
| 315 x = new with$I.I$C(0, 0); | |
| 316 Expect.equals(8, x.x); | |
| 317 Expect.equals(14, x.y); | |
| 318 x = new with$I.C$C(0, 0); | |
| 319 Expect.equals(5, x.x); | |
| 320 Expect.equals(15, x.y); | |
| 321 x = new with$I.C$C$C(0, 0); | |
| 322 Expect.equals(6, x.x); | |
| 323 Expect.equals(16, x.y); | |
| 324 x = new with$I.$C$I(0, 0); | |
| 325 Expect.equals(7, x.x); | |
| 326 Expect.equals(17, x.y); | |
| 327 x = new with$I.$$I$C(0, 0); | |
| 328 Expect.equals(8, x.x); | |
| 329 Expect.equals(18, x.y); | |
| 330 x = new with$I.$(0, 0); | |
| 331 Expect.equals(5, x.x); | |
| 332 Expect.equals(19, x.y); | |
| 333 x = new with$I.$$(0, 0); | |
| 334 Expect.equals(6, x.x); | |
| 335 Expect.equals(20, x.y); | |
| 336 x = new with$C(0, 0); | |
| 337 Expect.equals(9, x.x); | |
| 338 Expect.equals(21, x.y); | |
| 339 x = new with$C.I(0, 0); | |
| 340 Expect.equals(10, x.x); | |
| 341 Expect.equals(22, x.y); | |
| 342 x = new with$C.C(0, 0); | |
| 343 Expect.equals(11, x.x); | |
| 344 Expect.equals(23, x.y); | |
| 345 x = new with$C.I$C(0, 0); | |
| 346 Expect.equals(12, x.x); | |
| 347 Expect.equals(24, x.y); | |
| 348 x = new with$C.C$C(0, 0); | |
| 349 Expect.equals(9, x.x); | |
| 350 Expect.equals(25, x.y); | |
| 351 x = new with$C.C$C$C(0, 0); | |
| 352 Expect.equals(10, x.x); | |
| 353 Expect.equals(26, x.y); | |
| 354 x = new with$C.$C$I(0, 0); | |
| 355 Expect.equals(11, x.x); | |
| 356 Expect.equals(27, x.y); | |
| 357 x = new with$C.$$I$C(0, 0); | |
| 358 Expect.equals(12, x.x); | |
| 359 Expect.equals(28, x.y); | |
| 360 x = new with$C.$(0, 0); | |
| 361 Expect.equals(9, x.x); | |
| 362 Expect.equals(29, x.y); | |
| 363 x = new with$C.$$(0, 0); | |
| 364 Expect.equals(10, x.x); | |
| 365 Expect.equals(30, x.y); | |
| 366 x = new with$I$C(0, 0); | |
| 367 Expect.equals(9, x.x); | |
| 368 Expect.equals(31, x.y); | |
| 369 x = new with$I$C.I(0, 0); | |
| 370 Expect.equals(10, x.x); | |
| 371 Expect.equals(32, x.y); | |
| 372 x = new with$I$C.C(0, 0); | |
| 373 Expect.equals(11, x.x); | |
| 374 Expect.equals(33, x.y); | |
| 375 x = new with$I$C.I$C(0, 0); | |
| 376 Expect.equals(12, x.x); | |
| 377 Expect.equals(34, x.y); | |
| 378 x = new with$I$C.C$C(0, 0); | |
| 379 Expect.equals(9, x.x); | |
| 380 Expect.equals(35, x.y); | |
| 381 x = new with$I$C.C$C$C(0, 0); | |
| 382 Expect.equals(10, x.x); | |
| 383 Expect.equals(36, x.y); | |
| 384 x = new with$I$C.$C$I(0, 0); | |
| 385 Expect.equals(11, x.x); | |
| 386 Expect.equals(37, x.y); | |
| 387 x = new with$I$C.$$I$C(0, 0); | |
| 388 Expect.equals(12, x.x); | |
| 389 Expect.equals(38, x.y); | |
| 390 x = new with$I$C.$(0, 0); | |
| 391 Expect.equals(9, x.x); | |
| 392 Expect.equals(39, x.y); | |
| 393 x = new with$I$C.$$(0, 0); | |
| 394 Expect.equals(10, x.x); | |
| 395 Expect.equals(40, x.y); | |
| 396 var wasCaught = false; | |
| 397 try { | |
| 398 throw new with(0, 0); | |
| 399 } catch(with e) { | |
| 400 wasCaught = true; | |
| 401 Expect.equals(5, e.x); | |
| 402 } | |
| 403 Expect.equals(true, wasCaught); | |
| 404 } | |
| 405 | |
| 406 static void testMemberMangling() { | |
| 407 Expect.equals(5, debugger.__PROTO__); | |
| 408 new Toto().titi(); | |
| 409 } | |
| 410 | |
| 411 static void testFactoryMangling() { | |
| 412 var o = new debugger.F(); | |
| 413 Expect.equals(2, o.x); | |
| 414 o = new debugger$C.F(); | |
| 415 Expect.equals(6, o.x); | |
| 416 o = new debugger$C$C.F(); | |
| 417 Expect.equals(10, o.x); | |
| 418 o = new with.F(); | |
| 419 Expect.equals(6, o.x); | |
| 420 Expect.equals(3, o.y); | |
| 421 o = new with$I.F(); | |
| 422 Expect.equals(6, o.x); | |
| 423 Expect.equals(13, o.y); | |
| 424 o = new with$C.F(); | |
| 425 Expect.equals(10, o.x); | |
| 426 Expect.equals(23, o.y); | |
| 427 o = new with$I$C.F(); | |
| 428 Expect.equals(10, o.x); | |
| 429 Expect.equals(33, o.y); | |
| 430 } | |
| 431 | |
| 432 static testFunctionParameters() { | |
| 433 a(with) { | |
| 434 return with; | |
| 435 } | |
| 436 | |
| 437 b(eval) { | |
| 438 return eval; | |
| 439 } | |
| 440 | |
| 441 c(arguments) { | |
| 442 return arguments; | |
| 443 } | |
| 444 | |
| 445 Expect.equals(10, a(10)); | |
| 446 Expect.equals(10, b(10)); | |
| 447 Expect.equals(10, c(10)); | |
| 448 } | |
| 449 | |
| 450 static testPseudoTokens() { | |
| 451 var EOS = 400; | |
| 452 var ILLEGAL = 99; | |
| 453 Expect.equals(499, EOS + ILLEGAL); | |
| 454 } | |
| 455 | |
| 456 static testDollar() { | |
| 457 Expect.equals(123, $(123).wrapped); | |
| 458 var x = new Object(), y = new Object(); | |
| 459 Expect.identical(x, $(x).wrapped); | |
| 460 Expect.identical(y, $(x).$add(y)); | |
| 461 Expect.identical(x, $(x).$negate()); | |
| 462 Expect.equals(123, $(x) + x); | |
| 463 Expect.equals(444, -$(x)); | |
| 464 } | |
| 465 | |
| 466 static void testMain() { | |
| 467 count = 0; | |
| 468 testExceptionNaming(); | |
| 469 testTmpNaming(); | |
| 470 testScopeNaming(); | |
| 471 testGlobalMangling(); | |
| 472 testMemberMangling(); | |
| 473 testFactoryMangling(); | |
| 474 testFunctionParameters(); | |
| 475 Bug4082360.test(); | |
| 476 Hoisting.test(); | |
| 477 testPseudoTokens(); | |
| 478 testDollar(); | |
| 479 } | |
| 480 } | |
| 481 | |
| 482 // Test that the generated JS names don't conflict with "$" | |
| 483 class DartQuery { | |
| 484 Object wrapped; | |
| 485 DartQuery(this.wrapped); | |
| 486 | |
| 487 $add(Object other) => other; | |
| 488 $negate() => wrapped; | |
| 489 | |
| 490 operator +(Object other) => 123; | |
| 491 operator negate() => 444; | |
| 492 } | |
| 493 | |
| 494 $add(Object first, Object second) => second; | |
| 495 DartQuery $(Object obj) => new DartQuery(obj); | |
| 496 | |
| 497 // Ensure we don't have false positive. named constructor and methods | |
| 498 // are in different namespaces, therefore it is ok to have a method | |
| 499 // called foo and a named constructor CLASS.foo | |
| 500 class Naming1Test { | |
| 501 Naming1Test.foo() { } | |
| 502 foo() { } | |
| 503 | |
| 504 static void main(args) { | |
| 505 var a = new Naming1Test.foo(); | |
| 506 a.foo(); | |
| 507 } | |
| 508 } | |
| 509 | |
| 510 // Ensure we don't have false positive. | |
| 511 class Naming2Test { | |
| 512 Naming2Test() { } | |
| 513 int get foo() { return 1; } | |
| 514 set foo(x) { } | |
| 515 | |
| 516 static void main(args) { | |
| 517 var a = new Naming2Test(); | |
| 518 Expect.throws(() => a.foo(2), (e) => e is ObjectNotClosureException); | |
| 519 } | |
| 520 } | |
| 521 | |
| 522 main() { | |
| 523 NamingTest.testMain(); | |
| 524 Naming1Test.main(null); | |
| 525 Naming2Test.main(null); | |
| 526 } | |
| OLD | NEW |