|
6 | 6 | @Timeout.factor(2.0) |
7 | 7 | import 'dart:collection'; |
8 | 8 |
|
| 9 | +import 'package:analyzer/dart/element/element.dart'; |
9 | 10 | import 'package:analyzer/dart/element/type.dart'; |
10 | 11 | import 'package:build_test/build_test.dart'; |
11 | 12 | import 'package:meta/meta.dart'; |
@@ -202,4 +203,87 @@ void main() { |
202 | 203 | ]); |
203 | 204 | expect(listOrMap.isExactlyType(staticMap), isTrue); |
204 | 205 | }); |
| 206 | + |
| 207 | + group('should find annotations', () { |
| 208 | + TypeChecker $A; |
| 209 | + TypeChecker $B; |
| 210 | + TypeChecker $C; |
| 211 | + |
| 212 | + ClassElement $ExampleOfA; |
| 213 | + ClassElement $ExampleOfMultiA; |
| 214 | + ClassElement $ExampleOfAPlusB; |
| 215 | + ClassElement $ExampleOfBPlusC; |
| 216 | + |
| 217 | + setUpAll(() async { |
| 218 | + final resolver = await resolveSource(r''' |
| 219 | + library _test; |
| 220 | +
|
| 221 | + @A() |
| 222 | + class ExampleOfA {} |
| 223 | +
|
| 224 | + @A() |
| 225 | + @A() |
| 226 | + class ExampleOfMultiA {} |
| 227 | +
|
| 228 | + @A() |
| 229 | + @B() |
| 230 | + class ExampleOfAPlusB {} |
| 231 | +
|
| 232 | + @B() |
| 233 | + @C() |
| 234 | + class ExampleOfBPlusC {} |
| 235 | +
|
| 236 | + class A { |
| 237 | + const A(); |
| 238 | + } |
| 239 | +
|
| 240 | + class B { |
| 241 | + const B(); |
| 242 | + } |
| 243 | +
|
| 244 | + class C extends B { |
| 245 | + const C(); |
| 246 | + } |
| 247 | + '''); |
| 248 | + final library = resolver.getLibraryByName('_test'); |
| 249 | + $A = new TypeChecker.fromStatic(library.getType('A').type); |
| 250 | + $B = new TypeChecker.fromStatic(library.getType('B').type); |
| 251 | + $C = new TypeChecker.fromStatic(library.getType('C').type); |
| 252 | + $ExampleOfA = library.getType('ExampleOfA'); |
| 253 | + $ExampleOfMultiA = library.getType('ExampleOfMultiA'); |
| 254 | + $ExampleOfAPlusB = library.getType('ExampleOfAPlusB'); |
| 255 | + $ExampleOfBPlusC = library.getType('ExampleOfBPlusC'); |
| 256 | + }); |
| 257 | + |
| 258 | + test('of a single @A', () { |
| 259 | + final aAnnotation = $A.firstAnnotationOf($ExampleOfA); |
| 260 | + expect(aAnnotation.type.name, 'A'); |
| 261 | + expect($B.annotationsOf($ExampleOfA), isEmpty); |
| 262 | + expect($C.annotationsOf($ExampleOfA), isEmpty); |
| 263 | + }); |
| 264 | + |
| 265 | + test('of a multiple @A', () { |
| 266 | + final aAnnotations = $A.annotationsOf($ExampleOfMultiA); |
| 267 | + expect(aAnnotations.map((a) => a.type.name), ['A', 'A']); |
| 268 | + expect($B.annotationsOf($ExampleOfA), isEmpty); |
| 269 | + expect($C.annotationsOf($ExampleOfA), isEmpty); |
| 270 | + }); |
| 271 | + |
| 272 | + test('of a single @A + single @B', () { |
| 273 | + final aAnnotations = $A.annotationsOf($ExampleOfAPlusB); |
| 274 | + expect(aAnnotations.map((a) => a.type.name), ['A']); |
| 275 | + final bAnnotations = $B.annotationsOf($ExampleOfAPlusB); |
| 276 | + expect(bAnnotations.map((a) => a.type.name), ['B']); |
| 277 | + expect($C.annotationsOf($ExampleOfAPlusB), isEmpty); |
| 278 | + }); |
| 279 | + |
| 280 | + test('of a single @B + single @C', () { |
| 281 | + final cAnnotations = $C.annotationsOf($ExampleOfBPlusC); |
| 282 | + expect(cAnnotations.map((a) => a.type.name), ['C']); |
| 283 | + final bAnnotations = $B.annotationsOf($ExampleOfBPlusC); |
| 284 | + expect(bAnnotations.map((a) => a.type.name), ['B', 'C']); |
| 285 | + final bExact = $B.annotationsOfExact($ExampleOfBPlusC); |
| 286 | + expect(bExact.map((a) => a.type.name), ['B']); |
| 287 | + }); |
| 288 | + }); |
205 | 289 | } |
0 commit comments