| // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| import 'package:analyzer/dart/analysis/features.dart'; |
| import 'package:analyzer/dart/ast/ast.dart'; |
| import 'package:analyzer/src/generated/engine.dart'; |
| import 'package:analyzer/src/test_utilities/find_element.dart'; |
| import 'package:test/test.dart'; |
| import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| |
| import '../../summary/resolved_ast_printer.dart'; |
| import 'driver_resolution.dart'; |
| |
| main() { |
| defineReflectiveSuite(() { |
| defineReflectiveTests(MetadataResolutionTest); |
| defineReflectiveTests(MetadataResolutionWithNnbdTest); |
| }); |
| } |
| |
| @reflectiveTest |
| class MetadataResolutionTest extends DriverResolutionTest { |
| test_onFieldFormal() async { |
| await assertNoErrorsInCode(r''' |
| class A { |
| const A(_); |
| } |
| |
| class B { |
| final int f; |
| B({@A( A(0) ) this.f}); |
| } |
| '''); |
| _assertResolvedNodeText(findNode.annotation('@A'), r''' |
| Annotation |
| arguments: ArgumentList |
| arguments |
| InstanceCreationExpression |
| argumentList: ArgumentList |
| arguments |
| IntegerLiteral |
| literal: 0 |
| staticType: int |
| constructorName: ConstructorName |
| type: TypeName |
| name: SimpleIdentifier |
| staticElement: self::@class::A |
| staticType: null |
| token: A |
| type: A |
| staticElement: self::@class::A::@constructor::• |
| staticType: A |
| element: self::@class::A::@constructor::• |
| name: SimpleIdentifier |
| staticElement: self::@class::A |
| staticType: Type |
| token: A |
| '''); |
| } |
| |
| test_otherLibrary_constructor_named() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| final int f; |
| const A.named(this.f); |
| } |
| '''); |
| |
| newFile('/test/lib/b.dart', content: r''' |
| import 'a.dart'; |
| |
| @A.named(42) |
| class B {} |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| import 'b.dart'; |
| |
| B b; |
| '''); |
| |
| var classB = findNode.typeName('B b;').name.staticElement; |
| var annotation = classB.metadata.single; |
| var value = annotation.computeConstantValue(); |
| assertType(value.type, 'A'); |
| expect(value.getField('f').toIntValue(), 42); |
| } |
| |
| test_otherLibrary_constructor_unnamed() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| final int f; |
| const A(this.f); |
| } |
| '''); |
| |
| newFile('/test/lib/b.dart', content: r''' |
| import 'a.dart'; |
| |
| @A(42) |
| class B {} |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| import 'b.dart'; |
| |
| B b; |
| '''); |
| |
| var classB = findNode.typeName('B b;').name.staticElement; |
| var annotation = classB.metadata.single; |
| var value = annotation.computeConstantValue(); |
| assertType(value.type, 'A'); |
| expect(value.getField('f').toIntValue(), 42); |
| } |
| |
| test_otherLibrary_implicitConst() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| final int f; |
| const A(this.f); |
| } |
| |
| class B { |
| final A a; |
| const B(this.a); |
| } |
| |
| @B( A(42) ) |
| class C {} |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| import 'a.dart'; |
| |
| C c; |
| '''); |
| |
| var classC = findNode.typeName('C c;').name.staticElement; |
| var annotation = classC.metadata.single; |
| var value = annotation.computeConstantValue(); |
| assertType(value.type, 'B'); |
| expect(value.getField('a').getField('f').toIntValue(), 42); |
| } |
| |
| @FailingTest(reason: 'Reverted because of dartbug.com/38565') |
| test_sameLibrary_genericClass_constructor_unnamed() async { |
| await assertNoErrorsInCode(r''' |
| class A<T> { |
| final T f; |
| const A(this.f); |
| } |
| |
| @A(42) |
| class B {} |
| '''); |
| var annotation = findElement.class_('B').metadata.single; |
| var value = annotation.computeConstantValue(); |
| assertType(value.type, 'A<int>'); |
| expect(value.getField('f').toIntValue(), 42); |
| } |
| |
| void _assertResolvedNodeText(AstNode node, String expected) { |
| var actual = _resolvedNodeText(node); |
| expect(actual, expected); |
| } |
| |
| String _resolvedNodeText(AstNode node) { |
| var buffer = StringBuffer(); |
| node.accept( |
| ResolvedAstPrinter( |
| selfUriStr: result.uri.toString(), |
| sink: buffer, |
| indent: '', |
| ), |
| ); |
| return buffer.toString(); |
| } |
| } |
| |
| @reflectiveTest |
| class MetadataResolutionWithNnbdTest extends DriverResolutionTest { |
| @override |
| AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl() |
| ..contextFeatures = FeatureSet.forTesting( |
| sdkVersion: '2.7.0', additionalFeatures: [Feature.non_nullable]); |
| |
| ImportFindElement get import_a { |
| return findElement.importFind('package:test/a.dart'); |
| } |
| |
| @override |
| bool get typeToStringWithNullability => true; |
| |
| test_optIn_fromOptOut_class() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| const A(int a); |
| } |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart'; |
| |
| @A(0) |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.simple('A('), |
| declaration: import_a.class_('A'), |
| ); |
| |
| assertElement2( |
| findNode.annotation('@A'), |
| declaration: import_a.unnamedConstructor('A'), |
| isLegacy: true, |
| ); |
| } |
| |
| test_optIn_fromOptOut_class_constructor() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| const A.named(int a); |
| } |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart'; |
| |
| @A.named(0) |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.simple('A.named('), |
| declaration: import_a.class_('A'), |
| ); |
| |
| assertElement2( |
| findNode.annotation('@A'), |
| declaration: import_a.constructor('named', of: 'A'), |
| isLegacy: true, |
| ); |
| } |
| |
| test_optIn_fromOptOut_class_getter() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| const foo = 0; |
| } |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart'; |
| |
| @A.foo |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.simple('A.foo'), |
| declaration: import_a.class_('A'), |
| ); |
| |
| assertElement2( |
| findNode.annotation('@A.foo'), |
| declaration: import_a.getter('foo'), |
| isLegacy: true, |
| ); |
| } |
| |
| test_optIn_fromOptOut_getter() async { |
| newFile('/test/lib/a.dart', content: r''' |
| const foo = 0; |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart'; |
| |
| @foo |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.annotation('@foo'), |
| declaration: import_a.topGet('foo'), |
| isLegacy: true, |
| ); |
| } |
| |
| test_optIn_fromOptOut_prefix_class() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| const A(int a); |
| } |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart' as a; |
| |
| @a.A(0) |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.simple('A('), |
| declaration: import_a.class_('A'), |
| ); |
| |
| assertElement2( |
| findNode.annotation('@a.A'), |
| declaration: import_a.unnamedConstructor('A'), |
| isLegacy: true, |
| ); |
| } |
| |
| test_optIn_fromOptOut_prefix_class_constructor() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| const A.named(int a); |
| } |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart' as a; |
| |
| @a.A.named(0) |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.simple('A.named('), |
| declaration: import_a.class_('A'), |
| ); |
| |
| assertElement2( |
| findNode.annotation('@a.A'), |
| declaration: import_a.constructor('named', of: 'A'), |
| isLegacy: true, |
| ); |
| } |
| |
| test_optIn_fromOptOut_prefix_class_getter() async { |
| newFile('/test/lib/a.dart', content: r''' |
| class A { |
| const foo = 0; |
| } |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart' as a; |
| |
| @a.A.foo |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.simple('A.foo'), |
| declaration: import_a.class_('A'), |
| ); |
| |
| assertElement2( |
| findNode.annotation('@a.A'), |
| declaration: import_a.getter('foo'), |
| isLegacy: true, |
| ); |
| } |
| |
| test_optIn_fromOptOut_prefix_getter() async { |
| newFile('/test/lib/a.dart', content: r''' |
| const foo = 0; |
| '''); |
| |
| await assertNoErrorsInCode(r''' |
| // @dart = 2.7 |
| import 'a.dart' as a; |
| |
| @a.foo |
| void f() {} |
| '''); |
| |
| assertElement2( |
| findNode.annotation('@a.foo'), |
| declaration: import_a.topGet('foo'), |
| isLegacy: true, |
| ); |
| } |
| } |