|
| 1 | +// Copyright (c) 2020, 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/driver_resolution.dart'; |
| 9 | + |
| 10 | +main() { |
| 11 | + defineReflectiveSuite(() { |
| 12 | + defineReflectiveTests(PrivateSetterTest); |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +@reflectiveTest |
| 17 | +class PrivateSetterTest extends DriverResolutionTest { |
| 18 | + test_typeLiteral_privateField_differentLibrary() async { |
| 19 | + newFile('/test/lib/a.dart', content: r''' |
| 20 | +class A { |
| 21 | + static int _foo = 0; |
| 22 | +} |
| 23 | +'''); |
| 24 | + await assertErrorsInCode(r''' |
| 25 | +import 'a.dart'; |
| 26 | +
|
| 27 | +main() { |
| 28 | + A._foo = 0; |
| 29 | +} |
| 30 | +''', [ |
| 31 | + error(CompileTimeErrorCode.PRIVATE_SETTER, 31, 4), |
| 32 | + ]); |
| 33 | + |
| 34 | + var aImport = findElement.importFind('package:test/a.dart'); |
| 35 | + assertElement( |
| 36 | + findNode.simple('_foo = 0'), |
| 37 | + aImport.setter('_foo'), |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + test_typeLiteral_privateField_sameLibrary() async { |
| 42 | + await assertNoErrorsInCode(r''' |
| 43 | +class A { |
| 44 | + // ignore:unused_field |
| 45 | + static int _foo = 0; |
| 46 | +} |
| 47 | +
|
| 48 | +main() { |
| 49 | + A._foo = 0; |
| 50 | +} |
| 51 | +'''); |
| 52 | + } |
| 53 | + |
| 54 | + test_typeLiteral_privateSetter__sameLibrary() async { |
| 55 | + await assertNoErrorsInCode(r''' |
| 56 | +class A { |
| 57 | + static set _foo(int _) {} |
| 58 | +} |
| 59 | +
|
| 60 | +main() { |
| 61 | + A._foo = 0; |
| 62 | +} |
| 63 | +'''); |
| 64 | + } |
| 65 | + |
| 66 | + test_typeLiteral_privateSetter_differentLibrary_hasGetter() async { |
| 67 | + newFile('/test/lib/a.dart', content: r''' |
| 68 | +class A { |
| 69 | + static set _foo(int _) {} |
| 70 | + |
| 71 | + static int get _foo => 0; |
| 72 | +} |
| 73 | +'''); |
| 74 | + await assertErrorsInCode(r''' |
| 75 | +import 'a.dart'; |
| 76 | +
|
| 77 | +main() { |
| 78 | + A._foo = 0; |
| 79 | +} |
| 80 | +''', [ |
| 81 | + error(CompileTimeErrorCode.PRIVATE_SETTER, 31, 4), |
| 82 | + ]); |
| 83 | + |
| 84 | + var aImport = findElement.importFind('package:test/a.dart'); |
| 85 | + assertElement( |
| 86 | + findNode.simple('_foo = 0'), |
| 87 | + aImport.setter('_foo'), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + test_typeLiteral_privateSetter_differentLibrary_noGetter() async { |
| 92 | + newFile('/test/lib/a.dart', content: r''' |
| 93 | +class A { |
| 94 | + static set _foo(int _) {} |
| 95 | +} |
| 96 | +'''); |
| 97 | + await assertErrorsInCode(r''' |
| 98 | +import 'a.dart'; |
| 99 | +
|
| 100 | +main() { |
| 101 | + A._foo = 0; |
| 102 | +} |
| 103 | +''', [ |
| 104 | + error(CompileTimeErrorCode.PRIVATE_SETTER, 31, 4), |
| 105 | + ]); |
| 106 | + |
| 107 | + var aImport = findElement.importFind('package:test/a.dart'); |
| 108 | + assertElement( |
| 109 | + findNode.simple('_foo = 0'), |
| 110 | + aImport.setter('_foo'), |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
0 commit comments