| 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 library dev_compiler.src.testing; | 5 library dev_compiler.src.testing; |
| 6 | 6 |
| 7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | 9 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 return _parse(tokens[0]); | 262 return _parse(tokens[0]); |
| 263 } | 263 } |
| 264 | 264 |
| 265 String toString() => '$level $type'; | 265 String toString() => '$level $type'; |
| 266 } | 266 } |
| 267 | 267 |
| 268 /// Uri resolver that can load test files from memory. | 268 /// Uri resolver that can load test files from memory. |
| 269 class TestUriResolver extends UriResolver { | 269 class TestUriResolver extends UriResolver { |
| 270 final Map<Uri, TestSource> files = <Uri, TestSource>{}; | 270 final Map<Uri, TestSource> files = <Uri, TestSource>{}; |
| 271 | 271 |
| 272 TestUriResolver(Map<String, String> allFiles) { | 272 /// Whether to represent a non-existing file with a [TestSource] (default |
| 273 /// behavior from analyzer), or to use null (possible when overriding the |
| 274 /// package-url-resolvers.) |
| 275 final bool representNonExistingFiles; |
| 276 |
| 277 TestUriResolver(Map<String, String> allFiles, |
| 278 {this.representNonExistingFiles: true}) { |
| 273 allFiles.forEach((key, value) { | 279 allFiles.forEach((key, value) { |
| 274 var uri = key.startsWith('package:') ? Uri.parse(key) : new Uri.file(key); | 280 var uri = key.startsWith('package:') ? Uri.parse(key) : new Uri.file(key); |
| 275 files[uri] = new TestSource(uri, value); | 281 files[uri] = new TestSource(uri, value); |
| 276 }); | 282 }); |
| 277 } | 283 } |
| 278 | 284 |
| 279 Source resolveAbsolute(Uri uri) { | 285 Source resolveAbsolute(Uri uri) { |
| 280 if (uri.scheme != 'file' && uri.scheme != 'package') return null; | 286 if (uri.scheme != 'file' && uri.scheme != 'package') return null; |
| 287 if (!representNonExistingFiles) return files[uri]; |
| 281 return files.putIfAbsent(uri, () => new TestSource(uri, null)); | 288 return files.putIfAbsent(uri, () => new TestSource(uri, null)); |
| 282 } | 289 } |
| 283 } | 290 } |
| 284 | 291 |
| 285 class TestContents implements TimestampedData<String> { | 292 class TestContents implements TimestampedData<String> { |
| 286 int modificationTime; | 293 int modificationTime; |
| 287 String data; | 294 String data; |
| 288 | 295 |
| 289 TestContents(this.modificationTime, this.data); | 296 TestContents(this.modificationTime, this.data); |
| 290 } | 297 } |
| 291 | 298 |
| 292 /// An in memory source file. | 299 /// An in memory source file. |
| 293 class TestSource implements Source { | 300 class TestSource implements Source { |
| 294 final Uri uri; | 301 final Uri uri; |
| 295 TestContents contents; | 302 TestContents contents; |
| 296 final SourceFile _file; | 303 final SourceFile _file; |
| 297 final UriKind uriKind; | 304 final UriKind uriKind; |
| 298 bool _exists; | |
| 299 | 305 |
| 300 TestSource(uri, contents) | 306 TestSource(uri, contents) |
| 301 : uri = uri, | 307 : uri = uri, |
| 302 _exists = contents != null, | |
| 303 contents = new TestContents(1, contents), | 308 contents = new TestContents(1, contents), |
| 304 _file = contents != null ? new SourceFile(contents, url: uri) : null, | 309 _file = contents != null ? new SourceFile(contents, url: uri) : null, |
| 305 uriKind = uri.scheme == 'file' ? UriKind.FILE_URI : UriKind.PACKAGE_URI; | 310 uriKind = uri.scheme == 'file' ? UriKind.FILE_URI : UriKind.PACKAGE_URI; |
| 306 | 311 |
| 307 bool exists() => _exists; | 312 bool exists() => contents.data != null; |
| 308 | 313 |
| 309 Source get source => this; | 314 Source get source => this; |
| 310 | 315 |
| 311 String _encoding; | 316 String _encoding; |
| 312 String get encoding => _encoding != null ? _encoding : (_encoding = '$uri'); | 317 String get encoding => _encoding != null ? _encoding : (_encoding = '$uri'); |
| 313 | 318 |
| 314 String get fullName => uri.path; | 319 String get fullName => uri.path; |
| 315 | 320 |
| 316 int get modificationStamp => contents.modificationTime; | 321 int get modificationStamp => contents.modificationTime; |
| 317 String get shortName => path.basename(uri.path); | 322 String get shortName => path.basename(uri.path); |
| 318 | 323 |
| 319 operator ==(other) => other is TestSource && uri == other.uri; | 324 operator ==(other) => other is TestSource && uri == other.uri; |
| 320 int get hashCode => uri.hashCode; | 325 int get hashCode => uri.hashCode; |
| 321 bool get isInSystemLibrary => false; | 326 bool get isInSystemLibrary => false; |
| 322 | 327 |
| 323 Uri resolveRelativeUri(Uri relativeUri) => uri.resolveUri(relativeUri); | 328 Uri resolveRelativeUri(Uri relativeUri) => uri.resolveUri(relativeUri); |
| 324 | 329 |
| 325 SourceSpan spanFor(AstNode node) { | 330 SourceSpan spanFor(AstNode node) { |
| 326 final begin = node is AnnotatedNode | 331 final begin = node is AnnotatedNode |
| 327 ? node.firstTokenAfterCommentAndMetadata.offset | 332 ? node.firstTokenAfterCommentAndMetadata.offset |
| 328 : node.offset; | 333 : node.offset; |
| 329 return _file.span(begin, node.end); | 334 return _file.span(begin, node.end); |
| 330 } | 335 } |
| 331 | 336 |
| 332 String toString() => '[$runtimeType: $uri]'; | 337 String toString() => '[$runtimeType: $uri]'; |
| 333 } | 338 } |
| OLD | NEW |