Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: pkg/analyzer/test/src/dart/analysis/driver_test.dart

Issue 2455843002: Several tests for AnalysisDriver and an elements.dart tweak. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 library analyzer.test.driver;
6
7 import 'dart:async';
8 import 'dart:convert';
9
10 import 'package:analyzer/dart/ast/ast.dart';
11 import 'package:analyzer/error/error.dart';
12 import 'package:analyzer/file_system/file_system.dart';
13 import 'package:analyzer/file_system/memory_file_system.dart';
14 import 'package:analyzer/source/package_map_resolver.dart';
15 import 'package:analyzer/src/dart/analysis/byte_store.dart';
16 import 'package:analyzer/src/dart/analysis/driver.dart';
17 import 'package:analyzer/src/error/codes.dart';
18 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
19 import 'package:analyzer/src/generated/source.dart';
20 import 'package:async/async.dart';
21 import 'package:convert/convert.dart';
22 import 'package:crypto/crypto.dart';
23 import 'package:test/test.dart';
24 import 'package:test_reflective_loader/test_reflective_loader.dart';
25
26 import '../../context/mock_sdk.dart';
27
28 main() {
29 defineReflectiveSuite(() {
30 defineReflectiveTests(DriverTest);
31 });
32 }
33
34 @reflectiveTest
35 class DriverTest {
36 static final MockSdk sdk = new MockSdk();
37
38 final MemoryResourceProvider provider = new MemoryResourceProvider();
39 final ByteStore byteStore = new _TestByteStore();
40 final ContentCache contentCache = new ContentCache();
41 final StringBuffer logBuffer = new StringBuffer();
42
43 AnalysisDriver driver;
44 StreamSplitter<AnalysisStatus> statusSplitter;
45 final List<AnalysisStatus> allStatuses = <AnalysisStatus>[];
46 final List<AnalysisResult> allResults = <AnalysisResult>[];
47
48 String _testProject;
Brian Wilkerson 2016/10/27 05:06:04 Is there any value in these fields being private?
scheglov 2016/10/27 16:24:23 Done.
49 String _testFile;
50
51 void setUp() {
52 new MockSdk();
53 _testProject = _p('/test/lib');
54 _testFile = _p('/test/lib/test.dart');
55 driver = new AnalysisDriver(
56 new PerformanceLog(logBuffer),
57 provider,
58 byteStore,
59 contentCache,
60 new SourceFactory([
61 new DartUriResolver(sdk),
62 new PackageMapUriResolver(provider, <String, List<Folder>>{
63 'test': [provider.getFolder(_testProject)]
64 })
65 ], null, provider),
66 new AnalysisOptionsImpl()..strongMode = true);
67 statusSplitter = new StreamSplitter(driver.status);
68 statusSplitter.split().listen(allStatuses.add);
69 driver.results.listen(allResults.add);
70 }
71
72 test_getResult() async {
73 String content = 'int f() => 42;';
74 provider.newFile(_testFile, content);
75 driver.addFile(_testFile);
76 driver.priorityFiles = [_testFile];
Brian Wilkerson 2016/10/27 05:06:04 These four lines (or sometimes just the first thre
scheglov 2016/10/27 16:24:23 Done.
77
78 AnalysisResult result = await driver.getResult(_testFile);
79 expect(result.path, _testFile);
80 expect(result.uri.toString(), 'package:test/test.dart');
81 expect(result.content, content);
82 expect(result.contentHash, _md5(content));
83 expect(result.unit, isNotNull);
84 expect(result.errors, hasLength(0));
85
86 var f = result.unit.declarations[0] as FunctionDeclaration;
87 expect(f.name.staticType.toString(), '() → int');
88 expect(f.returnType.type.toString(), 'int');
89
90 // The same result is also received through the stream.
91 await _waitForIdle();
92 expect(allResults, [result]);
93 }
94
95 test_getResult_errors() async {
96 String content = 'main() { int vv; }';
97 provider.newFile(_testFile, content);
98 driver.addFile(_testFile);
99 driver.priorityFiles = [_testFile];
100
101 AnalysisResult result = await driver.getResult(_testFile);
102 expect(result.path, _testFile);
103 expect(result.errors, hasLength(1));
104 {
105 AnalysisError error = result.errors[0];
106 expect(error.offset, 13);
107 expect(error.length, 2);
108 expect(error.errorCode, HintCode.UNUSED_LOCAL_VARIABLE);
109 expect(error.message, "The value of the local variable 'vv' isn't used.");
110 expect(error.correction, "Try removing the variable, or using it.");
111 }
112 }
113
114 test_results_priority() async {
115 String content = 'int f() => 42;';
116 provider.newFile(_testFile, content);
117 driver.addFile(_testFile);
118 driver.priorityFiles = [_testFile];
119
120 await _waitForIdle();
121
122 expect(allResults, hasLength(1));
123 AnalysisResult result = allResults.single;
124 expect(result.path, _testFile);
125 expect(result.uri.toString(), 'package:test/test.dart');
126 expect(result.content, content);
127 expect(result.contentHash, _md5(content));
128 expect(result.unit, isNotNull);
129 expect(result.errors, hasLength(0));
130
131 var f = result.unit.declarations[0] as FunctionDeclaration;
132 expect(f.name.staticType.toString(), '() → int');
133 expect(f.returnType.type.toString(), 'int');
134 }
135
136 test_results_priorityFirst() async {
137 var a = _p('/test/lib/a.dart');
138 var b = _p('/test/lib/b.dart');
139 var c = _p('/test/lib/c.dart');
140 provider.newFile(a, 'class A {}');
141 provider.newFile(b, 'class B {}');
142 provider.newFile(c, 'class C {}');
143
144 driver.addFile(a);
145 driver.addFile(b);
146 driver.addFile(c);
147 driver.priorityFiles = [b];
148 await _waitForIdle();
149
150 expect(allResults, hasLength(3));
151 AnalysisResult result = allResults[0];
152 expect(result.path, b);
153 expect(result.unit, isNotNull);
154 expect(result.errors, hasLength(0));
155 }
156
157 test_results_regular() async {
158 String content = 'int f() => 42;';
159 provider.newFile(_testFile, content);
160 driver.addFile(_testFile);
161
162 await _waitForIdle();
163
164 expect(allResults, hasLength(1));
165 AnalysisResult result = allResults.single;
166 expect(result.path, _testFile);
167 expect(result.uri.toString(), 'package:test/test.dart');
168 expect(result.content, isNull);
169 expect(result.contentHash, _md5(content));
170 expect(result.unit, isNull);
171 expect(result.errors, hasLength(0));
172 }
173
174 test_results_status() async {
175 String content = 'int f() => 42;';
176 provider.newFile(_testFile, content);
177 driver.addFile(_testFile);
178
179 await _waitForIdle();
180
181 expect(allStatuses, hasLength(2));
182 expect(allStatuses[0].isAnalyzing, isTrue);
183 expect(allStatuses[0].isIdle, isFalse);
184 expect(allStatuses[1].isAnalyzing, isFalse);
185 expect(allStatuses[1].isIdle, isTrue);
186 }
187
188 /**
189 * Return the [provider] specific path for the given Posix [path].
190 */
191 String _p(String path) => provider.convertPath(path);
192
193 Future<Null> _waitForIdle() async {
194 await statusSplitter.split().firstWhere((status) => status.isIdle);
195 }
196
197 static String _md5(String content) {
198 return hex.encode(md5.convert(UTF8.encode(content)).bytes);
199 }
200 }
201
202 class _TestByteStore implements ByteStore {
203 final map = <String, List<int>>{};
204
205 @override
206 List<int> get(String key) {
207 return map[key];
208 }
209
210 @override
211 void put(String key, List<int> bytes) {
212 map[key] = bytes;
213 }
214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698