| 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', () { |
| 31 var helper = new TreeShakerTestHelper(''' |
| 32 main() { |
| 33 return foo; |
| 34 } |
| 35 var foo; |
| 36 var bar; |
| 37 '''); |
| 38 helper.assertHasFunction('main'); |
| 39 helper.assertHasVariable('foo'); |
| 40 helper.assertNoVariable('bar'); |
| 41 }); |
| 42 |
| 43 test('Toplevel field invocation', () { |
| 44 var helper = new TreeShakerTestHelper(''' |
| 45 main() { |
| 46 return foo(); |
| 47 } |
| 48 var foo; |
| 49 var bar; |
| 50 '''); |
| 51 helper.assertHasFunction('main'); |
| 52 helper.assertHasVariable('foo'); |
| 53 helper.assertNoVariable('bar'); |
| 54 }); |
| 55 |
| 56 test('Member field invocation', () { |
| 57 var helper = new TreeShakerTestHelper(''' |
| 58 class A { |
| 59 void call() {} |
| 60 void baz() {} |
| 61 } |
| 62 main() { |
| 63 new A(); |
| 64 foo(); |
| 65 } |
| 66 var foo; |
| 67 var bar; |
| 68 '''); |
| 69 helper.assertHasFunction('main'); |
| 70 helper.assertHasVariable('foo'); |
| 71 helper.assertNoVariable('bar'); |
| 72 helper.assertHasInstantiatedClass('A'); |
| 73 helper.assertHasMethod('A.call'); |
| 74 helper.assertNoMethod('A.baz'); |
| 75 }); |
| 76 |
| 30 test('Class instantiation', () { | 77 test('Class instantiation', () { |
| 31 var helper = new TreeShakerTestHelper(''' | 78 var helper = new TreeShakerTestHelper(''' |
| 32 main() { | 79 main() { |
| 33 var x = new A(); | 80 var x = new A(); |
| 34 } | 81 } |
| 35 class A {} | 82 class A {} |
| 36 class B {} | 83 class B {} |
| 37 '''); | 84 '''); |
| 38 helper.assertHasInstantiatedClass('A'); | 85 helper.assertHasInstantiatedClass('A'); |
| 39 helper.assertNoInstantiatedClass('B'); | 86 helper.assertNoInstantiatedClass('B'); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 * Getters contained in [world], indexed by className.propertyName. | 206 * Getters contained in [world], indexed by className.propertyName. |
| 160 */ | 207 */ |
| 161 Map<String, MethodDeclaration> getters = <String, MethodDeclaration>{}; | 208 Map<String, MethodDeclaration> getters = <String, MethodDeclaration>{}; |
| 162 | 209 |
| 163 /** | 210 /** |
| 164 * Fields contained in [world], indexed by className.fieldName. | 211 * Fields contained in [world], indexed by className.fieldName. |
| 165 */ | 212 */ |
| 166 Map<String, VariableDeclaration> fields = <String, VariableDeclaration>{}; | 213 Map<String, VariableDeclaration> fields = <String, VariableDeclaration>{}; |
| 167 | 214 |
| 168 /** | 215 /** |
| 216 * Top level variables contained in [world], indexed by name. |
| 217 */ |
| 218 Map<String, VariableDeclaration> variables = <String, VariableDeclaration>{}; |
| 219 |
| 220 /** |
| 169 * Classes instantiated in [world], indexed by name. | 221 * Classes instantiated in [world], indexed by name. |
| 170 */ | 222 */ |
| 171 Map<String, ClassDeclaration> instantiatedClasses = <String, | 223 Map<String, ClassDeclaration> instantiatedClasses = <String, |
| 172 ClassDeclaration>{}; | 224 ClassDeclaration>{}; |
| 173 | 225 |
| 174 /** | 226 /** |
| 175 * Create a TreeShakerTestHelper based on the given file contents. | 227 * Create a TreeShakerTestHelper based on the given file contents. |
| 176 */ | 228 */ |
| 177 TreeShakerTestHelper(String contents) { | 229 TreeShakerTestHelper(String contents) { |
| 178 MemoryResourceProvider provider = new MemoryResourceProvider(); | 230 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 expect(declaration, isNotNull); | 265 expect(declaration, isNotNull); |
| 214 expect(declaration.element, equals(element)); | 266 expect(declaration.element, equals(element)); |
| 215 instantiatedClasses[element.name] = declaration; | 267 instantiatedClasses[element.name] = declaration; |
| 216 }); | 268 }); |
| 217 world.fields.forEach( | 269 world.fields.forEach( |
| 218 (FieldElement element, VariableDeclaration declaration) { | 270 (FieldElement element, VariableDeclaration declaration) { |
| 219 expect(declaration, isNotNull); | 271 expect(declaration, isNotNull); |
| 220 expect(declaration.element, equals(element)); | 272 expect(declaration.element, equals(element)); |
| 221 fields['${element.enclosingElement.name}.${element.name}'] = declaration; | 273 fields['${element.enclosingElement.name}.${element.name}'] = declaration; |
| 222 }); | 274 }); |
| 275 world.variables.forEach( |
| 276 (TopLevelVariableElement element, VariableDeclaration declaration) { |
| 277 expect(declaration, isNotNull); |
| 278 expect(declaration.element, equals(element)); |
| 279 variables['${element.name}'] = declaration; |
| 280 }); |
| 223 } | 281 } |
| 224 | 282 |
| 225 /** | 283 /** |
| 226 * Asserts that [world] contains a field with the given qualified name. | 284 * Asserts that [world] contains a field with the given qualified name. |
| 227 */ | 285 */ |
| 228 void assertHasField(String qualifiedName) { | 286 void assertHasField(String qualifiedName) { |
| 229 expect(fields, contains(qualifiedName)); | 287 expect(fields, contains(qualifiedName)); |
| 230 } | 288 } |
| 231 | 289 |
| 232 /** | 290 /** |
| 291 * Asserts that [world] contains a top level variable with the given name. |
| 292 */ |
| 293 void assertHasVariable(String name) { |
| 294 expect(variables, contains(name)); |
| 295 } |
| 296 |
| 297 /** |
| 233 * Asserts that [world] contains a top-level function with the given name. | 298 * Asserts that [world] contains a top-level function with the given name. |
| 234 */ | 299 */ |
| 235 void assertHasFunction(String name) { | 300 void assertHasFunction(String name) { |
| 236 expect(functions, contains(name)); | 301 expect(functions, contains(name)); |
| 237 } | 302 } |
| 238 | 303 |
| 239 /** | 304 /** |
| 240 * Asserts that [world] contains a getter with the given qualified name. | 305 * Asserts that [world] contains a getter with the given qualified name. |
| 241 */ | 306 */ |
| 242 void assertHasGetter(String qualifiedName) { | 307 void assertHasGetter(String qualifiedName) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 261 | 326 |
| 262 /** | 327 /** |
| 263 * Asserts that [world] doesn't contain a field with the given qualified | 328 * Asserts that [world] doesn't contain a field with the given qualified |
| 264 * name. | 329 * name. |
| 265 */ | 330 */ |
| 266 void assertNoField(String qualifiedName) { | 331 void assertNoField(String qualifiedName) { |
| 267 expect(fields, isNot(contains(qualifiedName))); | 332 expect(fields, isNot(contains(qualifiedName))); |
| 268 } | 333 } |
| 269 | 334 |
| 270 /** | 335 /** |
| 336 * Asserts that [world] doesn't contain a top level variable with the given |
| 337 * name. |
| 338 */ |
| 339 void assertNoVariable(String name) { |
| 340 expect(variables, isNot(contains(name))); |
| 341 } |
| 342 |
| 343 /** |
| 271 * Asserts that [world] doesn't contain a top-level function with the given | 344 * Asserts that [world] doesn't contain a top-level function with the given |
| 272 * name. | 345 * name. |
| 273 */ | 346 */ |
| 274 void assertNoFunction(String name) { | 347 void assertNoFunction(String name) { |
| 275 expect(functions, isNot(contains(name))); | 348 expect(functions, isNot(contains(name))); |
| 276 } | 349 } |
| 277 | 350 |
| 278 /** | 351 /** |
| 279 * Asserts that [world] doesn't contain a getter with the given qualified | 352 * Asserts that [world] doesn't contain a getter with the given qualified |
| 280 * name. | 353 * name. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 293 /** | 366 /** |
| 294 * Asserts that [world] doesn't contain a method with the given qualified | 367 * Asserts that [world] doesn't contain a method with the given qualified |
| 295 * name. | 368 * name. |
| 296 * | 369 * |
| 297 * [qualifiedName] - the qualified name in form 'className.methodName'. | 370 * [qualifiedName] - the qualified name in form 'className.methodName'. |
| 298 */ | 371 */ |
| 299 void assertNoMethod(String qualifiedName) { | 372 void assertNoMethod(String qualifiedName) { |
| 300 expect(methods, isNot(contains(qualifiedName))); | 373 expect(methods, isNot(contains(qualifiedName))); |
| 301 } | 374 } |
| 302 } | 375 } |
| OLD | NEW |