|
| 1 | +// Copyright (c) 2017, 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 | +// Increase timeouts on this test which resolves source code and can be slow. |
| 6 | +@Timeout.factor(2.0) |
| 7 | +import 'dart:async'; |
| 8 | + |
| 9 | +import 'package:analyzer/dart/element/element.dart'; |
| 10 | +import 'package:build/build.dart'; |
| 11 | +import 'package:build_test/build_test.dart'; |
| 12 | +import 'package:source_gen/source_gen.dart'; |
| 13 | +import 'package:test/test.dart'; |
| 14 | + |
| 15 | +import 'package:source_gen/src/library.dart' show testingPrefixForType; |
| 16 | + |
| 17 | +void main() { |
| 18 | + // Holds the analyzer open until all of the tests complete. |
| 19 | + final doneWithResolver = new Completer<Null>(); |
| 20 | + |
| 21 | + LibraryReader b; |
| 22 | + |
| 23 | + setUpAll(() async { |
| 24 | + final resolver = await resolveSources( |
| 25 | + { |
| 26 | + // The order here is important; pkg/build_test has a bug where only the |
| 27 | + // first library can be accessed via "libraryFor", the others throw that |
| 28 | + // the file is not a library. |
| 29 | + // |
| 30 | + // See https://github.com/dart-lang/build/issues/843. |
| 31 | + 'test_lib|lib/b.dart': sourceB, |
| 32 | + 'test_lib|lib/a.dart': sourceA, |
| 33 | + }, |
| 34 | + (r) => r, |
| 35 | + tearDown: doneWithResolver.future, |
| 36 | + ); |
| 37 | + b = new LibraryReader( |
| 38 | + await resolver.libraryFor(new AssetId('test_lib', 'lib/b.dart')), |
| 39 | + ); |
| 40 | + expect(b.element, isNotNull); |
| 41 | + }); |
| 42 | + |
| 43 | + tearDownAll(doneWithResolver.complete); |
| 44 | + |
| 45 | + group('top level fields', () { |
| 46 | + List<Element> topLevelFieldTypes; |
| 47 | + |
| 48 | + setUpAll(() { |
| 49 | + topLevelFieldTypes = b.element.definingCompilationUnit.topLevelVariables; |
| 50 | + }); |
| 51 | + |
| 52 | + Element field(String name) => |
| 53 | + topLevelFieldTypes.firstWhere((e) => e.name == name, |
| 54 | + orElse: () => throw new ArgumentError.value( |
| 55 | + name, 'name', 'Could not find a field named $name.')); |
| 56 | + |
| 57 | + test('should read the prefix of a type', () { |
| 58 | + expect( |
| 59 | + testingPrefixForType(b, field('topLevelFieldWithPrefix')), |
| 60 | + 'a_prefixed', |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + test('should read null when the type is not prefixed', () { |
| 65 | + expect( |
| 66 | + testingPrefixForType(b, field('topLevelFieldWithoutPrefix')), |
| 67 | + isNull, |
| 68 | + ); |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +const sourceA = r''' |
| 74 | + class A {} |
| 75 | + class B {} |
| 76 | +'''; |
| 77 | + |
| 78 | +const sourceB = r''' |
| 79 | + import 'a.dart' show B; |
| 80 | + import 'a.dart' as a_prefixed; |
| 81 | +
|
| 82 | + a_prefixed.A topLevelFieldWithPrefix; // [0] |
| 83 | + B topLevelFieldWithoutPrefix; // [1] |
| 84 | +'''; |
0 commit comments