| OLD | NEW |
| 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 // TODO(jmesserly): this file needs to be refactored, it's a port from | 5 // TODO(jmesserly): this file needs to be refactored, it's a port from |
| 6 // package:dev_compiler's tests | 6 // package:dev_compiler's tests |
| 7 library test.src.task.strong.strong_test_helper; | 7 library analyzer.test.src.task.strong.strong_test_helper; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; |
| 11 import 'package:analyzer/dart/element/element.dart'; |
| 12 import 'package:analyzer/error/error.dart'; |
| 13 import 'package:analyzer/error/listener.dart'; |
| 9 import 'package:analyzer/file_system/file_system.dart'; | 14 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/file_system/memory_file_system.dart'; | 15 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 11 import 'package:analyzer/src/context/context.dart' show SdkAnalysisContext; | 16 import 'package:analyzer/source/error_processor.dart'; |
| 12 import 'package:analyzer/src/generated/ast.dart'; | 17 import 'package:analyzer/src/dart/ast/token.dart'; |
| 13 import 'package:analyzer/src/generated/element.dart'; | 18 import 'package:analyzer/src/error/codes.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart' hide SdkAnalysisContext; | 19 import 'package:analyzer/src/generated/engine.dart'; |
| 15 import 'package:analyzer/src/generated/error.dart'; | |
| 16 import 'package:analyzer/src/generated/sdk.dart'; | |
| 17 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 18 import 'package:analyzer/src/task/strong/checker.dart'; | 21 import 'package:source_span/source_span.dart'; |
| 19 import 'package:analyzer/src/task/strong/rules.dart'; | |
| 20 import 'package:logging/logging.dart'; // TODO(jmesserly): remove | |
| 21 import 'package:source_span/source_span.dart'; // TODO(jmesserly): remove | |
| 22 import 'package:unittest/unittest.dart'; | 22 import 'package:unittest/unittest.dart'; |
| 23 | 23 |
| 24 /// Run the checker on a program with files contents as indicated in | 24 import '../../context/mock_sdk.dart'; |
| 25 /// [testFiles]. | 25 |
| 26 MemoryResourceProvider files; |
| 27 bool _checkCalled; |
| 28 |
| 29 /// Adds a file to check. The file should contain: |
| 26 /// | 30 /// |
| 27 /// This function makes several assumptions to make it easier to describe error | 31 /// * all expected failures are listed in the source code using comments |
| 28 /// expectations: | 32 /// immediately in front of the AST node that should contain the error. |
| 29 /// | 33 /// |
| 30 /// * a file named `/main.dart` exists in [testFiles]. | 34 /// * errors are formatted as a token `severity:ErrorCode`, where |
| 31 /// * all expected failures are listed in the source code using comments | 35 /// `severity` is the ErrorSeverity the error would be reported at, and |
| 32 /// immediately in front of the AST node that should contain the error. | 36 /// `ErrorCode` is the error code's name. |
| 33 /// * errors are formatted as a token `level:Type`, where `level` is the | |
| 34 /// logging level were the error would be reported at, and `Type` is the | |
| 35 /// concrete subclass of [StaticInfo] that denotes the error. | |
| 36 /// | 37 /// |
| 37 /// For example, to check that an assignment produces a warning about a boxing | 38 /// For example to check that an assignment produces a type error, you can |
| 38 /// conversion, you can describe the test as follows: | 39 /// create a file like: |
| 39 /// | 40 /// |
| 40 /// testChecker({ | 41 /// addFile(''' |
| 41 /// '/main.dart': ''' | 42 /// String x = /*error:STATIC_TYPE_ERROR*/3; |
| 42 /// testMethod() { | 43 /// '''); |
| 43 /// dynamic x = /*warning:Box*/3; | 44 /// check(); |
| 44 /// } | |
| 45 /// ''' | |
| 46 /// }); | |
| 47 /// | 45 /// |
| 48 void testChecker(String name, Map<String, String> testFiles) { | 46 /// For a single file, you may also use [checkFile]. |
| 49 test(name, () { | 47 void addFile(String content, {String name: '/main.dart'}) { |
| 50 expect(testFiles.containsKey('/main.dart'), isTrue, | 48 name = name.replaceFirst('^package:', '/packages/'); |
| 51 reason: '`/main.dart` is missing in testFiles'); | 49 files.newFile(name, content); |
| 50 } |
| 52 | 51 |
| 53 var provider = new MemoryResourceProvider(); | 52 /// Run the checker on a program, staring from '/main.dart', and verifies that |
| 54 testFiles.forEach((key, value) { | 53 /// errors/warnings/hints match the expected value. |
| 55 var scheme = 'package:'; | 54 /// |
| 56 if (key.startsWith(scheme)) { | 55 /// See [addFile] for more information about how to encode expectations in |
| 57 key = '/packages/${key.substring(scheme.length)}'; | 56 /// the file text. |
| 58 } | 57 /// |
| 59 provider.newFile(key, value); | 58 /// Returns the main resolved library. This can be used for further checks. |
| 60 }); | 59 CompilationUnit check( |
| 61 var uriResolver = new TestUriResolver(provider); | 60 {bool implicitCasts: true, |
| 62 // Enable task model strong mode | 61 bool implicitDynamic: true, |
| 63 AnalysisEngine.instance.useTaskModel = true; | 62 List<String> nonnullableTypes: AnalysisOptionsImpl.NONNULLABLE_TYPES}) { |
| 64 var context = AnalysisEngine.instance.createAnalysisContext(); | 63 _checkCalled = true; |
| 65 context.analysisOptions.strongMode = true; | |
| 66 | 64 |
| 67 context.sourceFactory = new SourceFactory([ | 65 expect(files.getFile('/main.dart').exists, true, |
| 68 new MockDartSdk(mockSdkSources, reportMissing: true).resolver, | 66 reason: '`/main.dart` is missing'); |
| 69 uriResolver | |
| 70 ]); | |
| 71 | 67 |
| 72 // Run the checker on /main.dart. | 68 var uriResolver = new _TestUriResolver(files); |
| 73 Source mainSource = uriResolver.resolveAbsolute(new Uri.file('/main.dart')); | 69 // Enable task model strong mode |
| 74 var initialLibrary = | 70 var context = AnalysisEngine.instance.createAnalysisContext(); |
| 75 context.resolveCompilationUnit2(mainSource, mainSource); | 71 AnalysisOptionsImpl options = context.analysisOptions as AnalysisOptionsImpl; |
| 72 options.strongMode = true; |
| 73 options.strongModeHints = true; |
| 74 options.implicitCasts = implicitCasts; |
| 75 options.implicitDynamic = implicitDynamic; |
| 76 options.nonnullableTypes = nonnullableTypes; |
| 77 var mockSdk = new MockSdk(); |
| 78 (mockSdk.context.analysisOptions as AnalysisOptionsImpl).strongMode = true; |
| 79 context.sourceFactory = |
| 80 new SourceFactory([new DartUriResolver(mockSdk), uriResolver]); |
| 76 | 81 |
| 77 var collector = new _ErrorCollector(); | 82 // Run the checker on /main.dart. |
| 78 var checker = new CodeChecker( | 83 Source mainSource = uriResolver.resolveAbsolute(new Uri.file('/main.dart')); |
| 79 new TypeRules(context.typeProvider), collector, | 84 var initialLibrary = context.resolveCompilationUnit2(mainSource, mainSource); |
| 80 hints: true); | |
| 81 | 85 |
| 82 // Extract expectations from the comments in the test files, and | 86 var collector = new _ErrorCollector(context); |
| 83 // check that all errors we emit are included in the expected map. | |
| 84 var allLibraries = reachableLibraries(initialLibrary.element.library); | |
| 85 for (var lib in allLibraries) { | |
| 86 for (var unit in lib.units) { | |
| 87 var errors = <AnalysisError>[]; | |
| 88 collector.errors = errors; | |
| 89 | 87 |
| 90 var source = unit.source; | 88 // Extract expectations from the comments in the test files, and |
| 91 if (source.uri.scheme == 'dart') continue; | 89 // check that all errors we emit are included in the expected map. |
| 90 var allLibraries = _reachableLibraries(initialLibrary.element.library); |
| 91 for (var lib in allLibraries) { |
| 92 for (var unit in lib.units) { |
| 93 var errors = <AnalysisError>[]; |
| 94 collector.errors = errors; |
| 92 | 95 |
| 93 var librarySource = context.getLibrariesContaining(source).single; | 96 var source = unit.source; |
| 94 var resolved = context.resolveCompilationUnit2(source, librarySource); | 97 if (source.uri.scheme == 'dart') continue; |
| 95 checker.visitCompilationUnit(resolved); | |
| 96 | 98 |
| 97 new _ExpectedErrorVisitor(errors).validate(resolved); | 99 var librarySource = context.getLibrariesContaining(source).single; |
| 98 } | 100 var resolved = context.resolveCompilationUnit2(source, librarySource); |
| 101 |
| 102 errors.addAll(context.computeErrors(source).where((e) => |
| 103 // TODO(jmesserly): these are usually intentional dynamic calls. |
| 104 e.errorCode.name != 'UNDEFINED_METHOD' && |
| 105 // We don't care about any of these: |
| 106 e.errorCode != HintCode.UNUSED_ELEMENT && |
| 107 e.errorCode != HintCode.UNUSED_FIELD && |
| 108 e.errorCode != HintCode.UNUSED_IMPORT && |
| 109 e.errorCode != HintCode.UNUSED_LOCAL_VARIABLE && |
| 110 e.errorCode != TodoCode.TODO)); |
| 111 _expectErrors(context, resolved, errors); |
| 99 } | 112 } |
| 113 } |
| 114 |
| 115 return initialLibrary; |
| 116 } |
| 117 |
| 118 /// Adds a file using [addFile] and calls [check]. |
| 119 /// |
| 120 /// Also returns the resolved compilation unit. |
| 121 CompilationUnit checkFile(String content) { |
| 122 addFile(content); |
| 123 return check(); |
| 124 } |
| 125 |
| 126 void initStrongModeTests() { |
| 127 setUp(() { |
| 128 AnalysisEngine.instance.processRequiredPlugins(); |
| 129 files = new MemoryResourceProvider(); |
| 130 _checkCalled = false; |
| 131 }); |
| 132 |
| 133 tearDown(() { |
| 134 // This is a sanity check, in case only addFile is called. |
| 135 expect(_checkCalled, true, reason: 'must call check() method in test case'); |
| 136 files = null; |
| 100 }); | 137 }); |
| 101 } | 138 } |
| 102 | 139 |
| 103 class _ErrorCollector implements AnalysisErrorListener { | 140 SourceSpanWithContext _createSpanHelper( |
| 104 List<AnalysisError> errors; | 141 LineInfo lineInfo, int start, Source source, String content, |
| 105 final bool hints; | 142 {int end}) { |
| 106 _ErrorCollector({this.hints: true}); | 143 var startLoc = _locationForOffset(lineInfo, source.uri, start); |
| 107 | 144 var endLoc = _locationForOffset(lineInfo, source.uri, end ?? start); |
| 108 void onError(AnalysisError error) { | |
| 109 // Unless DDC hints are requested, filter them out. | |
| 110 var HINT = ErrorSeverity.INFO.ordinal; | |
| 111 if (hints || error.errorCode.errorSeverity.ordinal > HINT) { | |
| 112 errors.add(error); | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 class TestUriResolver extends ResourceUriResolver { | |
| 118 final MemoryResourceProvider provider; | |
| 119 TestUriResolver(provider) | |
| 120 : provider = provider, | |
| 121 super(provider); | |
| 122 | |
| 123 @override | |
| 124 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | |
| 125 if (uri.scheme == 'package') { | |
| 126 return (provider.getResource('/packages/' + uri.path) as File) | |
| 127 .createSource(uri); | |
| 128 } | |
| 129 return super.resolveAbsolute(uri, actualUri); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 class _ExpectedErrorVisitor extends UnifyingAstVisitor { | |
| 134 final Set<AnalysisError> _actualErrors; | |
| 135 CompilationUnit _unit; | |
| 136 String _unitSourceCode; | |
| 137 | |
| 138 _ExpectedErrorVisitor(List<AnalysisError> actualErrors) | |
| 139 : _actualErrors = new Set.from(actualErrors); | |
| 140 | |
| 141 validate(CompilationUnit unit) { | |
| 142 _unit = unit; | |
| 143 // This reads the file. Only safe because tests use MemoryFileSystem. | |
| 144 _unitSourceCode = unit.element.source.contents.data; | |
| 145 | |
| 146 // Visit the compilation unit. | |
| 147 unit.accept(this); | |
| 148 | |
| 149 if (_actualErrors.isNotEmpty) { | |
| 150 var actualMsgs = _actualErrors.map(_formatActualError).join('\n'); | |
| 151 fail('Unexpected errors reported by checker:\n\n$actualMsgs'); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 visitNode(AstNode node) { | |
| 156 var token = node.beginToken; | |
| 157 var comment = token.precedingComments; | |
| 158 // Use error marker found in an immediately preceding comment, | |
| 159 // and attach it to the outermost expression that starts at that token. | |
| 160 if (comment != null) { | |
| 161 while (comment.next != null) { | |
| 162 comment = comment.next; | |
| 163 } | |
| 164 if (comment.end == token.offset && node.parent.beginToken != token) { | |
| 165 var commentText = '$comment'; | |
| 166 var start = commentText.lastIndexOf('/*'); | |
| 167 var end = commentText.lastIndexOf('*/'); | |
| 168 if (start != -1 && end != -1) { | |
| 169 expect(start, lessThan(end)); | |
| 170 var errors = commentText.substring(start + 2, end).split(','); | |
| 171 var expectations = | |
| 172 errors.map(_ErrorExpectation.parse).where((x) => x != null); | |
| 173 | |
| 174 for (var e in expectations) _expectError(node, e); | |
| 175 } | |
| 176 } | |
| 177 } | |
| 178 return super.visitNode(node); | |
| 179 } | |
| 180 | |
| 181 void _expectError(AstNode node, _ErrorExpectation expected) { | |
| 182 // See if we can find the expected error in our actual errors | |
| 183 for (var actual in _actualErrors) { | |
| 184 if (actual.offset == node.offset && actual.length == node.length) { | |
| 185 var actualMsg = _formatActualError(actual); | |
| 186 expect(_actualErrorLevel(actual), expected.level, | |
| 187 reason: 'expected different error code at:\n\n$actualMsg'); | |
| 188 expect(errorCodeName(actual.errorCode), expected.typeName, | |
| 189 reason: 'expected different error type at:\n\n$actualMsg'); | |
| 190 | |
| 191 // We found it. Stop the search. | |
| 192 _actualErrors.remove(actual); | |
| 193 return; | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 var span = _createSpan(node.offset, node.length); | |
| 198 var levelName = expected.level.name.toLowerCase(); | |
| 199 var msg = span.message(expected.typeName, color: colorOf(levelName)); | |
| 200 fail('expected error was not reported at:\n\n$levelName: $msg'); | |
| 201 } | |
| 202 | |
| 203 Level _actualErrorLevel(AnalysisError actual) { | |
| 204 return const <ErrorSeverity, Level>{ | |
| 205 ErrorSeverity.ERROR: Level.SEVERE, | |
| 206 ErrorSeverity.WARNING: Level.WARNING, | |
| 207 ErrorSeverity.INFO: Level.INFO | |
| 208 }[actual.errorCode.errorSeverity]; | |
| 209 } | |
| 210 | |
| 211 String _formatActualError(AnalysisError actual) { | |
| 212 var span = _createSpan(actual.offset, actual.length); | |
| 213 var levelName = _actualErrorLevel(actual).name.toLowerCase(); | |
| 214 var msg = span.message(actual.message, color: colorOf(levelName)); | |
| 215 return '$levelName: [${errorCodeName(actual.errorCode)}] $msg'; | |
| 216 } | |
| 217 | |
| 218 SourceSpan _createSpan(int offset, int len) { | |
| 219 return createSpanHelper(_unit.lineInfo, offset, offset + len, | |
| 220 _unit.element.source, _unitSourceCode); | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 SourceLocation locationForOffset(LineInfo lineInfo, Uri uri, int offset) { | |
| 225 var loc = lineInfo.getLocation(offset); | |
| 226 return new SourceLocation(offset, | |
| 227 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); | |
| 228 } | |
| 229 | |
| 230 SourceSpanWithContext createSpanHelper( | |
| 231 LineInfo lineInfo, int start, int end, Source source, String content) { | |
| 232 var startLoc = locationForOffset(lineInfo, source.uri, start); | |
| 233 var endLoc = locationForOffset(lineInfo, source.uri, end); | |
| 234 | 145 |
| 235 var lineStart = startLoc.offset - startLoc.column; | 146 var lineStart = startLoc.offset - startLoc.column; |
| 236 // Find the end of the line. This is not exposed directly on LineInfo, but | 147 // Find the end of the line. This is not exposed directly on LineInfo, but |
| 237 // we can find it pretty easily. | 148 // we can find it pretty easily. |
| 238 // TODO(jmesserly): for now we do the simple linear scan. Ideally we can get | 149 // TODO(jmesserly): for now we do the simple linear scan. Ideally we can get |
| 239 // some help from the LineInfo API. | 150 // some help from the LineInfo API. |
| 240 int lineEnd = endLoc.offset; | 151 int lineEnd = endLoc.offset; |
| 241 int lineNum = lineInfo.getLocation(lineEnd).lineNumber; | 152 int lineNum = lineInfo.getLocation(lineEnd).lineNumber; |
| 242 while (lineEnd < content.length && | 153 while (lineEnd < content.length && |
| 243 lineInfo.getLocation(++lineEnd).lineNumber == lineNum); | 154 lineInfo.getLocation(++lineEnd).lineNumber == lineNum); |
| 244 | 155 |
| 156 if (end == null) { |
| 157 end = lineEnd; |
| 158 endLoc = _locationForOffset(lineInfo, source.uri, lineEnd); |
| 159 } |
| 160 |
| 245 var text = content.substring(start, end); | 161 var text = content.substring(start, end); |
| 246 var lineText = content.substring(lineStart, lineEnd); | 162 var lineText = content.substring(lineStart, lineEnd); |
| 247 return new SourceSpanWithContext(startLoc, endLoc, text, lineText); | 163 return new SourceSpanWithContext(startLoc, endLoc, text, lineText); |
| 248 } | 164 } |
| 249 | 165 |
| 250 /// Describes an expected message that should be produced by the checker. | 166 String _errorCodeName(ErrorCode errorCode) { |
| 251 class _ErrorExpectation { | 167 var name = errorCode.name; |
| 252 final Level level; | 168 final prefix = 'STRONG_MODE_'; |
| 253 final String typeName; | 169 if (name.startsWith(prefix)) { |
| 254 _ErrorExpectation(this.level, this.typeName); | 170 return name.substring(prefix.length); |
| 255 | 171 } else { |
| 256 static _ErrorExpectation _parse(String descriptor) { | 172 return name; |
| 257 var tokens = descriptor.split(':'); | |
| 258 expect(tokens.length, 2, reason: 'invalid error descriptor'); | |
| 259 var name = tokens[0].toUpperCase(); | |
| 260 var typeName = tokens[1]; | |
| 261 | |
| 262 var level = | |
| 263 Level.LEVELS.firstWhere((l) => l.name == name, orElse: () => null); | |
| 264 expect(level, isNotNull, | |
| 265 reason: 'invalid level in error descriptor: `${tokens[0]}`'); | |
| 266 expect(typeName, isNotNull, | |
| 267 reason: 'invalid type in error descriptor: ${tokens[1]}'); | |
| 268 return new _ErrorExpectation(level, typeName); | |
| 269 } | |
| 270 | |
| 271 static _ErrorExpectation parse(String descriptor) { | |
| 272 descriptor = descriptor.trim(); | |
| 273 var tokens = descriptor.split(' '); | |
| 274 if (tokens.length == 1) return _parse(tokens[0]); | |
| 275 expect(tokens.length, 4, reason: 'invalid error descriptor'); | |
| 276 expect(tokens[1], "should", reason: 'invalid error descriptor'); | |
| 277 expect(tokens[2], "be", reason: 'invalid error descriptor'); | |
| 278 if (tokens[0] == "pass") return null; | |
| 279 // TODO(leafp) For now, we just use whatever the current expectation is, | |
| 280 // eventually we could do more automated reporting here. | |
| 281 return _parse(tokens[0]); | |
| 282 } | |
| 283 | |
| 284 String toString() => '$level $typeName'; | |
| 285 } | |
| 286 | |
| 287 /// Dart SDK which contains a mock implementation of the SDK libraries. May be | |
| 288 /// used to speed up execution when most of the core libraries is not needed. | |
| 289 class MockDartSdk implements DartSdk { | |
| 290 final Map<Uri, _MockSdkSource> _sources = {}; | |
| 291 final bool reportMissing; | |
| 292 final Map<String, SdkLibrary> _libs = {}; | |
| 293 final String sdkVersion = '0'; | |
| 294 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | |
| 295 final AnalysisContext context = new SdkAnalysisContext(); | |
| 296 DartUriResolver _resolver; | |
| 297 DartUriResolver get resolver => _resolver; | |
| 298 | |
| 299 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { | |
| 300 sources.forEach((uriString, contents) { | |
| 301 var uri = Uri.parse(uriString); | |
| 302 _sources[uri] = new _MockSdkSource(uri, contents); | |
| 303 _libs[uriString] = new SdkLibraryImpl(uri.path) | |
| 304 ..setDart2JsLibrary() | |
| 305 ..setVmLibrary(); | |
| 306 }); | |
| 307 _resolver = new DartUriResolver(this); | |
| 308 context.sourceFactory = new SourceFactory([_resolver]); | |
| 309 } | |
| 310 | |
| 311 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); | |
| 312 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; | |
| 313 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); | |
| 314 | |
| 315 Source fromEncoding(UriKind kind, Uri uri) { | |
| 316 if (kind != UriKind.DART_URI) { | |
| 317 throw new UnsupportedError('expected dart: uri kind, got $kind.'); | |
| 318 } | |
| 319 return _getSource(uri); | |
| 320 } | |
| 321 | |
| 322 Source _getSource(Uri uri) { | |
| 323 var src = _sources[uri]; | |
| 324 if (src == null) { | |
| 325 if (reportMissing) print('warning: missing mock for $uri.'); | |
| 326 _sources[uri] = | |
| 327 src = new _MockSdkSource(uri, 'library dart.${uri.path};'); | |
| 328 } | |
| 329 return src; | |
| 330 } | |
| 331 | |
| 332 @override | |
| 333 Source fromFileUri(Uri uri) { | |
| 334 throw new UnsupportedError('MockDartSdk.fromFileUri'); | |
| 335 } | 173 } |
| 336 } | 174 } |
| 337 | 175 |
| 338 class _MockSdkSource implements Source { | 176 ErrorSeverity _errorSeverity(AnalysisContext context, AnalysisError error) { |
| 339 /// Absolute URI which this source can be imported from. | 177 // Attempt to process severity in a similar way to analyzer_cli and server. |
| 340 final Uri uri; | 178 return ErrorProcessor.getProcessor(context, error)?.severity ?? |
| 341 final String _contents; | 179 error.errorCode.errorSeverity; |
| 342 | |
| 343 _MockSdkSource(this.uri, this._contents); | |
| 344 | |
| 345 bool exists() => true; | |
| 346 | |
| 347 int get hashCode => uri.hashCode; | |
| 348 | |
| 349 final int modificationStamp = 1; | |
| 350 | |
| 351 TimestampedData<String> get contents => | |
| 352 new TimestampedData(modificationStamp, _contents); | |
| 353 | |
| 354 String get encoding => "${uriKind.encoding}$uri"; | |
| 355 | |
| 356 Source get source => this; | |
| 357 | |
| 358 String get fullName => shortName; | |
| 359 | |
| 360 String get shortName => uri.path; | |
| 361 | |
| 362 UriKind get uriKind => UriKind.DART_URI; | |
| 363 | |
| 364 bool get isInSystemLibrary => true; | |
| 365 | |
| 366 Source resolveRelative(Uri relativeUri) => | |
| 367 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
| 368 | |
| 369 Uri resolveRelativeUri(Uri relativeUri) => | |
| 370 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
| 371 } | 180 } |
| 372 | 181 |
| 373 /// Sample mock SDK sources. | 182 void _expectErrors(AnalysisContext context, CompilationUnit unit, |
| 374 final Map<String, String> mockSdkSources = { | 183 List<AnalysisError> actualErrors) { |
| 375 // The list of types below is derived from: | 184 var expectedErrors = _findExpectedErrors(unit.beginToken); |
| 376 // * types we use via our smoke queries, including HtmlElement and | |
| 377 // types from `_typeHandlers` (deserialize.dart) | |
| 378 // * types that are used internally by the resolver (see | |
| 379 // _initializeFrom in resolver.dart). | |
| 380 'dart:core': ''' | |
| 381 library dart.core; | |
| 382 | 185 |
| 383 void print(Object o) {} | 186 // Sort both lists: by offset, then level, then name. |
| 187 actualErrors.sort((x, y) { |
| 188 int delta = x.offset.compareTo(y.offset); |
| 189 if (delta != 0) return delta; |
| 384 | 190 |
| 385 class Object { | 191 delta = _errorSeverity(context, x).compareTo(_errorSeverity(context, y)); |
| 386 int get hashCode {} | 192 if (delta != 0) return delta; |
| 387 Type get runtimeType {} | 193 |
| 388 String toString(){} | 194 return _errorCodeName(x.errorCode).compareTo(_errorCodeName(y.errorCode)); |
| 389 bool ==(other){} | 195 }); |
| 196 expectedErrors.sort((x, y) { |
| 197 int delta = x.offset.compareTo(y.offset); |
| 198 if (delta != 0) return delta; |
| 199 |
| 200 delta = x.severity.compareTo(y.severity); |
| 201 if (delta != 0) return delta; |
| 202 |
| 203 return x.typeName.compareTo(y.typeName); |
| 204 }); |
| 205 |
| 206 // Categorize the differences, if any. |
| 207 var unreported = <_ErrorExpectation>[]; |
| 208 var different = <_ErrorExpectation, AnalysisError>{}; |
| 209 |
| 210 for (var expected in expectedErrors) { |
| 211 AnalysisError actual = expected._removeMatchingActual(actualErrors); |
| 212 if (actual != null) { |
| 213 if (_errorSeverity(context, actual) != expected.severity || |
| 214 _errorCodeName(actual.errorCode) != expected.typeName) { |
| 215 different[expected] = actual; |
| 216 } |
| 217 } else { |
| 218 unreported.add(expected); |
| 219 } |
| 220 } |
| 221 |
| 222 // Whatever is left was an unexpected error. |
| 223 List<AnalysisError> unexpected = actualErrors; |
| 224 |
| 225 if (unreported.isNotEmpty || unexpected.isNotEmpty || different.isNotEmpty) { |
| 226 _reportFailure(context, unit, unreported, unexpected, different); |
| 227 } |
| 228 } |
| 229 |
| 230 List<_ErrorExpectation> _findExpectedErrors(Token beginToken) { |
| 231 var expectedErrors = <_ErrorExpectation>[]; |
| 232 |
| 233 // Collect expectations like "error:STATIC_TYPE_ERROR" from comment tokens. |
| 234 for (Token t = beginToken; t.type != TokenType.EOF; t = t.next) { |
| 235 for (CommentToken c = t.precedingComments; c != null; c = c.next) { |
| 236 if (c.type == TokenType.MULTI_LINE_COMMENT) { |
| 237 String value = c.lexeme.substring(2, c.lexeme.length - 2); |
| 238 if (value.contains(':')) { |
| 239 int offset = t.offset; |
| 240 Token previous = t.previous; |
| 241 while (previous != null && previous.offset > c.offset) { |
| 242 offset = previous.offset; |
| 243 previous = previous.previous; |
| 244 } |
| 245 for (var expectCode in value.split(',')) { |
| 246 var expected = _ErrorExpectation.parse(offset, expectCode); |
| 247 if (expected != null) { |
| 248 expectedErrors.add(expected); |
| 249 } |
| 250 } |
| 390 } | 251 } |
| 391 class Function {} | 252 } |
| 392 class StackTrace {} | 253 } |
| 393 class Symbol {} | 254 } |
| 394 class Type {} | 255 return expectedErrors; |
| 256 } |
| 395 | 257 |
| 396 class String { | 258 SourceLocation _locationForOffset(LineInfo lineInfo, Uri uri, int offset) { |
| 397 String operator +(String other) {} | 259 var loc = lineInfo.getLocation(offset); |
| 398 } | 260 return new SourceLocation(offset, |
| 399 class bool {} | 261 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); |
| 400 class num { | 262 } |
| 401 num operator +(num other) {} | |
| 402 } | |
| 403 class int extends num { | |
| 404 bool operator<(num other) {} | |
| 405 int operator-() {} | |
| 406 } | |
| 407 class double extends num {} | |
| 408 class DateTime {} | |
| 409 class Null {} | |
| 410 | |
| 411 class Deprecated { | |
| 412 final String expires; | |
| 413 const Deprecated(this.expires); | |
| 414 } | |
| 415 const Object deprecated = const Deprecated("next release"); | |
| 416 class _Override { const _Override(); } | |
| 417 const Object override = const _Override(); | |
| 418 class _Proxy { const _Proxy(); } | |
| 419 const Object proxy = const _Proxy(); | |
| 420 | |
| 421 class Iterable<E> { | |
| 422 fold(initialValue, combine(previousValue, E element)) {} | |
| 423 Iterable map(f(E element)) {} | |
| 424 } | |
| 425 class List<E> implements Iterable<E> { | |
| 426 List([int length]); | |
| 427 List.filled(int length, E fill); | |
| 428 } | |
| 429 class Map<K, V> { | |
| 430 Iterable<K> get keys {} | |
| 431 } | |
| 432 ''', | |
| 433 'dart:async': ''' | |
| 434 class Future<T> { | |
| 435 Future(computation()) {} | |
| 436 Future.value(T t) {} | |
| 437 Future then(onValue(T value)) {} | |
| 438 static Future<List> wait(Iterable<Future> futures) {} | |
| 439 } | |
| 440 class Stream<T> {} | |
| 441 ''', | |
| 442 'dart:html': ''' | |
| 443 library dart.html; | |
| 444 class HtmlElement {} | |
| 445 ''', | |
| 446 'dart:math': ''' | |
| 447 library dart.math; | |
| 448 class Random { | |
| 449 bool nextBool() {} | |
| 450 } | |
| 451 num min(num x, num y) {} | |
| 452 num max(num x, num y) {} | |
| 453 ''', | |
| 454 }; | |
| 455 | 263 |
| 456 /// Returns all libraries transitively imported or exported from [start]. | 264 /// Returns all libraries transitively imported or exported from [start]. |
| 457 List<LibraryElement> reachableLibraries(LibraryElement start) { | 265 List<LibraryElement> _reachableLibraries(LibraryElement start) { |
| 458 var results = <LibraryElement>[]; | 266 var results = <LibraryElement>[]; |
| 459 var seen = new Set(); | 267 var seen = new Set(); |
| 460 void find(LibraryElement lib) { | 268 void find(LibraryElement lib) { |
| 461 if (seen.contains(lib)) return; | 269 if (seen.contains(lib)) return; |
| 462 seen.add(lib); | 270 seen.add(lib); |
| 463 results.add(lib); | 271 results.add(lib); |
| 464 lib.importedLibraries.forEach(find); | 272 lib.importedLibraries.forEach(find); |
| 465 lib.exportedLibraries.forEach(find); | 273 lib.exportedLibraries.forEach(find); |
| 466 } | 274 } |
| 275 |
| 467 find(start); | 276 find(start); |
| 468 return results; | 277 return results; |
| 469 } | 278 } |
| 470 | 279 |
| 471 String errorCodeName(ErrorCode errorCode) { | 280 void _reportFailure( |
| 472 var name = errorCode.name; | 281 AnalysisContext context, |
| 473 final prefix = 'dev_compiler.'; | 282 CompilationUnit unit, |
| 474 if (name.startsWith(prefix)) { | 283 List<_ErrorExpectation> unreported, |
| 475 return name.substring(prefix.length); | 284 List<AnalysisError> unexpected, |
| 476 } else { | 285 Map<_ErrorExpectation, AnalysisError> different) { |
| 477 // TODO(jmesserly): this is for backwards compat, but not sure it's very | 286 // Get the source code. This reads the data again, but it's safe because |
| 478 // useful to log this. | 287 // all tests use memory file system. |
| 479 return 'AnalyzerMessage'; | 288 var sourceCode = unit.element.source.contents.data; |
| 289 |
| 290 String formatActualError(AnalysisError error) { |
| 291 int offset = error.offset; |
| 292 int length = error.length; |
| 293 var span = _createSpanHelper( |
| 294 unit.lineInfo, offset, unit.element.source, sourceCode, |
| 295 end: offset + length); |
| 296 var levelName = _errorSeverity(context, error).displayName; |
| 297 return '@$offset $levelName:${_errorCodeName(error.errorCode)}\n' + |
| 298 span.message(error.message); |
| 299 } |
| 300 |
| 301 String formatExpectedError(_ErrorExpectation error) { |
| 302 int offset = error.offset; |
| 303 var span = _createSpanHelper( |
| 304 unit.lineInfo, offset, unit.element.source, sourceCode); |
| 305 var severity = error.severity.displayName; |
| 306 return '@$offset $severity:${error.typeName}\n' + span.message(''); |
| 307 } |
| 308 |
| 309 var message = new StringBuffer(); |
| 310 if (unreported.isNotEmpty) { |
| 311 message.writeln('Expected errors that were not reported:'); |
| 312 unreported.map(formatExpectedError).forEach(message.writeln); |
| 313 message.writeln(); |
| 314 } |
| 315 if (unexpected.isNotEmpty) { |
| 316 message.writeln('Errors that were not expected:'); |
| 317 unexpected.map(formatActualError).forEach(message.writeln); |
| 318 message.writeln(); |
| 319 } |
| 320 if (different.isNotEmpty) { |
| 321 message.writeln('Errors that were reported, but different than expected:'); |
| 322 different.forEach((expected, actual) { |
| 323 message.writeln('Expected: ' + formatExpectedError(expected)); |
| 324 message.writeln('Actual: ' + formatActualError(actual)); |
| 325 }); |
| 326 message.writeln(); |
| 327 } |
| 328 fail('Checker errors do not match expected errors:\n\n$message'); |
| 329 } |
| 330 |
| 331 class _ErrorCollector implements AnalysisErrorListener { |
| 332 final AnalysisContext _context; |
| 333 List<AnalysisError> errors; |
| 334 final bool hints; |
| 335 |
| 336 _ErrorCollector(this._context, {this.hints: true}); |
| 337 |
| 338 void onError(AnalysisError error) { |
| 339 // Unless DDC hints are requested, filter them out. |
| 340 var HINT = ErrorSeverity.INFO.ordinal; |
| 341 if (hints || _errorSeverity(_context, error).ordinal > HINT) { |
| 342 errors.add(error); |
| 343 } |
| 480 } | 344 } |
| 481 } | 345 } |
| 482 | 346 |
| 483 /// Returns an ANSII color escape sequence corresponding to [levelName]. Colors | 347 /// Describes an expected message that should be produced by the checker. |
| 484 /// are defined for: severe, error, warning, or info. Returns null if the level | 348 class _ErrorExpectation { |
| 485 /// name is not recognized. | 349 final int offset; |
| 486 String colorOf(String levelName) { | 350 final ErrorSeverity severity; |
| 487 levelName = levelName.toLowerCase(); | 351 final String typeName; |
| 488 if (levelName == 'shout' || levelName == 'severe' || levelName == 'error') { | 352 |
| 489 return _RED_COLOR; | 353 _ErrorExpectation(this.offset, this.severity, this.typeName); |
| 354 |
| 355 String toString() => '@$offset ${severity.displayName}: [$typeName]'; |
| 356 |
| 357 AnalysisError _removeMatchingActual(List<AnalysisError> actualErrors) { |
| 358 for (var actual in actualErrors) { |
| 359 if (actual.offset == offset) { |
| 360 actualErrors.remove(actual); |
| 361 return actual; |
| 362 } |
| 363 } |
| 364 return null; |
| 490 } | 365 } |
| 491 if (levelName == 'warning') return _MAGENTA_COLOR; | 366 |
| 492 if (levelName == 'info') return _CYAN_COLOR; | 367 static _ErrorExpectation parse(int offset, String descriptor) { |
| 493 return null; | 368 descriptor = descriptor.trim(); |
| 369 var tokens = descriptor.split(' '); |
| 370 if (tokens.length == 1) return _parse(offset, tokens[0]); |
| 371 expect(tokens.length, 4, reason: 'invalid error descriptor'); |
| 372 expect(tokens[1], "should", reason: 'invalid error descriptor'); |
| 373 expect(tokens[2], "be", reason: 'invalid error descriptor'); |
| 374 if (tokens[0] == "pass") return null; |
| 375 // TODO(leafp) For now, we just use whatever the current expectation is, |
| 376 // eventually we could do more automated reporting here. |
| 377 return _parse(offset, tokens[0]); |
| 378 } |
| 379 |
| 380 static _ErrorExpectation _parse(offset, String descriptor) { |
| 381 var tokens = descriptor.split(':'); |
| 382 expect(tokens.length, 2, reason: 'invalid error descriptor'); |
| 383 var name = tokens[0].toUpperCase(); |
| 384 var typeName = tokens[1]; |
| 385 |
| 386 var level = ErrorSeverity.values |
| 387 .firstWhere((l) => l.name == name, orElse: () => null); |
| 388 expect(level, isNotNull, |
| 389 reason: 'invalid severity in error descriptor: `${tokens[0]}`'); |
| 390 expect(typeName, isNotNull, |
| 391 reason: 'invalid type in error descriptor: ${tokens[1]}'); |
| 392 return new _ErrorExpectation(offset, level, typeName); |
| 393 } |
| 494 } | 394 } |
| 495 | 395 |
| 496 const String _RED_COLOR = '\u001b[31m'; | 396 class _TestUriResolver extends ResourceUriResolver { |
| 497 const String _MAGENTA_COLOR = '\u001b[35m'; | 397 final MemoryResourceProvider provider; |
| 498 const String _CYAN_COLOR = '\u001b[36m'; | 398 _TestUriResolver(provider) |
| 499 const String GREEN_COLOR = '\u001b[32m'; | 399 : provider = provider, |
| 500 const String NO_COLOR = '\u001b[0m'; | 400 super(provider); |
| 401 |
| 402 @override |
| 403 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 404 if (uri.scheme == 'package') { |
| 405 return (provider.getResource('/packages/' + uri.path) as File) |
| 406 .createSource(uri); |
| 407 } |
| 408 return super.resolveAbsolute(uri, actualUri); |
| 409 } |
| 410 } |
| OLD | NEW |