| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'dart:async'; |
| 6 |
| 7 import 'package:analyzer/dart/analysis/session.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 10 import 'package:analyzer/src/dart/analysis/session.dart'; |
| 11 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; |
| 12 import 'package:analyzer/src/dart/element/element.dart'; |
| 13 import 'package:analyzer/src/dart/element/type.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart' |
| 15 show AnalysisOptions, AnalysisOptionsImpl; |
| 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:test/test.dart'; |
| 18 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 19 |
| 20 main() { |
| 21 defineReflectiveSuite(() { |
| 22 defineReflectiveTests(DriverBasedAnalysisSessionTest); |
| 23 }); |
| 24 } |
| 25 |
| 26 @reflectiveTest |
| 27 class DriverBasedAnalysisSessionTest { |
| 28 MockAnalysisDriver driver; |
| 29 DriverBasedAnalysisSession session; |
| 30 |
| 31 void setUp() { |
| 32 driver = new MockAnalysisDriver(); |
| 33 session = new DriverBasedAnalysisSession(driver); |
| 34 driver.currentSession = session; |
| 35 } |
| 36 |
| 37 test_getErrors() async { |
| 38 ErrorsResult result = new ErrorsResult(null, null, null, null, null); |
| 39 driver.errorsResult = result; |
| 40 expect(await session.getErrors('path'), result); |
| 41 } |
| 42 |
| 43 test_getLibraryByUri() async { |
| 44 String uri = 'uri'; |
| 45 LibraryElement element = new LibraryElementImpl(null, null, null, null); |
| 46 driver.libraryMap[uri] = element; |
| 47 expect(await session.getLibraryByUri(uri), element); |
| 48 } |
| 49 |
| 50 test_getParsedAst() async { |
| 51 ParseResult result = |
| 52 new ParseResult(null, null, null, null, null, null, null); |
| 53 driver.parseResult = result; |
| 54 expect(await session.getParsedAst('path'), result); |
| 55 } |
| 56 |
| 57 test_getResolvedAst() async { |
| 58 AnalysisResult result = new AnalysisResult( |
| 59 driver, null, null, null, null, null, null, null, null, null, null); |
| 60 driver.result = result; |
| 61 expect(await session.getResolvedAst('path'), result); |
| 62 } |
| 63 |
| 64 test_getSourceKind() async { |
| 65 SourceKind kind = SourceKind.LIBRARY; |
| 66 driver.sourceKind = kind; |
| 67 expect(await session.getSourceKind('path'), kind); |
| 68 } |
| 69 |
| 70 test_getTopLevelDeclarations() async { |
| 71 List<TopLevelDeclarationInSource> declarations = []; |
| 72 driver.topLevelDeclarations = declarations; |
| 73 expect(await session.getTopLevelDeclarations('path'), declarations); |
| 74 } |
| 75 |
| 76 test_getUnitElement() async { |
| 77 UnitElementResult result = |
| 78 new UnitElementResult(null, null, null, null, null); |
| 79 driver.unitElementResult = result; |
| 80 expect(await session.getUnitElement('path'), result); |
| 81 } |
| 82 |
| 83 test_getUnitElementSignature() async { |
| 84 String signature = 'xyzzy'; |
| 85 driver.unitElementSignature = signature; |
| 86 expect(await session.getUnitElementSignature('path'), signature); |
| 87 } |
| 88 |
| 89 test_typeProvider() async { |
| 90 _initializeSDK(); |
| 91 expect(await session.typeProvider, isNotNull); |
| 92 } |
| 93 |
| 94 test_typeSystem() async { |
| 95 _initializeSDK(); |
| 96 expect(await session.typeSystem, isNotNull); |
| 97 } |
| 98 |
| 99 void _initializeSDK() { |
| 100 CompilationUnitElementImpl newUnit(String name) { |
| 101 CompilationUnitElementImpl unit = new CompilationUnitElementImpl(name); |
| 102 unit.accessors = []; |
| 103 unit.enums = []; |
| 104 unit.functions = []; |
| 105 unit.typeAliases = []; |
| 106 return unit; |
| 107 } |
| 108 |
| 109 ClassElementImpl newClass(String name) { |
| 110 TypeParameterElementImpl param = new TypeParameterElementImpl('E', 0); |
| 111 param.type = new TypeParameterTypeImpl(param); |
| 112 ClassElementImpl element = new ClassElementImpl(name, 0); |
| 113 element.typeParameters = [param]; |
| 114 return element; |
| 115 } |
| 116 |
| 117 { |
| 118 CompilationUnitElementImpl coreUnit = newUnit('dart.core'); |
| 119 coreUnit.types = <ClassElement>[newClass('Iterable')]; |
| 120 LibraryElementImpl core = new LibraryElementImpl(null, null, null, null); |
| 121 core.definingCompilationUnit = coreUnit; |
| 122 driver.libraryMap['dart:core'] = core; |
| 123 } |
| 124 { |
| 125 CompilationUnitElementImpl asyncUnit = newUnit('dart.async'); |
| 126 asyncUnit.types = <ClassElement>[ |
| 127 newClass('Future'), |
| 128 newClass('FutureOr'), |
| 129 newClass('Stream') |
| 130 ]; |
| 131 LibraryElementImpl async = new LibraryElementImpl(null, null, null, null); |
| 132 async.definingCompilationUnit = asyncUnit; |
| 133 driver.libraryMap['dart:async'] = async; |
| 134 } |
| 135 } |
| 136 } |
| 137 |
| 138 class MockAnalysisDriver implements AnalysisDriver { |
| 139 @override |
| 140 AnalysisSession currentSession; |
| 141 |
| 142 ErrorsResult errorsResult; |
| 143 Map<String, LibraryElement> libraryMap = <String, LibraryElement>{}; |
| 144 ParseResult parseResult; |
| 145 AnalysisResult result; |
| 146 SourceKind sourceKind; |
| 147 List<TopLevelDeclarationInSource> topLevelDeclarations; |
| 148 UnitElementResult unitElementResult; |
| 149 String unitElementSignature; |
| 150 |
| 151 AnalysisOptions get analysisOptions => new AnalysisOptionsImpl(); |
| 152 |
| 153 @override |
| 154 Future<ErrorsResult> getErrors(String path) async { |
| 155 return errorsResult; |
| 156 } |
| 157 |
| 158 @override |
| 159 Future<LibraryElement> getLibraryByUri(String uri) async { |
| 160 return libraryMap[uri]; |
| 161 } |
| 162 |
| 163 @override |
| 164 Future<AnalysisResult> getResult(String path) async { |
| 165 return result; |
| 166 } |
| 167 |
| 168 @override |
| 169 Future<SourceKind> getSourceKind(String path) async { |
| 170 return sourceKind; |
| 171 } |
| 172 |
| 173 @override |
| 174 Future<List<TopLevelDeclarationInSource>> getTopLevelNameDeclarations( |
| 175 String name) async { |
| 176 return topLevelDeclarations; |
| 177 } |
| 178 |
| 179 @override |
| 180 Future<UnitElementResult> getUnitElement(String path) async { |
| 181 return unitElementResult; |
| 182 } |
| 183 |
| 184 @override |
| 185 Future<String> getUnitElementSignature(String path) async { |
| 186 return unitElementSignature; |
| 187 } |
| 188 |
| 189 @override |
| 190 dynamic noSuchMethod(Invocation invocation) { |
| 191 fail('Unexpected invocation of ${invocation.memberName}'); |
| 192 return null; |
| 193 } |
| 194 |
| 195 @override |
| 196 Future<ParseResult> parseFile(String path) async { |
| 197 return parseResult; |
| 198 } |
| 199 } |
| OLD | NEW |