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

Side by Side Diff: lib/src/testing.dart

Issue 1143683002: cleanup: simplify creation of AnalysisContext (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 7 months 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
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 dev_compiler.src.testing; 5 library dev_compiler.src.testing;
6 6
7 import 'package:analyzer/file_system/file_system.dart'; 7 import 'package:analyzer/file_system/file_system.dart';
8 import 'package:analyzer/file_system/memory_file_system.dart'; 8 import 'package:analyzer/file_system/memory_file_system.dart';
9 import 'package:analyzer/src/generated/ast.dart'; 9 import 'package:analyzer/src/generated/ast.dart';
10 import 'package:analyzer/src/generated/element.dart';
11 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; 10 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext;
12 import 'package:logging/logging.dart'; 11 import 'package:logging/logging.dart';
13 import 'package:source_span/source_span.dart'; 12 import 'package:source_span/source_span.dart';
14 import 'package:unittest/unittest.dart'; 13 import 'package:unittest/unittest.dart';
15 14
16 import 'package:dev_compiler/src/checker/dart_sdk.dart'
17 show mockSdkSources, dartSdkDirectory;
18 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver;
19 import 'package:dev_compiler/src/utils.dart';
20 import 'package:dev_compiler/src/info.dart';
21 import 'package:dev_compiler/src/options.dart';
22 import 'package:dev_compiler/src/report.dart';
23 import 'package:dev_compiler/config.dart'; 15 import 'package:dev_compiler/config.dart';
24 import 'package:dev_compiler/devc.dart' show Compiler; 16 import 'package:dev_compiler/devc.dart' show Compiler;
25 17
18 import 'analysis_context.dart';
26 import 'dependency_graph.dart' show runtimeFilesForServerMode; 19 import 'dependency_graph.dart' show runtimeFilesForServerMode;
20 import 'info.dart';
21 import 'options.dart';
22 import 'report.dart';
23 import 'utils.dart';
27 24
28 /// Run the checker on a program with files contents as indicated in 25 /// Run the checker on a program with files contents as indicated in
29 /// [testFiles]. 26 /// [testFiles].
30 /// 27 ///
31 /// This function makes several assumptions to make it easier to describe error 28 /// This function makes several assumptions to make it easier to describe error
32 /// expectations: 29 /// expectations:
33 /// 30 ///
34 /// * a file named `/main.dart` exists in [testFiles]. 31 /// * a file named `/main.dart` exists in [testFiles].
35 /// * all expected failures are listed in the source code using comments 32 /// * all expected failures are listed in the source code using comments
36 /// immediately in front of the AST node that should contain the error. 33 /// immediately in front of the AST node that should contain the error.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 relaxedCasts: relaxedCasts, 67 relaxedCasts: relaxedCasts,
71 inferDownwards: inferDownwards, 68 inferDownwards: inferDownwards,
72 inferFromOverrides: inferFromOverrides, 69 inferFromOverrides: inferFromOverrides,
73 inferTransitively: inferTransitively, 70 inferTransitively: inferTransitively,
74 nonnullableTypes: nonnullableTypes, 71 nonnullableTypes: nonnullableTypes,
75 wrapClosures: wrapClosures, 72 wrapClosures: wrapClosures,
76 useMockSdk: sdkDir == null, 73 useMockSdk: sdkDir == null,
77 dartSdkPath: sdkDir, 74 dartSdkPath: sdkDir,
78 runtimeDir: '/dev_compiler_runtime/', 75 runtimeDir: '/dev_compiler_runtime/',
79 entryPointFile: '/main.dart'); 76 entryPointFile: '/main.dart');
80 var resolver = sdkDir == null 77
81 ? new TypeResolver.fromMock(mockSdkSources, options, 78 var context = createAnalysisContext(options, fileResolvers: [uriResolver]);
82 otherResolvers: [uriResolver])
83 : new TypeResolver.fromDir(sdkDir, options,
84 otherResolvers: [uriResolver]);
85 var context = resolver.context;
86 79
87 // Run the checker on /main.dart. 80 // Run the checker on /main.dart.
88 var mainFile = new Uri.file('/main.dart'); 81 var mainFile = new Uri.file('/main.dart');
89 TestReporter testReporter; 82 TestReporter testReporter;
90 CheckerReporter reporter; 83 CheckerReporter reporter;
91 if (createReporter == null) { 84 if (createReporter == null) {
92 reporter = testReporter = new TestReporter(context); 85 reporter = testReporter = new TestReporter(context);
93 } else { 86 } else {
94 reporter = createReporter(context); 87 reporter = createReporter(context);
95 } 88 }
96 var results = 89 var results =
97 new Compiler(options, resolver: resolver, reporter: reporter).run(); 90 new Compiler(options, context: context, reporter: reporter).run();
98 91
99 // Extract expectations from the comments in the test files. 92 // Extract expectations from the comments in the test files.
100 var expectedErrors = <AstNode, List<_ErrorExpectation>>{}; 93 var expectedErrors = <AstNode, List<_ErrorExpectation>>{};
101 var visitor = new _ErrorMarkerVisitor(expectedErrors); 94 var visitor = new _ErrorMarkerVisitor(expectedErrors);
102 var initialLibrary = 95 var initialLibrary =
103 resolver.context.getLibraryElement(uriResolver.resolveAbsolute(mainFile)); 96 context.getLibraryElement(uriResolver.resolveAbsolute(mainFile));
104 for (var lib in reachableLibraries(initialLibrary)) { 97 for (var lib in reachableLibraries(initialLibrary)) {
105 for (var unit in lib.units) { 98 for (var unit in lib.units) {
106 unit.unit.accept(visitor); 99 unit.unit.accept(visitor);
107 } 100 }
108 } 101 }
109 102
110 if (testReporter == null) return results; 103 if (testReporter == null) return results;
111 104
112 var total = expectedErrors.values.fold(0, (p, l) => p + l.length); 105 var total = expectedErrors.values.fold(0, (p, l) => p + l.length);
113 // Check that all errors we emit are included in the expected map. 106 // Check that all errors we emit are included in the expected map.
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 expect(tokens[1], "should", reason: 'invalid error descriptor'); 300 expect(tokens[1], "should", reason: 'invalid error descriptor');
308 expect(tokens[2], "be", reason: 'invalid error descriptor'); 301 expect(tokens[2], "be", reason: 'invalid error descriptor');
309 if (tokens[0] == "pass") return null; 302 if (tokens[0] == "pass") return null;
310 // TODO(leafp) For now, we just use whatever the current expectation is, 303 // TODO(leafp) For now, we just use whatever the current expectation is,
311 // eventually we could do more automated reporting here. 304 // eventually we could do more automated reporting here.
312 return _parse(tokens[0]); 305 return _parse(tokens[0]);
313 } 306 }
314 307
315 String toString() => '$level $type'; 308 String toString() => '$level $type';
316 } 309 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698