|
| 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/context_collection_resolution.dart'; |
| 9 | + |
| 10 | +main() { |
| 11 | + defineReflectiveSuite(() { |
| 12 | + defineReflectiveTests(AssignmentOfDoNotStoreTest); |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +@reflectiveTest |
| 17 | +class AssignmentOfDoNotStoreTest extends PubPackageResolutionTest { |
| 18 | + @override |
| 19 | + void setUp() { |
| 20 | + super.setUp(); |
| 21 | + writeTestPackageConfigWithMeta(); |
| 22 | + } |
| 23 | + |
| 24 | + test_classMemberGetter() async { |
| 25 | + await assertErrorsInCode(''' |
| 26 | +import 'package:meta/meta.dart'; |
| 27 | +
|
| 28 | +class A { |
| 29 | + @doNotStore |
| 30 | + String get v => ''; |
| 31 | +} |
| 32 | +
|
| 33 | +class B { |
| 34 | + String f = A().v; |
| 35 | +} |
| 36 | +''', [ |
| 37 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 5), |
| 38 | + ]); |
| 39 | + } |
| 40 | + |
| 41 | + test_classMemberVariable() async { |
| 42 | + await assertErrorsInCode(''' |
| 43 | +import 'package:meta/meta.dart'; |
| 44 | +
|
| 45 | +class A{ |
| 46 | + @doNotStore |
| 47 | + final f = ''; |
| 48 | +} |
| 49 | +
|
| 50 | +class B { |
| 51 | + String f = A().f; |
| 52 | +} |
| 53 | +''', [ |
| 54 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 99, 5), |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + test_classStaticGetter() async { |
| 59 | + await assertErrorsInCode(''' |
| 60 | +import 'package:meta/meta.dart'; |
| 61 | +
|
| 62 | +class A { |
| 63 | + @doNotStore |
| 64 | + static String get v => ''; |
| 65 | +} |
| 66 | +
|
| 67 | +class B { |
| 68 | + String f = A.v; |
| 69 | +} |
| 70 | +''', [ |
| 71 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 113, 3), |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + test_classStaticVariable() async { |
| 76 | + await assertErrorsInCode(''' |
| 77 | +import 'package:meta/meta.dart'; |
| 78 | +
|
| 79 | +class A{ |
| 80 | + @doNotStore |
| 81 | + static final f = ''; |
| 82 | +} |
| 83 | +
|
| 84 | +class B { |
| 85 | + String f = A.f; |
| 86 | +} |
| 87 | +''', [ |
| 88 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 3), |
| 89 | + ]); |
| 90 | + } |
| 91 | + |
| 92 | + test_functionAssignment() async { |
| 93 | + await assertNoErrorsInCode(''' |
| 94 | +import 'package:meta/meta.dart'; |
| 95 | +
|
| 96 | +@doNotStore |
| 97 | +String g(int i) => ''; |
| 98 | +
|
| 99 | +class C { |
| 100 | + String Function(int) f = g; |
| 101 | +} |
| 102 | +'''); |
| 103 | + } |
| 104 | + |
| 105 | + test_functionReturnValue() async { |
| 106 | + await assertErrorsInCode(''' |
| 107 | +import 'package:meta/meta.dart'; |
| 108 | +
|
| 109 | +@doNotStore |
| 110 | +String getV() => ''; |
| 111 | +
|
| 112 | +class A { |
| 113 | + final f = getV(); |
| 114 | +} |
| 115 | +''', [ |
| 116 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 90, 6), |
| 117 | + ]); |
| 118 | + } |
| 119 | + |
| 120 | + test_methodReturnValue() async { |
| 121 | + await assertErrorsInCode(''' |
| 122 | +import 'package:meta/meta.dart'; |
| 123 | +
|
| 124 | +class A { |
| 125 | + @doNotStore |
| 126 | + String getV() => ''; |
| 127 | +} |
| 128 | +
|
| 129 | +class B { |
| 130 | + final f = A().getV(); |
| 131 | +} |
| 132 | +''', [ |
| 133 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 10), |
| 134 | + ]); |
| 135 | + } |
| 136 | + |
| 137 | + test_tearOff() async { |
| 138 | + await assertNoErrorsInCode(''' |
| 139 | +import 'package:meta/meta.dart'; |
| 140 | +
|
| 141 | +@doNotStore |
| 142 | +String getV() => ''; |
| 143 | +
|
| 144 | +class A { |
| 145 | + final f = getV; |
| 146 | +} |
| 147 | +'''); |
| 148 | + } |
| 149 | + |
| 150 | + test_topLevelGetter() async { |
| 151 | + await assertErrorsInCode(''' |
| 152 | +import 'package:meta/meta.dart'; |
| 153 | +
|
| 154 | +@doNotStore |
| 155 | +String get v => ''; |
| 156 | +
|
| 157 | +class A { |
| 158 | + final f = v; |
| 159 | +} |
| 160 | +''', [ |
| 161 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 89, 1), |
| 162 | + ]); |
| 163 | + } |
| 164 | + |
| 165 | + test_topLevelVariable() async { |
| 166 | + await assertErrorsInCode(''' |
| 167 | +import 'package:meta/meta.dart'; |
| 168 | +
|
| 169 | +@doNotStore |
| 170 | +final v = ''; |
| 171 | +
|
| 172 | +class A { |
| 173 | + final f = v; |
| 174 | +} |
| 175 | +''', [ |
| 176 | + error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 83, 1), |
| 177 | + ]); |
| 178 | + } |
| 179 | +} |
0 commit comments