|
| 1 | +// Copyright (c) 2021, 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 'package:analyzer/src/error/codes.dart'; |
| 6 | +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 7 | + |
| 8 | +import '../dart/resolution/context_collection_resolution.dart'; |
| 9 | + |
| 10 | +main() { |
| 11 | + defineReflectiveSuite(() { |
| 12 | + defineReflectiveTests(InvalidImplementationOverrideTest); |
| 13 | + defineReflectiveTests(InvalidImplementationOverrideWithNullSafetyTest); |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +@reflectiveTest |
| 18 | +class InvalidImplementationOverrideTest extends PubPackageResolutionTest { |
| 19 | + test_getter_abstractOverridesConcrete() async { |
| 20 | + await assertErrorsInCode(''' |
| 21 | +class A { |
| 22 | + num get g => 7; |
| 23 | +} |
| 24 | +class B extends A { |
| 25 | + int get g; |
| 26 | +} |
| 27 | +''', [ |
| 28 | + error(CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE, 36, 1), |
| 29 | + ]); |
| 30 | + } |
| 31 | + |
| 32 | + test_method_abstractOverridesConcrete() async { |
| 33 | + await assertErrorsInCode(''' |
| 34 | +class A { |
| 35 | + int add(int a, int b) => a + b; |
| 36 | +} |
| 37 | +class B extends A { |
| 38 | + int add(); |
| 39 | +} |
| 40 | +''', [ |
| 41 | + error(CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE, 52, 1), |
| 42 | + error(CompileTimeErrorCode.INVALID_OVERRIDE, 72, 3), |
| 43 | + ]); |
| 44 | + } |
| 45 | + |
| 46 | + test_method_abstractOverridesConcrete_expandedParameterType() async { |
| 47 | + await assertErrorsInCode(''' |
| 48 | +class A { |
| 49 | + int add(int a) => a; |
| 50 | +} |
| 51 | +class B extends A { |
| 52 | + int add(num a); |
| 53 | +} |
| 54 | +''', [ |
| 55 | + error(CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE, 41, 1), |
| 56 | + ]); |
| 57 | + } |
| 58 | + |
| 59 | + test_method_abstractOverridesConcrete_expandedParameterType_covariant() async { |
| 60 | + await assertNoErrorsInCode(''' |
| 61 | +class A { |
| 62 | + int add(covariant int a) => a; |
| 63 | +} |
| 64 | +class B extends A { |
| 65 | + int add(num a); |
| 66 | +} |
| 67 | +'''); |
| 68 | + } |
| 69 | + |
| 70 | + test_method_abstractOverridesConcrete_withOptional() async { |
| 71 | + await assertErrorsInCode(''' |
| 72 | +class A { |
| 73 | + int add() => 7; |
| 74 | +} |
| 75 | +class B extends A { |
| 76 | + int add([int a = 0, int b = 0]); |
| 77 | +} |
| 78 | +''', [ |
| 79 | + error(CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE, 36, 1), |
| 80 | + ]); |
| 81 | + } |
| 82 | + |
| 83 | + test_method_abstractOverridesConcreteInMixin() async { |
| 84 | + await assertErrorsInCode(''' |
| 85 | +mixin M { |
| 86 | + int add(int a, int b) => a + b; |
| 87 | +} |
| 88 | +class A with M { |
| 89 | + int add(); |
| 90 | +} |
| 91 | +''', [ |
| 92 | + error(CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE, 52, 1), |
| 93 | + error(CompileTimeErrorCode.INVALID_OVERRIDE, 69, 3), |
| 94 | + ]); |
| 95 | + } |
| 96 | + |
| 97 | + test_method_abstractOverridesConcreteViaMixin() async { |
| 98 | + await assertErrorsInCode(''' |
| 99 | +class A { |
| 100 | + int add(int a, int b) => a + b; |
| 101 | +} |
| 102 | +mixin M { |
| 103 | + int add(); |
| 104 | +} |
| 105 | +class B extends A with M {} |
| 106 | +''', [ |
| 107 | + error(CompileTimeErrorCode.INVALID_IMPLEMENTATION_OVERRIDE, 77, 1), |
| 108 | + error(CompileTimeErrorCode.INVALID_OVERRIDE, 94, 1), |
| 109 | + ]); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +@reflectiveTest |
| 114 | +class InvalidImplementationOverrideWithNullSafetyTest |
| 115 | + extends PubPackageResolutionTest with WithNullSafetyMixin {} |
0 commit comments