Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'mock_sdk.dart'; | 5 import 'mock_sdk.dart'; |
| 6 import 'package:analyzer/file_system/memory_file_system.dart'; | 6 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/sdk.dart'; | 9 import 'package:analyzer/src/generated/sdk.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:compiler/implementation/dart2jslib.dart' show NullSink; | 11 import 'package:compiler/implementation/dart2jslib.dart' show NullSink; |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 | 13 |
| 14 import '../lib/src/closed_world.dart'; | 14 import '../lib/src/closed_world.dart'; |
| 15 import '../lib/src/driver.dart'; | 15 import '../lib/src/driver.dart'; |
| 16 | 16 |
| 17 main() { | 17 main() { |
| 18 test('Toplevel function', () { | 18 test('Toplevel function', () { |
| 19 var helper = new TreeShakerTestHelper(''' | 19 var helper = new TreeShakerTestHelper(''' |
| 20 main() { | 20 main() { |
| 21 foo(); | 21 foo(); |
| 22 } | 22 } |
| 23 foo() { | 23 foo() { |
| 24 } | 24 } |
| 25 '''); | 25 '''); |
| 26 helper.assertHasFunction('main'); | 26 helper.assertHasFunction('main'); |
| 27 helper.assertHasFunction('foo'); | 27 helper.assertHasFunction('foo'); |
| 28 }); | 28 }); |
| 29 | 29 |
| 30 test('Toplevel field access', () { | 30 test('Toplevel field read', () { |
| 31 var helper = new TreeShakerTestHelper(''' | 31 var helper = new TreeShakerTestHelper(''' |
| 32 main() { | 32 main() { |
| 33 return foo; | 33 return foo; |
| 34 } | 34 } |
| 35 var foo; | 35 var foo; |
| 36 var bar; | 36 var bar; |
| 37 '''); | 37 '''); |
| 38 helper.assertHasFunction('main'); | 38 helper.assertHasFunction('main'); |
| 39 helper.assertHasVariable('foo'); | 39 helper.assertHasVariable('foo'); |
| 40 helper.assertNoVariable('bar'); | 40 helper.assertNoVariable('bar'); |
| 41 }); | 41 }); |
| 42 | 42 |
| 43 test('Toplevel field write', () { | |
| 44 var helper = new TreeShakerTestHelper(''' | |
| 45 main() { | |
| 46 foo = 1; | |
| 47 } | |
| 48 var foo; | |
| 49 var bar; | |
| 50 '''); | |
| 51 helper.assertHasFunction('main'); | |
| 52 helper.assertHasVariable('foo'); | |
| 53 helper.assertNoVariable('bar'); | |
| 54 }); | |
| 55 | |
| 43 test('Toplevel field invocation', () { | 56 test('Toplevel field invocation', () { |
| 44 var helper = new TreeShakerTestHelper(''' | 57 var helper = new TreeShakerTestHelper(''' |
| 45 main() { | 58 main() { |
| 46 return foo(); | 59 return foo(); |
| 47 } | 60 } |
| 48 var foo; | 61 var foo; |
| 49 var bar; | 62 var bar; |
| 50 '''); | 63 '''); |
| 51 helper.assertHasFunction('main'); | 64 helper.assertHasFunction('main'); |
| 52 helper.assertHasVariable('foo'); | 65 helper.assertHasVariable('foo'); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 '''); | 151 '''); |
| 139 helper.assertHasMethod('A.m1'); | 152 helper.assertHasMethod('A.m1'); |
| 140 helper.assertHasMethod('A.m2'); | 153 helper.assertHasMethod('A.m2'); |
| 141 }); | 154 }); |
| 142 | 155 |
| 143 test('Getter usage', () { | 156 test('Getter usage', () { |
| 144 var helper = new TreeShakerTestHelper(''' | 157 var helper = new TreeShakerTestHelper(''' |
| 145 class A { | 158 class A { |
| 146 get g1 => null; | 159 get g1 => null; |
| 147 get g2 => null; | 160 get g2 => null; |
| 161 set g1(x) {} | |
| 162 set g2(x) {} | |
| 148 } | 163 } |
| 149 class B { | 164 class B { |
| 150 get g1 => null; | 165 get g1 => null; |
| 151 get g2 => null; | 166 get g2 => null; |
| 167 set g1(x) {} | |
| 168 set g2(x) {} | |
| 152 } | 169 } |
| 153 main() { | 170 main() { |
| 154 new A().g1; | 171 new A().g1; |
| 155 } | 172 } |
| 156 '''); | 173 '''); |
| 157 helper.assertHasGetter('A.g1'); | 174 helper.assertHasGetter('A.g1'); |
| 158 helper.assertNoGetter('A.g2'); | 175 helper.assertNoGetter('A.g2'); |
| 159 helper.assertNoGetter('B.g1'); | 176 helper.assertNoGetter('B.g1'); |
| 160 helper.assertNoGetter('B.g2'); | 177 helper.assertNoGetter('B.g2'); |
| 178 helper.assertNoSetter('A.g1'); | |
| 179 helper.assertNoSetter('A.g2'); | |
| 180 helper.assertNoSetter('B.g1'); | |
| 181 helper.assertNoSetter('B.g2'); | |
| 182 }); | |
| 183 | |
| 184 test('Setter usage', () { | |
| 185 var helper = new TreeShakerTestHelper(''' | |
| 186 class A { | |
| 187 get g1 => null; | |
| 188 get g2 => null; | |
| 189 set g1(x) {} | |
| 190 set g2(x) {} | |
| 191 } | |
| 192 class B { | |
| 193 get g1 => null; | |
| 194 get g2 => null; | |
| 195 set g1(x) {} | |
| 196 set g2(x) {} | |
| 197 } | |
| 198 main() { | |
| 199 new A().g1 = 1; | |
| 200 } | |
| 201 '''); | |
| 202 helper.assertHasSetter('A.g1'); | |
| 203 helper.assertNoSetter('A.g2'); | |
| 204 helper.assertNoSetter('B.g1'); | |
| 205 helper.assertNoSetter('B.g2'); | |
| 206 helper.assertNoGetter('A.g1'); | |
| 207 helper.assertNoGetter('A.g2'); | |
| 208 helper.assertNoGetter('B.g1'); | |
| 209 helper.assertNoGetter('B.g2'); | |
| 161 }); | 210 }); |
| 162 | 211 |
| 163 test('Field access', () { | 212 test('Field read', () { |
| 164 var helper = new TreeShakerTestHelper(''' | 213 var helper = new TreeShakerTestHelper(''' |
| 165 class A { | 214 class A { |
| 166 var f1; | 215 var f1; |
| 167 var f2; | 216 var f2; |
| 168 } | 217 } |
| 169 class B { | 218 class B { |
| 170 var f1; | 219 var f1; |
| 171 var f2; | 220 var f2; |
| 172 } | 221 } |
| 173 main() { | 222 main() { |
| 174 new A().f1; | 223 new A().f1; |
| 175 } | 224 } |
| 176 '''); | 225 '''); |
| 177 helper.assertHasField('A.f1'); | 226 helper.assertHasField('A.f1'); |
| 178 helper.assertNoField('A.f2'); | 227 helper.assertNoField('A.f2'); |
| 179 helper.assertNoField('B.f1'); | 228 helper.assertNoField('B.f1'); |
| 180 helper.assertNoField('B.f2'); | 229 helper.assertNoField('B.f2'); |
| 181 }); | 230 }); |
| 182 | 231 |
| 232 test('Field write', () { | |
| 233 var helper = new TreeShakerTestHelper(''' | |
| 234 class A { | |
| 235 var f1; | |
| 236 var f2; | |
| 237 } | |
| 238 class B { | |
| 239 var f1; | |
| 240 var f2; | |
| 241 } | |
| 242 main() { | |
| 243 new A().f1 = 1; | |
| 244 } | |
| 245 '''); | |
| 246 helper.assertHasField('A.f1'); | |
| 247 helper.assertNoField('A.f2'); | |
| 248 helper.assertNoField('B.f1'); | |
| 249 helper.assertNoField('B.f2'); | |
| 250 }); | |
| 251 | |
| 183 test('Ordinary constructor with initializer list', () { | 252 test('Ordinary constructor with initializer list', () { |
| 184 var helper = new TreeShakerTestHelper(''' | 253 var helper = new TreeShakerTestHelper(''' |
| 185 class A { | 254 class A { |
| 186 A() : x = f(); | 255 A() : x = f(); |
| 187 var x; | 256 var x; |
| 188 foo() {} | 257 foo() {} |
| 189 } | 258 } |
| 190 f() {} | 259 f() {} |
| 191 main() { | 260 main() { |
| 192 new A().foo(); | 261 new A().foo(); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 * Methods contained in [world], indexed by className.methodName. | 338 * Methods contained in [world], indexed by className.methodName. |
| 270 */ | 339 */ |
| 271 Map<String, MethodDeclaration> methods = <String, MethodDeclaration>{}; | 340 Map<String, MethodDeclaration> methods = <String, MethodDeclaration>{}; |
| 272 | 341 |
| 273 /** | 342 /** |
| 274 * Getters contained in [world], indexed by className.propertyName. | 343 * Getters contained in [world], indexed by className.propertyName. |
| 275 */ | 344 */ |
| 276 Map<String, MethodDeclaration> getters = <String, MethodDeclaration>{}; | 345 Map<String, MethodDeclaration> getters = <String, MethodDeclaration>{}; |
| 277 | 346 |
| 278 /** | 347 /** |
| 348 * Setters contained in [world], indexed by className.propertyName. | |
| 349 */ | |
| 350 Map<String, MethodDeclaration> setters = <String, MethodDeclaration>{}; | |
| 351 | |
| 352 /** | |
| 279 * Fields contained in [world], indexed by className.fieldName. | 353 * Fields contained in [world], indexed by className.fieldName. |
| 280 */ | 354 */ |
| 281 Map<String, VariableDeclaration> fields = <String, VariableDeclaration>{}; | 355 Map<String, VariableDeclaration> fields = <String, VariableDeclaration>{}; |
| 282 | 356 |
| 283 /** | 357 /** |
| 284 * Top level variables contained in [world], indexed by name. | 358 * Top level variables contained in [world], indexed by name. |
| 285 */ | 359 */ |
| 286 Map<String, VariableDeclaration> variables = <String, VariableDeclaration>{}; | 360 Map<String, VariableDeclaration> variables = <String, VariableDeclaration>{}; |
| 287 | 361 |
| 288 /** | 362 /** |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 315 expect(declaration.element, equals(element)); | 389 expect(declaration.element, equals(element)); |
| 316 methods['${element.enclosingElement.name}.${element.name}'] = | 390 methods['${element.enclosingElement.name}.${element.name}'] = |
| 317 declaration; | 391 declaration; |
| 318 } else if (element is PropertyAccessorElement) { | 392 } else if (element is PropertyAccessorElement) { |
| 319 MethodDeclaration declaration = node as MethodDeclaration; | 393 MethodDeclaration declaration = node as MethodDeclaration; |
| 320 expect(declaration, isNotNull); | 394 expect(declaration, isNotNull); |
| 321 expect(declaration.element, equals(element)); | 395 expect(declaration.element, equals(element)); |
| 322 if (declaration.isGetter) { | 396 if (declaration.isGetter) { |
| 323 getters['${element.enclosingElement.name}.${element.name}'] = | 397 getters['${element.enclosingElement.name}.${element.name}'] = |
| 324 declaration; | 398 declaration; |
| 399 } else if (declaration.isSetter) { | |
| 400 // element.name uses the convention that setter names end in '='. | |
| 401 // Strip off the '=' to avoid confusion in writing the tests. | |
|
scheglov
2014/10/21 14:58:21
We could use element.displayName instead.
Paul Berry
2014/10/21 15:14:04
Done.
| |
| 402 String setterName = | |
| 403 '${element.enclosingElement.name}.${element.name}'; | |
| 404 assert(setterName.endsWith('=')); | |
| 405 setters[setterName.substring(0, setterName.length - 1)] = | |
| 406 declaration; | |
| 325 } else { | 407 } else { |
| 326 // TODO(paulberry): handle setters. | 408 fail('Unexpected property accessor (neither getter nor setter)'); |
| 327 throw new UnimplementedError(); | |
| 328 } | 409 } |
| 329 } | 410 } |
| 330 }); | 411 }); |
| 331 world.instantiatedClasses.forEach( | 412 world.instantiatedClasses.forEach( |
| 332 (ClassElement element, ClassDeclaration declaration) { | 413 (ClassElement element, ClassDeclaration declaration) { |
| 333 expect(declaration, isNotNull); | 414 expect(declaration, isNotNull); |
| 334 expect(declaration.element, equals(element)); | 415 expect(declaration.element, equals(element)); |
| 335 instantiatedClasses[element.name] = declaration; | 416 instantiatedClasses[element.name] = declaration; |
| 336 }); | 417 }); |
| 337 world.fields.forEach( | 418 world.fields.forEach( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 } | 451 } |
| 371 | 452 |
| 372 /** | 453 /** |
| 373 * Asserts that [world] contains a getter with the given qualified name. | 454 * Asserts that [world] contains a getter with the given qualified name. |
| 374 */ | 455 */ |
| 375 void assertHasGetter(String qualifiedName) { | 456 void assertHasGetter(String qualifiedName) { |
| 376 expect(getters, contains(qualifiedName)); | 457 expect(getters, contains(qualifiedName)); |
| 377 } | 458 } |
| 378 | 459 |
| 379 /** | 460 /** |
| 461 * Asserts that [world] contains a setter with the given qualified name. | |
| 462 */ | |
| 463 void assertHasSetter(String qualifiedName) { | |
| 464 expect(setters, contains(qualifiedName)); | |
| 465 } | |
| 466 | |
| 467 /** | |
| 380 * Asserts that [world] instantiates a class with the given name. | 468 * Asserts that [world] instantiates a class with the given name. |
| 381 */ | 469 */ |
| 382 void assertHasInstantiatedClass(String name) { | 470 void assertHasInstantiatedClass(String name) { |
| 383 expect(instantiatedClasses, contains(name)); | 471 expect(instantiatedClasses, contains(name)); |
| 384 } | 472 } |
| 385 | 473 |
| 386 /** | 474 /** |
| 387 * Asserts that [world] contains a method with the given qualified name. | 475 * Asserts that [world] contains a method with the given qualified name. |
| 388 * | 476 * |
| 389 * [qualifiedName] - the qualified name in form 'className.methodName'. | 477 * [qualifiedName] - the qualified name in form 'className.methodName'. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 418 | 506 |
| 419 /** | 507 /** |
| 420 * Asserts that [world] doesn't contain a getter with the given qualified | 508 * Asserts that [world] doesn't contain a getter with the given qualified |
| 421 * name. | 509 * name. |
| 422 */ | 510 */ |
| 423 void assertNoGetter(String qualifiedName) { | 511 void assertNoGetter(String qualifiedName) { |
| 424 expect(getters, isNot(contains(qualifiedName))); | 512 expect(getters, isNot(contains(qualifiedName))); |
| 425 } | 513 } |
| 426 | 514 |
| 427 /** | 515 /** |
| 516 * Asserts that [world] doesn't contain a setter with the given qualified | |
| 517 * name. | |
| 518 */ | |
| 519 void assertNoSetter(String qualifiedName) { | |
| 520 expect(setters, isNot(contains(qualifiedName))); | |
| 521 } | |
| 522 | |
| 523 /** | |
| 428 * Asserts that [world] doesn't instantiate a class with the given name. | 524 * Asserts that [world] doesn't instantiate a class with the given name. |
| 429 */ | 525 */ |
| 430 void assertNoInstantiatedClass(String name) { | 526 void assertNoInstantiatedClass(String name) { |
| 431 expect(instantiatedClasses, isNot(contains(name))); | 527 expect(instantiatedClasses, isNot(contains(name))); |
| 432 } | 528 } |
| 433 | 529 |
| 434 /** | 530 /** |
| 435 * Asserts that [world] doesn't contain a method with the given qualified | 531 * Asserts that [world] doesn't contain a method with the given qualified |
| 436 * name. | 532 * name. |
| 437 * | 533 * |
| 438 * [qualifiedName] - the qualified name in form 'className.methodName'. | 534 * [qualifiedName] - the qualified name in form 'className.methodName'. |
| 439 */ | 535 */ |
| 440 void assertNoMethod(String qualifiedName) { | 536 void assertNoMethod(String qualifiedName) { |
| 441 expect(methods, isNot(contains(qualifiedName))); | 537 expect(methods, isNot(contains(qualifiedName))); |
| 442 } | 538 } |
| 443 } | 539 } |
| OLD | NEW |