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

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: Fix test name. 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("in an otherwise empty file", () {
25 new File(_path).writeAsStringSync("@Annotation()");
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 a library tag", () {
34 new File(_path).writeAsStringSync("@Annotation()\nlibrary 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("before an import", () {
43 new File(_path).writeAsStringSync("@Annotation()\nimport 'foo';");
44
45 var annotations = parseAnnotations(_path);
46 expect(annotations, hasLength(1));
47 expect(annotations.first.name.name, equals('Annotation'));
48 expect(annotations.first.arguments.arguments, isEmpty);
49 });
50
51 test("multiple annotations", () {
52 new File(_path).writeAsStringSync(
53 "@Annotation1() @Annotation2()\n@Annotation3()");
54
55 var annotations = parseAnnotations(_path);
56 expect(annotations, hasLength(3));
57 expect(annotations[0].name.name, equals('Annotation1'));
58 expect(annotations[0].arguments.arguments, isEmpty);
59 expect(annotations[1].name.name, equals('Annotation2'));
60 expect(annotations[1].arguments.arguments, isEmpty);
61 expect(annotations[2].name.name, equals('Annotation3'));
62 expect(annotations[2].arguments.arguments, isEmpty);
63 });
64
65 test("with no arguments", () {
66 new File(_path).writeAsStringSync("@Annotation");
67
68 var annotations = parseAnnotations(_path);
69 expect(annotations, hasLength(1));
70 expect(annotations.first.name.name, equals('Annotation'));
71 expect(annotations.first.arguments, isNull);
72 });
73
74 test("with positional arguments", () {
75 new File(_path).writeAsStringSync("@Annotation('foo', 12)");
76
77 var annotations = parseAnnotations(_path);
78 expect(annotations, hasLength(1));
79 expect(annotations.first.name.name, equals('Annotation'));
80 var args = annotations.first.arguments.arguments;
81 expect(args, hasLength(2));
82 expect(args[0], new isInstanceOf<StringLiteral>());
83 expect(args[0].stringValue, equals('foo'));
84 expect(args[1], new isInstanceOf<IntegerLiteral>());
85 expect(args[1].value, equals(12));
86 });
87
88 test("with named arguments", () {
89 new File(_path).writeAsStringSync("@Annotation(name1: 'foo', name2: 12)");
90
91 var annotations = parseAnnotations(_path);
92 expect(annotations, hasLength(1));
93 expect(annotations.first.name.name, equals('Annotation'));
94 var args = annotations.first.arguments.arguments;
95 expect(args, hasLength(2));
96 expect(args[0], new isInstanceOf<NamedExpression>());
97 expect(args[0].expression, new isInstanceOf<StringLiteral>());
98 expect(args[0].expression.stringValue, equals('foo'));
99 expect(args[1], new isInstanceOf<IntegerLiteral>());
100 expect(args[1].expression, new isInstanceOf<IntegerLiteral>());
101 expect(args[1].expression.value, equals(12));
102 });
103
104 test("with a prefix/named constructor", () {
105 new File(_path).writeAsStringSync("@Annotation.name()");
106
107 var annotations = parseAnnotations(_path);
108 expect(annotations, hasLength(1));
109 expect(annotations.first.name, new isInstanceOf<PrefixedIdentifier>());
110 expect(annotations.first.name.prefix.name, equals('Annotation'));
111 expect(annotations.first.name.identifier.name, equals('name'));
112 expect(annotations.first.constructorName, isNull);
113 expect(annotations.first.arguments.arguments, isEmpty);
114 });
115
116 test("with a prefix and named constructor", () {
117 new File(_path).writeAsStringSync("@prefix.Annotation.name()");
118
119 var annotations = parseAnnotations(_path);
120 expect(annotations, hasLength(1));
121 expect(annotations.first.name, new isInstanceOf<PrefixedIdentifier>());
122 expect(annotations.first.name.prefix.name, equals('prefix'));
123 expect(annotations.first.name.identifier.name, equals('Annotation'));
124 expect(annotations.first.constructorName.name, equals('name'));
125 expect(annotations.first.arguments.arguments, isEmpty);
126 });
127 }
Bob Nystrom 2015/03/11 19:27:17 Test that annotations after the first production a
nweiz 2015/03/12 19:24:37 Done.
nweiz 2015/03/12 19:24:37 Done.
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