| 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 // Test of Compiler.forgetElement. | 5 // Test of Compiler.forgetElement. |
| 6 library trydart.forget_element_test; | 6 library trydart.forget_element_test; |
| 7 | 7 |
| 8 import 'package:compiler/implementation/elements/elements.dart' show | 8 import 'package:compiler/implementation/elements/elements.dart' show |
| 9 LocalFunctionElement; | 9 LocalFunctionElement, |
| 10 MetadataAnnotation; |
| 11 |
| 12 import 'package:compiler/implementation/js_backend/js_backend.dart' show |
| 13 JavaScriptBackend; |
| 10 | 14 |
| 11 import 'compiler_test_case.dart'; | 15 import 'compiler_test_case.dart'; |
| 12 | 16 |
| 13 class ForgetElementTestCase extends CompilerTestCase { | 17 class ForgetElementTestCase extends CompilerTestCase { |
| 14 final int expectedClosureCount; | 18 final int expectedClosureCount; |
| 15 | 19 |
| 16 ForgetElementTestCase(String source, {int closureCount}) | 20 final int expectedMetadataCount; |
| 21 |
| 22 ForgetElementTestCase( |
| 23 String source, |
| 24 {int closureCount: 0, |
| 25 int metadataCount: 0}) |
| 17 : this.expectedClosureCount = closureCount, | 26 : this.expectedClosureCount = closureCount, |
| 27 this.expectedMetadataCount = metadataCount, |
| 18 super(source); | 28 super(source); |
| 19 | 29 |
| 20 Future run() => compile().then((LibraryElement library) { | 30 Future run() => compile().then((LibraryElement library) { |
| 21 | 31 |
| 22 // Check that the compiler has recorded the expected number of closures. | 32 // Check that the compiler has recorded the expected number of closures. |
| 23 Expect.equals(expectedClosureCount, closuresInLibrary(library).length); | 33 Expect.equals( |
| 34 expectedClosureCount, closuresInLibrary(library).length, |
| 35 'closure count'); |
| 36 |
| 37 // Check that the compiler has recorded the expected number of metadata |
| 38 // annotations. |
| 39 Expect.equals( |
| 40 expectedMetadataCount, metadataInLibrary(library).length, |
| 41 'metadata count'); |
| 24 | 42 |
| 25 // Forget about all elements. | 43 // Forget about all elements. |
| 26 library.forEachLocalMember(compiler.forgetElement); | 44 library.forEachLocalMember(compiler.forgetElement); |
| 27 | 45 |
| 28 // Check that all the closures were forgotten. | 46 // Check that all the closures were forgotten. |
| 29 Expect.isTrue(closuresInLibrary(library).isEmpty); | 47 Expect.isTrue(closuresInLibrary(library).isEmpty, 'closures'); |
| 48 |
| 49 // Check that the metadata annotations were forgotten. |
| 50 Expect.isTrue(metadataInLibrary(library).isEmpty, 'metadata'); |
| 30 }); | 51 }); |
| 31 | 52 |
| 32 Iterable closuresInLibrary(LibraryElement library) { | 53 Iterable closuresInLibrary(LibraryElement library) { |
| 33 return compiler.enqueuer.resolution.universe.allClosures.where( | 54 return compiler.enqueuer.resolution.universe.allClosures.where( |
| 34 (LocalFunctionElement closure) => closure.library == library); | 55 (LocalFunctionElement closure) => closure.library == library); |
| 35 } | 56 } |
| 57 |
| 58 Iterable metadataInLibrary(LibraryElement library) { |
| 59 JavaScriptBackend backend = compiler.backend; |
| 60 return backend.constants.metadataConstantMap.keys.where( |
| 61 (MetadataAnnotation metadata) { |
| 62 return metadata.annotatedElement.library == library; |
| 63 }); |
| 64 } |
| 36 } | 65 } |
| 37 | 66 |
| 67 const String CONSTANT_CLASS = 'class Constant { const Constant(); }'; |
| 68 |
| 38 void main() { | 69 void main() { |
| 39 runTests( | 70 runTests( |
| 40 [ | 71 [ |
| 41 new ForgetElementTestCase( | 72 new ForgetElementTestCase( |
| 42 'main() {}', | 73 'main() {}'), |
| 43 closureCount: 0), | |
| 44 | 74 |
| 45 new ForgetElementTestCase( | 75 new ForgetElementTestCase( |
| 46 'main() => null;', | 76 'main() => null;'), |
| 47 closureCount: 0), | |
| 48 | 77 |
| 49 new ForgetElementTestCase( | 78 new ForgetElementTestCase( |
| 50 'main() => (() => null)();', | 79 'main() => (() => null)();', |
| 51 closureCount: 1), | 80 closureCount: 1), |
| 52 | 81 |
| 53 new ForgetElementTestCase( | 82 new ForgetElementTestCase( |
| 54 'main() => (() => (() => null)())();', | 83 'main() => (() => (() => null)())();', |
| 55 closureCount: 2), | 84 closureCount: 2), |
| 56 | 85 |
| 57 new ForgetElementTestCase( | 86 new ForgetElementTestCase( |
| 58 'main() => (() => (() => (() => null)())())();', | 87 'main() => (() => (() => (() => null)())())();', |
| 59 closureCount: 3), | 88 closureCount: 3), |
| 89 |
| 90 new ForgetElementTestCase( |
| 91 '@Constant() main() => null; $CONSTANT_CLASS', |
| 92 metadataCount: 1), |
| 93 |
| 94 new ForgetElementTestCase( |
| 95 'main() => ((@Constant() x) => x)(null); $CONSTANT_CLASS', |
| 96 closureCount: 1, |
| 97 metadataCount: 1), |
| 60 ] | 98 ] |
| 61 ); | 99 ); |
| 62 } | 100 } |
| OLD | NEW |