Skip to content
Snippets Groups Projects
Commit c5c1e7ea authored by Natalie Weizenbaum's avatar Natalie Weizenbaum
Browse files

Add a utility function to parse annotations in a test file.

See #6

R=rnystrom@google.com, kevmoo@google.com

Review URL: https://codereview.chromium.org//986973006
parent 0fb1b864
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ import 'dart:async'; ...@@ -8,6 +8,7 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:isolate'; import 'dart:isolate';
import 'package:analyzer/analyzer.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'io.dart'; import 'io.dart';
...@@ -70,3 +71,13 @@ void _isolateBuffer(message) { ...@@ -70,3 +71,13 @@ void _isolateBuffer(message) {
}); });
}); });
} }
/// Parse all the annotations at the beginning of a Dart file.
///
/// This will parse annotations until the first non-annotation production is
/// reached.
List<Annotation> parseAnnotations(String path) {
var contents = new File(path).readAsStringSync();
var directives = parseDirectives(contents, name: path).directives;
return directives.isEmpty ? [] : directives.first.metadata;
}
...@@ -6,6 +6,7 @@ homepage: https://github.com/dart-lang/unittest ...@@ -6,6 +6,7 @@ homepage: https://github.com/dart-lang/unittest
environment: environment:
sdk: '>=1.8.0 <2.0.0' sdk: '>=1.8.0 <2.0.0'
dependencies: dependencies:
analyzer: '>=0.23.0 <0.25.0'
args: '>=0.12.1 <0.14.0' args: '>=0.12.1 <0.14.0'
http_parser: '^0.0.2' http_parser: '^0.0.2'
pool: '^1.0.0' pool: '^1.0.0'
......
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:unittest/unittest.dart';
import 'package:unittest/src/util/dart.dart';
String _sandbox;
String _path;
void main() {
setUp(() {
_sandbox = Directory.systemTemp.createTempSync('unittest_').path;
_path = p.join(_sandbox, "test.dart");
});
tearDown(() {
new Directory(_sandbox).deleteSync(recursive: true);
});
test("with an empty file", () {
new File(_path).writeAsStringSync("");
var annotations = parseAnnotations(_path);
expect(annotations, isEmpty);
});
test("before a library tag", () {
new File(_path).writeAsStringSync("@Annotation()\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
expect(annotations.first.arguments.arguments, isEmpty);
});
test("before an import", () {
new File(_path).writeAsStringSync("@Annotation()\nimport 'foo';");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
expect(annotations.first.arguments.arguments, isEmpty);
});
test("multiple annotations", () {
new File(_path).writeAsStringSync(
"@Annotation1() @Annotation2()\n@Annotation3()\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(3));
expect(annotations[0].name.name, equals('Annotation1'));
expect(annotations[0].arguments.arguments, isEmpty);
expect(annotations[1].name.name, equals('Annotation2'));
expect(annotations[1].arguments.arguments, isEmpty);
expect(annotations[2].name.name, equals('Annotation3'));
expect(annotations[2].arguments.arguments, isEmpty);
});
test("with no arguments", () {
new File(_path).writeAsStringSync("@Annotation\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
expect(annotations.first.arguments, isNull);
});
test("with positional arguments", () {
new File(_path).writeAsStringSync("@Annotation('foo', 12)\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
var args = annotations.first.arguments.arguments;
expect(args, hasLength(2));
expect(args[0], new isInstanceOf<StringLiteral>());
expect(args[0].stringValue, equals('foo'));
expect(args[1], new isInstanceOf<IntegerLiteral>());
expect(args[1].value, equals(12));
});
test("with named arguments", () {
new File(_path).writeAsStringSync(
"@Annotation(name1: 'foo', name2: 12)\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
var args = annotations.first.arguments.arguments;
expect(args, hasLength(2));
expect(args[0], new isInstanceOf<NamedExpression>());
expect(args[0].expression, new isInstanceOf<StringLiteral>());
expect(args[0].expression.stringValue, equals('foo'));
expect(args[1], new isInstanceOf<IntegerLiteral>());
expect(args[1].expression, new isInstanceOf<IntegerLiteral>());
expect(args[1].expression.value, equals(12));
});
test("with a prefix/named constructor", () {
new File(_path).writeAsStringSync("@Annotation.name()\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name, new isInstanceOf<PrefixedIdentifier>());
expect(annotations.first.name.prefix.name, equals('Annotation'));
expect(annotations.first.name.identifier.name, equals('name'));
expect(annotations.first.constructorName, isNull);
expect(annotations.first.arguments.arguments, isEmpty);
});
test("with a prefix and named constructor", () {
new File(_path).writeAsStringSync(
"@prefix.Annotation.name()\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name, new isInstanceOf<PrefixedIdentifier>());
expect(annotations.first.name.prefix.name, equals('prefix'));
expect(annotations.first.name.identifier.name, equals('Annotation'));
expect(annotations.first.constructorName.name, equals('name'));
expect(annotations.first.arguments.arguments, isEmpty);
});
test("annotations after the first directive are ignored", () {
new File(_path).writeAsStringSync(
"library foo;\n@prefix.Annotation.name()");
expect(parseAnnotations(_path), isEmpty);
});
group("comments are ignored", () {
test("before an annotation", () {
new File(_path).writeAsStringSync(
"/* comment */@Annotation()\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
expect(annotations.first.arguments.arguments, isEmpty);
});
test("within an annotation", () {
new File(_path).writeAsStringSync(
"@Annotation(/* comment */)\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
expect(annotations.first.arguments.arguments, isEmpty);
});
test("after an annotation", () {
new File(_path).writeAsStringSync(
"@Annotation()/* comment */\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(1));
expect(annotations.first.name.name, equals('Annotation'));
expect(annotations.first.arguments.arguments, isEmpty);
});
test("between annotations", () {
new File(_path).writeAsStringSync(
"@Annotation1()/* comment */@Annotation2()\nlibrary foo;");
var annotations = parseAnnotations(_path);
expect(annotations, hasLength(2));
expect(annotations[0].name.name, equals('Annotation1'));
expect(annotations[0].arguments.arguments, isEmpty);
expect(annotations[1].name.name, equals('Annotation2'));
expect(annotations[1].arguments.arguments, isEmpty);
});
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment