| 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 analyzer.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'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 106 } |
| 107 | 107 |
| 108 /// Adds a file using [addFile] and calls [check]. | 108 /// Adds a file using [addFile] and calls [check]. |
| 109 /// | 109 /// |
| 110 /// Also returns the resolved compilation unit. | 110 /// Also returns the resolved compilation unit. |
| 111 CompilationUnit checkFile(String content) { | 111 CompilationUnit checkFile(String content) { |
| 112 addFile(content); | 112 addFile(content); |
| 113 return check(); | 113 return check(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void initStrongModeTests() { |
| 117 setUp(() { |
| 118 AnalysisEngine.instance.processRequiredPlugins(); |
| 119 files = new MemoryResourceProvider(); |
| 120 _checkCalled = false; |
| 121 }); |
| 122 |
| 123 tearDown(() { |
| 124 // This is a sanity check, in case only addFile is called. |
| 125 expect(_checkCalled, true, reason: 'must call check() method in test case'); |
| 126 files = null; |
| 127 }); |
| 128 } |
| 129 |
| 130 Level _actualErrorLevel(AnalysisError actual) { |
| 131 return const <ErrorSeverity, Level>{ |
| 132 ErrorSeverity.ERROR: Level.SEVERE, |
| 133 ErrorSeverity.WARNING: Level.WARNING, |
| 134 ErrorSeverity.INFO: Level.INFO |
| 135 }[actual.errorCode.errorSeverity]; |
| 136 } |
| 137 |
| 116 SourceSpanWithContext _createSpanHelper( | 138 SourceSpanWithContext _createSpanHelper( |
| 117 LineInfo lineInfo, int start, Source source, String content, | 139 LineInfo lineInfo, int start, Source source, String content, |
| 118 {int end}) { | 140 {int end}) { |
| 119 var startLoc = _locationForOffset(lineInfo, source.uri, start); | 141 var startLoc = _locationForOffset(lineInfo, source.uri, start); |
| 120 var endLoc = _locationForOffset(lineInfo, source.uri, end ?? start); | 142 var endLoc = _locationForOffset(lineInfo, source.uri, end ?? start); |
| 121 | 143 |
| 122 var lineStart = startLoc.offset - startLoc.column; | 144 var lineStart = startLoc.offset - startLoc.column; |
| 123 // Find the end of the line. This is not exposed directly on LineInfo, but | 145 // Find the end of the line. This is not exposed directly on LineInfo, but |
| 124 // we can find it pretty easily. | 146 // we can find it pretty easily. |
| 125 // TODO(jmesserly): for now we do the simple linear scan. Ideally we can get | 147 // TODO(jmesserly): for now we do the simple linear scan. Ideally we can get |
| (...skipping 16 matching lines...) Expand all Loading... |
| 142 String _errorCodeName(ErrorCode errorCode) { | 164 String _errorCodeName(ErrorCode errorCode) { |
| 143 var name = errorCode.name; | 165 var name = errorCode.name; |
| 144 final prefix = 'STRONG_MODE_'; | 166 final prefix = 'STRONG_MODE_'; |
| 145 if (name.startsWith(prefix)) { | 167 if (name.startsWith(prefix)) { |
| 146 return name.substring(prefix.length); | 168 return name.substring(prefix.length); |
| 147 } else { | 169 } else { |
| 148 return name; | 170 return name; |
| 149 } | 171 } |
| 150 } | 172 } |
| 151 | 173 |
| 152 void initStrongModeTests() { | |
| 153 setUp(() { | |
| 154 AnalysisEngine.instance.processRequiredPlugins(); | |
| 155 files = new MemoryResourceProvider(); | |
| 156 _checkCalled = false; | |
| 157 }); | |
| 158 | |
| 159 tearDown(() { | |
| 160 // This is a sanity check, in case only addFile is called. | |
| 161 expect(_checkCalled, true, reason: 'must call check() method in test case'); | |
| 162 files = null; | |
| 163 }); | |
| 164 } | |
| 165 | |
| 166 SourceLocation _locationForOffset(LineInfo lineInfo, Uri uri, int offset) { | |
| 167 var loc = lineInfo.getLocation(offset); | |
| 168 return new SourceLocation(offset, | |
| 169 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); | |
| 170 } | |
| 171 | |
| 172 /// Returns all libraries transitively imported or exported from [start]. | |
| 173 List<LibraryElement> _reachableLibraries(LibraryElement start) { | |
| 174 var results = <LibraryElement>[]; | |
| 175 var seen = new Set(); | |
| 176 void find(LibraryElement lib) { | |
| 177 if (seen.contains(lib)) return; | |
| 178 seen.add(lib); | |
| 179 results.add(lib); | |
| 180 lib.importedLibraries.forEach(find); | |
| 181 lib.exportedLibraries.forEach(find); | |
| 182 } | |
| 183 find(start); | |
| 184 return results; | |
| 185 } | |
| 186 | |
| 187 Level _actualErrorLevel(AnalysisError actual) { | |
| 188 return const <ErrorSeverity, Level>{ | |
| 189 ErrorSeverity.ERROR: Level.SEVERE, | |
| 190 ErrorSeverity.WARNING: Level.WARNING, | |
| 191 ErrorSeverity.INFO: Level.INFO | |
| 192 }[actual.errorCode.errorSeverity]; | |
| 193 } | |
| 194 | |
| 195 void _expectErrors(CompilationUnit unit, List<AnalysisError> actualErrors) { | 174 void _expectErrors(CompilationUnit unit, List<AnalysisError> actualErrors) { |
| 196 var expectedErrors = _findExpectedErrors(unit.beginToken); | 175 var expectedErrors = _findExpectedErrors(unit.beginToken); |
| 197 | 176 |
| 198 // Sort both lists: by offset, then level, then name. | 177 // Sort both lists: by offset, then level, then name. |
| 199 actualErrors.sort((x, y) { | 178 actualErrors.sort((x, y) { |
| 200 int delta = x.offset.compareTo(y.offset); | 179 int delta = x.offset.compareTo(y.offset); |
| 201 if (delta != 0) return delta; | 180 if (delta != 0) return delta; |
| 202 | 181 |
| 203 delta = x.errorCode.errorSeverity.compareTo(y.errorCode.errorSeverity); | 182 delta = x.errorCode.errorSeverity.compareTo(y.errorCode.errorSeverity); |
| 204 if (delta != 0) return delta; | 183 if (delta != 0) return delta; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 expectedErrors.add(expected); | 239 expectedErrors.add(expected); |
| 261 } | 240 } |
| 262 } | 241 } |
| 263 } | 242 } |
| 264 } | 243 } |
| 265 } | 244 } |
| 266 } | 245 } |
| 267 return expectedErrors; | 246 return expectedErrors; |
| 268 } | 247 } |
| 269 | 248 |
| 249 SourceLocation _locationForOffset(LineInfo lineInfo, Uri uri, int offset) { |
| 250 var loc = lineInfo.getLocation(offset); |
| 251 return new SourceLocation(offset, |
| 252 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); |
| 253 } |
| 254 |
| 255 /// Returns all libraries transitively imported or exported from [start]. |
| 256 List<LibraryElement> _reachableLibraries(LibraryElement start) { |
| 257 var results = <LibraryElement>[]; |
| 258 var seen = new Set(); |
| 259 void find(LibraryElement lib) { |
| 260 if (seen.contains(lib)) return; |
| 261 seen.add(lib); |
| 262 results.add(lib); |
| 263 lib.importedLibraries.forEach(find); |
| 264 lib.exportedLibraries.forEach(find); |
| 265 } |
| 266 find(start); |
| 267 return results; |
| 268 } |
| 269 |
| 270 void _reportFailure( | 270 void _reportFailure( |
| 271 CompilationUnit unit, | 271 CompilationUnit unit, |
| 272 List<_ErrorExpectation> unreported, | 272 List<_ErrorExpectation> unreported, |
| 273 List<AnalysisError> unexpected, | 273 List<AnalysisError> unexpected, |
| 274 Map<_ErrorExpectation, AnalysisError> different) { | 274 Map<_ErrorExpectation, AnalysisError> different) { |
| 275 // Get the source code. This reads the data again, but it's safe because | 275 // Get the source code. This reads the data again, but it's safe because |
| 276 // all tests use memory file system. | 276 // all tests use memory file system. |
| 277 var sourceCode = unit.element.source.contents.data; | 277 var sourceCode = unit.element.source.contents.data; |
| 278 | 278 |
| 279 String formatActualError(AnalysisError error) { | 279 String formatActualError(AnalysisError error) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 | 390 |
| 391 @override | 391 @override |
| 392 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | 392 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 393 if (uri.scheme == 'package') { | 393 if (uri.scheme == 'package') { |
| 394 return (provider.getResource('/packages/' + uri.path) as File) | 394 return (provider.getResource('/packages/' + uri.path) as File) |
| 395 .createSource(uri); | 395 .createSource(uri); |
| 396 } | 396 } |
| 397 return super.resolveAbsolute(uri, actualUri); | 397 return super.resolveAbsolute(uri, actualUri); |
| 398 } | 398 } |
| 399 } | 399 } |
| OLD | NEW |