| 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 library testing.abstract_context; | 5 library testing.abstract_context; |
| 6 | 6 |
| 7 import 'package:analysis_testing/mock_sdk.dart'; | 7 import 'package:analysis_testing/mock_sdk.dart'; |
| 8 import 'package:analysis_testing/reflective_tests.dart'; | |
| 9 import 'package:analyzer/file_system/file_system.dart'; | 8 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/file_system/memory_file_system.dart'; | 9 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 11 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/sdk.dart'; | 13 import 'package:analyzer/src/generated/sdk.dart'; |
| 15 import 'package:analyzer/src/generated/source_io.dart'; | 14 import 'package:analyzer/src/generated/source_io.dart'; |
| 16 | 15 |
| 17 | 16 |
| 18 /** | 17 /** |
| 19 * Finds an [engine.Element] with the given [name]. | 18 * Finds an [Element] with the given [name]. |
| 20 */ | 19 */ |
| 21 Element findElementInUnit(CompilationUnit unit, String name, [ElementKind kind]) | 20 Element findChildElement(Element root, String name, [ElementKind kind]) { |
| 22 { | |
| 23 Element result = null; | 21 Element result = null; |
| 24 unit.element.accept(new _ElementVisitorFunctionWrapper((Element element) { | 22 root.accept(new _ElementVisitorFunctionWrapper((Element element) { |
| 25 if (element.name != name) { | 23 if (element.name != name) { |
| 26 return; | 24 return; |
| 27 } | 25 } |
| 28 if (kind != null && element.kind != kind) { | 26 if (kind != null && element.kind != kind) { |
| 29 return; | 27 return; |
| 30 } | 28 } |
| 31 result = element; | 29 result = element; |
| 32 })); | 30 })); |
| 33 return result; | 31 return result; |
| 34 } | 32 } |
| 35 | 33 |
| 36 | 34 |
| 37 /** | 35 /** |
| 38 * A function to be called for every [Element]. | 36 * A function to be called for every [Element]. |
| 39 */ | 37 */ |
| 40 typedef void _ElementVisitorFunction(Element element); | 38 typedef void _ElementVisitorFunction(Element element); |
| 41 | 39 |
| 42 | 40 |
| 43 @ReflectiveTestCase() | |
| 44 class AbstractContextTest { | 41 class AbstractContextTest { |
| 45 static final DartSdk SDK = new MockSdk(); | 42 static final DartSdk SDK = new MockSdk(); |
| 46 | 43 |
| 47 MemoryResourceProvider provider = new MemoryResourceProvider(); | 44 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 48 AnalysisContext context; | 45 AnalysisContext context; |
| 49 | 46 |
| 50 Source addSource(String path, String content) { | 47 Source addSource(String path, String content) { |
| 51 File file = provider.newFile(path, content); | 48 File file = provider.newFile(path, content); |
| 52 Source source = file.createSource(UriKind.FILE_URI); | 49 Source source = file.createSource(UriKind.FILE_URI); |
| 53 ChangeSet changeSet = new ChangeSet(); | 50 ChangeSet changeSet = new ChangeSet(); |
| 54 changeSet.addedSource(source); | 51 changeSet.addedSource(source); |
| 55 context.applyChanges(changeSet); | 52 context.applyChanges(changeSet); |
| 56 context.setContents(source, content); | 53 context.setContents(source, content); |
| 57 return source; | 54 return source; |
| 58 } | 55 } |
| 59 | 56 |
| 57 CompilationUnit resolveDartUnit(Source unitSource, Source librarySource) { |
| 58 return context.resolveCompilationUnit2(unitSource, librarySource); |
| 59 } |
| 60 |
| 60 CompilationUnit resolveLibraryUnit(Source source) { | 61 CompilationUnit resolveLibraryUnit(Source source) { |
| 61 return context.resolveCompilationUnit2(source, source); | 62 return context.resolveCompilationUnit2(source, source); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void setUp() { | 65 void setUp() { |
| 65 context = AnalysisEngine.instance.createAnalysisContext(); | 66 context = AnalysisEngine.instance.createAnalysisContext(); |
| 66 context.sourceFactory = new SourceFactory(<UriResolver>[new DartUriResolver( | 67 context.sourceFactory = new SourceFactory(<UriResolver>[new DartUriResolver( |
| 67 SDK), new ResourceUriResolver(provider)]); | 68 SDK), new ResourceUriResolver(provider)]); |
| 68 } | 69 } |
| 69 | 70 |
| 70 void tearDown() { | 71 void tearDown() { |
| 71 context = null; | 72 context = null; |
| 72 provider = null; | 73 provider = null; |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 | 76 |
| 76 | 77 |
| 77 /** | 78 /** |
| 78 * Wraps the given [_ElementVisitorFunction] into an instance of | 79 * Wraps the given [_ElementVisitorFunction] into an instance of |
| 79 * [engine.GeneralizingElementVisitor]. | 80 * [engine.GeneralizingElementVisitor]. |
| 80 */ | 81 */ |
| 81 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { | 82 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { |
| 82 final _ElementVisitorFunction function; | 83 final _ElementVisitorFunction function; |
| 83 _ElementVisitorFunctionWrapper(this.function); | 84 _ElementVisitorFunctionWrapper(this.function); |
| 84 visitElement(Element element) { | 85 visitElement(Element element) { |
| 85 function(element); | 86 function(element); |
| 86 super.visitElement(element); | 87 super.visitElement(element); |
| 87 } | 88 } |
| 88 } | 89 } |
| OLD | NEW |