| 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:analyzer/file_system/file_system.dart'; | 8 import 'package:analyzer/file_system/file_system.dart'; |
| 9 import 'package:analyzer/file_system/memory_file_system.dart'; | 9 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * A function to be called for every [Element]. | 36 * A function to be called for every [Element]. |
| 37 */ | 37 */ |
| 38 typedef void _ElementVisitorFunction(Element element); | 38 typedef void _ElementVisitorFunction(Element element); |
| 39 | 39 |
| 40 | 40 |
| 41 class AbstractContextTest { | 41 class AbstractContextTest { |
| 42 static final DartSdk SDK = new MockSdk(); | 42 static final DartSdk SDK = new MockSdk(); |
| 43 static final UriResolver SDK_RESOLVER = new DartUriResolver(SDK); |
| 43 | 44 |
| 44 MemoryResourceProvider provider = new MemoryResourceProvider(); | 45 MemoryResourceProvider provider = new MemoryResourceProvider(); |
| 46 UriResolver resourceResolver; |
| 45 AnalysisContext context; | 47 AnalysisContext context; |
| 46 | 48 |
| 49 |
| 47 Source addSource(String path, String content) { | 50 Source addSource(String path, String content) { |
| 48 File file = provider.newFile(path, content); | 51 File file = provider.newFile(path, content); |
| 49 Source source = file.createSource(UriKind.FILE_URI); | 52 Source source = file.createSource(UriKind.FILE_URI); |
| 50 ChangeSet changeSet = new ChangeSet(); | 53 ChangeSet changeSet = new ChangeSet(); |
| 51 changeSet.addedSource(source); | 54 changeSet.addedSource(source); |
| 52 context.applyChanges(changeSet); | 55 context.applyChanges(changeSet); |
| 53 context.setContents(source, content); | 56 context.setContents(source, content); |
| 54 return source; | 57 return source; |
| 55 } | 58 } |
| 56 | 59 |
| 57 void addUriResolvers(List<UriResolver> resolvers) { | |
| 58 resolvers.add(new DartUriResolver(SDK)); | |
| 59 resolvers.add(new ResourceUriResolver(provider)); | |
| 60 } | |
| 61 | |
| 62 /** | 60 /** |
| 63 * Performs all analysis tasks in [context]. | 61 * Performs all analysis tasks in [context]. |
| 64 */ | 62 */ |
| 65 void performAllAnalysisTasks() { | 63 void performAllAnalysisTasks() { |
| 66 while (true) { | 64 while (true) { |
| 67 AnalysisResult result = context.performAnalysisTask(); | 65 AnalysisResult result = context.performAnalysisTask(); |
| 68 if (!result.hasMoreWork) { | 66 if (!result.hasMoreWork) { |
| 69 break; | 67 break; |
| 70 } | 68 } |
| 71 } | 69 } |
| 72 } | 70 } |
| 73 | 71 |
| 74 CompilationUnit resolveDartUnit(Source unitSource, Source librarySource) { | 72 CompilationUnit resolveDartUnit(Source unitSource, Source librarySource) { |
| 75 return context.resolveCompilationUnit2(unitSource, librarySource); | 73 return context.resolveCompilationUnit2(unitSource, librarySource); |
| 76 } | 74 } |
| 77 | 75 |
| 78 CompilationUnit resolveLibraryUnit(Source source) { | 76 CompilationUnit resolveLibraryUnit(Source source) { |
| 79 return context.resolveCompilationUnit2(source, source); | 77 return context.resolveCompilationUnit2(source, source); |
| 80 } | 78 } |
| 81 | 79 |
| 82 void setUp() { | 80 void setUp() { |
| 81 resourceResolver = new ResourceUriResolver(provider); |
| 83 context = AnalysisEngine.instance.createAnalysisContext(); | 82 context = AnalysisEngine.instance.createAnalysisContext(); |
| 84 List<UriResolver> resolvers = <UriResolver>[]; | 83 context.sourceFactory = new SourceFactory([SDK_RESOLVER, resourceResolver]); |
| 85 addUriResolvers(resolvers); | |
| 86 context.sourceFactory = new SourceFactory(resolvers); | |
| 87 } | 84 } |
| 88 | 85 |
| 89 void tearDown() { | 86 void tearDown() { |
| 90 context = null; | 87 context = null; |
| 91 provider = null; | 88 provider = null; |
| 92 } | 89 } |
| 93 } | 90 } |
| 94 | 91 |
| 95 | 92 |
| 96 /** | 93 /** |
| 97 * Wraps the given [_ElementVisitorFunction] into an instance of | 94 * Wraps the given [_ElementVisitorFunction] into an instance of |
| 98 * [engine.GeneralizingElementVisitor]. | 95 * [engine.GeneralizingElementVisitor]. |
| 99 */ | 96 */ |
| 100 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { | 97 class _ElementVisitorFunctionWrapper extends GeneralizingElementVisitor { |
| 101 final _ElementVisitorFunction function; | 98 final _ElementVisitorFunction function; |
| 102 _ElementVisitorFunctionWrapper(this.function); | 99 _ElementVisitorFunctionWrapper(this.function); |
| 103 visitElement(Element element) { | 100 visitElement(Element element) { |
| 104 function(element); | 101 function(element); |
| 105 super.visitElement(element); | 102 super.visitElement(element); |
| 106 } | 103 } |
| 107 } | 104 } |
| OLD | NEW |