OLD | NEW |
(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("with an empty file", () { |
| 25 new File(_path).writeAsStringSync(""); |
| 26 |
| 27 var annotations = parseAnnotations(_path); |
| 28 expect(annotations, isEmpty); |
| 29 }); |
| 30 |
| 31 test("before a library tag", () { |
| 32 new File(_path).writeAsStringSync("@Annotation()\nlibrary foo;"); |
| 33 |
| 34 var annotations = parseAnnotations(_path); |
| 35 expect(annotations, hasLength(1)); |
| 36 expect(annotations.first.name.name, equals('Annotation')); |
| 37 expect(annotations.first.arguments.arguments, isEmpty); |
| 38 }); |
| 39 |
| 40 test("before an import", () { |
| 41 new File(_path).writeAsStringSync("@Annotation()\nimport 'foo';"); |
| 42 |
| 43 var annotations = parseAnnotations(_path); |
| 44 expect(annotations, hasLength(1)); |
| 45 expect(annotations.first.name.name, equals('Annotation')); |
| 46 expect(annotations.first.arguments.arguments, isEmpty); |
| 47 }); |
| 48 |
| 49 test("multiple annotations", () { |
| 50 new File(_path).writeAsStringSync( |
| 51 "@Annotation1() @Annotation2()\n@Annotation3()\nlibrary foo;"); |
| 52 |
| 53 var annotations = parseAnnotations(_path); |
| 54 expect(annotations, hasLength(3)); |
| 55 expect(annotations[0].name.name, equals('Annotation1')); |
| 56 expect(annotations[0].arguments.arguments, isEmpty); |
| 57 expect(annotations[1].name.name, equals('Annotation2')); |
| 58 expect(annotations[1].arguments.arguments, isEmpty); |
| 59 expect(annotations[2].name.name, equals('Annotation3')); |
| 60 expect(annotations[2].arguments.arguments, isEmpty); |
| 61 }); |
| 62 |
| 63 test("with no arguments", () { |
| 64 new File(_path).writeAsStringSync("@Annotation\nlibrary foo;"); |
| 65 |
| 66 var annotations = parseAnnotations(_path); |
| 67 expect(annotations, hasLength(1)); |
| 68 expect(annotations.first.name.name, equals('Annotation')); |
| 69 expect(annotations.first.arguments, isNull); |
| 70 }); |
| 71 |
| 72 test("with positional arguments", () { |
| 73 new File(_path).writeAsStringSync("@Annotation('foo', 12)\nlibrary foo;"); |
| 74 |
| 75 var annotations = parseAnnotations(_path); |
| 76 expect(annotations, hasLength(1)); |
| 77 expect(annotations.first.name.name, equals('Annotation')); |
| 78 var args = annotations.first.arguments.arguments; |
| 79 expect(args, hasLength(2)); |
| 80 expect(args[0], new isInstanceOf<StringLiteral>()); |
| 81 expect(args[0].stringValue, equals('foo')); |
| 82 expect(args[1], new isInstanceOf<IntegerLiteral>()); |
| 83 expect(args[1].value, equals(12)); |
| 84 }); |
| 85 |
| 86 test("with named arguments", () { |
| 87 new File(_path).writeAsStringSync( |
| 88 "@Annotation(name1: 'foo', name2: 12)\nlibrary foo;"); |
| 89 |
| 90 var annotations = parseAnnotations(_path); |
| 91 expect(annotations, hasLength(1)); |
| 92 expect(annotations.first.name.name, equals('Annotation')); |
| 93 var args = annotations.first.arguments.arguments; |
| 94 expect(args, hasLength(2)); |
| 95 expect(args[0], new isInstanceOf<NamedExpression>()); |
| 96 expect(args[0].expression, new isInstanceOf<StringLiteral>()); |
| 97 expect(args[0].expression.stringValue, equals('foo')); |
| 98 expect(args[1], new isInstanceOf<IntegerLiteral>()); |
| 99 expect(args[1].expression, new isInstanceOf<IntegerLiteral>()); |
| 100 expect(args[1].expression.value, equals(12)); |
| 101 }); |
| 102 |
| 103 test("with a prefix/named constructor", () { |
| 104 new File(_path).writeAsStringSync("@Annotation.name()\nlibrary foo;"); |
| 105 |
| 106 var annotations = parseAnnotations(_path); |
| 107 expect(annotations, hasLength(1)); |
| 108 expect(annotations.first.name, new isInstanceOf<PrefixedIdentifier>()); |
| 109 expect(annotations.first.name.prefix.name, equals('Annotation')); |
| 110 expect(annotations.first.name.identifier.name, equals('name')); |
| 111 expect(annotations.first.constructorName, isNull); |
| 112 expect(annotations.first.arguments.arguments, isEmpty); |
| 113 }); |
| 114 |
| 115 test("with a prefix and named constructor", () { |
| 116 new File(_path).writeAsStringSync( |
| 117 "@prefix.Annotation.name()\nlibrary foo;"); |
| 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 |
| 128 test("annotations after the first directive are ignored", () { |
| 129 new File(_path).writeAsStringSync( |
| 130 "library foo;\n@prefix.Annotation.name()"); |
| 131 |
| 132 expect(parseAnnotations(_path), isEmpty); |
| 133 }); |
| 134 |
| 135 group("comments are ignored", () { |
| 136 test("before an annotation", () { |
| 137 new File(_path).writeAsStringSync( |
| 138 "/* comment */@Annotation()\nlibrary foo;"); |
| 139 |
| 140 var annotations = parseAnnotations(_path); |
| 141 expect(annotations, hasLength(1)); |
| 142 expect(annotations.first.name.name, equals('Annotation')); |
| 143 expect(annotations.first.arguments.arguments, isEmpty); |
| 144 }); |
| 145 |
| 146 test("within an annotation", () { |
| 147 new File(_path).writeAsStringSync( |
| 148 "@Annotation(/* comment */)\nlibrary foo;"); |
| 149 |
| 150 var annotations = parseAnnotations(_path); |
| 151 expect(annotations, hasLength(1)); |
| 152 expect(annotations.first.name.name, equals('Annotation')); |
| 153 expect(annotations.first.arguments.arguments, isEmpty); |
| 154 }); |
| 155 |
| 156 test("after an annotation", () { |
| 157 new File(_path).writeAsStringSync( |
| 158 "@Annotation()/* comment */\nlibrary foo;"); |
| 159 |
| 160 var annotations = parseAnnotations(_path); |
| 161 expect(annotations, hasLength(1)); |
| 162 expect(annotations.first.name.name, equals('Annotation')); |
| 163 expect(annotations.first.arguments.arguments, isEmpty); |
| 164 }); |
| 165 |
| 166 test("between annotations", () { |
| 167 new File(_path).writeAsStringSync( |
| 168 "@Annotation1()/* comment */@Annotation2()\nlibrary foo;"); |
| 169 |
| 170 var annotations = parseAnnotations(_path); |
| 171 expect(annotations, hasLength(2)); |
| 172 expect(annotations[0].name.name, equals('Annotation1')); |
| 173 expect(annotations[0].arguments.arguments, isEmpty); |
| 174 expect(annotations[1].name.name, equals('Annotation2')); |
| 175 expect(annotations[1].arguments.arguments, isEmpty); |
| 176 }); |
| 177 }); |
| 178 } |
OLD | NEW |