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 unittest.runner.parse_metadata; | 5 library test.runner.parse_metadata; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
12 import 'package:source_span/source_span.dart'; | 12 import 'package:source_span/source_span.dart'; |
13 | 13 |
14 import '../backend/metadata.dart'; | 14 import '../backend/metadata.dart'; |
15 import '../util/dart.dart'; | 15 import '../util/dart.dart'; |
16 | 16 |
17 /// Parse the test metadata for the test file at [path]. | 17 /// Parse the test metadata for the test file at [path]. |
18 /// | 18 /// |
19 /// Throws an [AnalysisError] if parsing fails or a [FormatException] if the | 19 /// Throws an [AnalysisError] if parsing fails or a [FormatException] if the |
20 /// test annotations are incorrect. | 20 /// test annotations are incorrect. |
21 Metadata parseMetadata(String path) { | 21 Metadata parseMetadata(String path) { |
22 var testOn; | 22 var testOn; |
23 | 23 |
24 var contents = new File(path).readAsStringSync(); | 24 var contents = new File(path).readAsStringSync(); |
25 var directives = parseDirectives(contents, name: path).directives; | 25 var directives = parseDirectives(contents, name: path).directives; |
26 var annotations = directives.isEmpty ? [] : directives.first.metadata; | 26 var annotations = directives.isEmpty ? [] : directives.first.metadata; |
27 | 27 |
28 // We explicitly *don't* just look for "package:unittest" imports here, | 28 // We explicitly *don't* just look for "package:test" imports here, |
29 // because it could be re-exported from another library. | 29 // because it could be re-exported from another library. |
30 var prefixes = directives.map((directive) { | 30 var prefixes = directives.map((directive) { |
31 if (directive is! ImportDirective) return null; | 31 if (directive is! ImportDirective) return null; |
32 if (directive.prefix == null) return null; | 32 if (directive.prefix == null) return null; |
33 return directive.prefix.name; | 33 return directive.prefix.name; |
34 }).where((prefix) => prefix != null).toSet(); | 34 }).where((prefix) => prefix != null).toSet(); |
35 | 35 |
36 for (var annotation in annotations) { | 36 for (var annotation in annotations) { |
37 // The annotation syntax is ambiguous between named constructors and | 37 // The annotation syntax is ambiguous between named constructors and |
38 // prefixed annotations, so we need to resolve that ambiguity using the | 38 // prefixed annotations, so we need to resolve that ambiguity using the |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 throw new SourceSpanFormatException(error.message, span); | 110 throw new SourceSpanFormatException(error.message, span); |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 /// Creates a [SourceSpan] for [node]. | 114 /// Creates a [SourceSpan] for [node]. |
115 SourceSpan _spanFor(AstNode node, String path) => | 115 SourceSpan _spanFor(AstNode node, String path) => |
116 // Load a SourceFile from scratch here since we're only ever going to emit | 116 // Load a SourceFile from scratch here since we're only ever going to emit |
117 // one error per file anyway. | 117 // one error per file anyway. |
118 new SourceFile(new File(path).readAsStringSync(), url: p.toUri(path)) | 118 new SourceFile(new File(path).readAsStringSync(), url: p.toUri(path)) |
119 .span(node.offset, node.end); | 119 .span(node.offset, node.end); |
OLD | NEW |