Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: test/util/parse_annotations_test.dart

Issue 986973006: Add a utility function to parse annotations in a test file. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« lib/src/util/dart.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:io';
6
7 import 'package:path/path.dart' as p;
8 import 'package:unittest/unittest.dart';
9 import 'package:unittest/src/util/dart.dart';
10
11 String _sandbox;
12 String _path;
13
14 void main() {
15 setUp(() {
16 _sandbox = Directory.systemTemp.createTempSync('unittest_').path;
17 _path = p.join(_sandbox, "test.dart");
18 });
19
20 tearDown(() {
21 new Directory(_sandbox).deleteSync(recursive: true);
22 });
23
24 test("before a library tag", () {
25 new File(_path).writeAsStringSync("@Annotation()\nlibrary foo;");
26
27 var annotations = parseAnnotations(_path);
28 expect(annotations, hasLength(1));
29 expect(annotations.first.name.name, equals('Annotation'));
30 expect(annotations.first.arguments.arguments, isEmpty);
31 });
32
33 test("before an import", () {
34 new File(_path).writeAsStringSync("@Annotation()\nimport 'foo';");
35
36 var annotations = parseAnnotations(_path);
37 expect(annotations, hasLength(1));
38 expect(annotations.first.name.name, equals('Annotation'));
39 expect(annotations.first.arguments.arguments, isEmpty);
40 });
41
42 test("multiple annotations", () {
43 new File(_path).writeAsStringSync(
44 "@Annotation1() @Annotation2()\n@Annotation3()\nlibrary foo;");
45
46 var annotations = parseAnnotations(_path);
47 expect(annotations, hasLength(3));
48 expect(annotations[0].name.name, equals('Annotation1'));
49 expect(annotations[0].arguments.arguments, isEmpty);
50 expect(annotations[1].name.name, equals('Annotation2'));
51 expect(annotations[1].arguments.arguments, isEmpty);
52 expect(annotations[2].name.name, equals('Annotation3'));
53 expect(annotations[2].arguments.arguments, isEmpty);
54 });
55
56 test("with no arguments", () {
57 new File(_path).writeAsStringSync("@Annotation\nlibrary foo;");
58
59 var annotations = parseAnnotations(_path);
60 expect(annotations, hasLength(1));
61 expect(annotations.first.name.name, equals('Annotation'));
62 expect(annotations.first.arguments, isNull);
63 });
64
65 test("with positional arguments", () {
66 new File(_path).writeAsStringSync("@Annotation('foo', 12)\nlibrary foo;");
67
68 var annotations = parseAnnotations(_path);
69 expect(annotations, hasLength(1));
70 expect(annotations.first.name.name, equals('Annotation'));
71 var args = annotations.first.arguments.arguments;
72 expect(args, hasLength(2));
73 expect(args[0], new isInstanceOf<StringLiteral>());
74 expect(args[0].stringValue, equals('foo'));
75 expect(args[1], new isInstanceOf<IntegerLiteral>());
76 expect(args[1].value, equals(12));
77 });
78
79 test("with named arguments", () {
80 new File(_path).writeAsStringSync(
81 "@Annotation(name1: 'foo', name2: 12)\nlibrary foo;");
82
83 var annotations = parseAnnotations(_path);
84 expect(annotations, hasLength(1));
85 expect(annotations.first.name.name, equals('Annotation'));
86 var args = annotations.first.arguments.arguments;
87 expect(args, hasLength(2));
88 expect(args[0], new isInstanceOf<NamedExpression>());
89 expect(args[0].expression, new isInstanceOf<StringLiteral>());
90 expect(args[0].expression.stringValue, equals('foo'));
91 expect(args[1], new isInstanceOf<IntegerLiteral>());
92 expect(args[1].expression, new isInstanceOf<IntegerLiteral>());
93 expect(args[1].expression.value, equals(12));
94 });
95
96 test("with a prefix/named constructor", () {
97 new File(_path).writeAsStringSync("@Annotation.name()\nlibrary foo;");
98
99 var annotations = parseAnnotations(_path);
100 expect(annotations, hasLength(1));
101 expect(annotations.first.name, new isInstanceOf<PrefixedIdentifier>());
102 expect(annotations.first.name.prefix.name, equals('Annotation'));
103 expect(annotations.first.name.identifier.name, equals('name'));
104 expect(annotations.first.constructorName, isNull);
105 expect(annotations.first.arguments.arguments, isEmpty);
106 });
107
108 test("with a prefix and named constructor", () {
109 new File(_path).writeAsStringSync(
110 "@prefix.Annotation.name()\nlibrary foo;");
111
112 var annotations = parseAnnotations(_path);
113 expect(annotations, hasLength(1));
114 expect(annotations.first.name, new isInstanceOf<PrefixedIdentifier>());
115 expect(annotations.first.name.prefix.name, equals('prefix'));
116 expect(annotations.first.name.identifier.name, equals('Annotation'));
117 expect(annotations.first.constructorName.name, equals('name'));
118 expect(annotations.first.arguments.arguments, isEmpty);
119 });
120
121 test("annotations after the first directive are ignored", () {
122 new File(_path).writeAsStringSync(
123 "library foo;\n@prefix.Annotation.name()");
124
125 expect(parseAnnotations(_path), isEmpty);
126 });
127
128 group("comments are ignored", () {
129 test("before an annotation", () {
130 new File(_path).writeAsStringSync(
131 "/* comment */@Annotation()\nlibrary foo;");
132
133 var annotations = parseAnnotations(_path);
134 expect(annotations, hasLength(1));
135 expect(annotations.first.name.name, equals('Annotation'));
136 expect(annotations.first.arguments.arguments, isEmpty);
137 });
138
139 test("within an annotation", () {
140 new File(_path).writeAsStringSync(
141 "@Annotation(/* comment */)\nlibrary foo;");
142
143 var annotations = parseAnnotations(_path);
144 expect(annotations, hasLength(1));
145 expect(annotations.first.name.name, equals('Annotation'));
146 expect(annotations.first.arguments.arguments, isEmpty);
147 });
148
149 test("after an annotation", () {
150 new File(_path).writeAsStringSync(
151 "@Annotation()/* comment */\nlibrary foo;");
152
153 var annotations = parseAnnotations(_path);
154 expect(annotations, hasLength(1));
155 expect(annotations.first.name.name, equals('Annotation'));
156 expect(annotations.first.arguments.arguments, isEmpty);
157 });
158
159 test("between annotations", () {
160 new File(_path).writeAsStringSync(
161 "@Annotation1()/* comment */@Annotation2()\nlibrary foo;");
162
163 var annotations = parseAnnotations(_path);
164 expect(annotations, hasLength(2));
165 expect(annotations[0].name.name, equals('Annotation1'));
166 expect(annotations[0].arguments.arguments, isEmpty);
167 expect(annotations[1].name.name, equals('Annotation2'));
168 expect(annotations[1].arguments.arguments, isEmpty);
169 });
170 });
171 }
OLDNEW
« lib/src/util/dart.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698