Skip to content

Commit d9c05ab

Browse files
authored
Move more use_build_context_synchronously tests (#4264)
1 parent fffb74c commit d9c05ab

File tree

2 files changed

+163
-86
lines changed

2 files changed

+163
-86
lines changed

test/rules/use_build_context_synchronously_test.dart

+163-35
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,96 @@ class UseBuildContextSynchronouslyTest extends LintRuleTest {
2424
@override
2525
String get testPackageRootPath => '$workspaceRootPath/lib';
2626

27+
test_await_afterReferenceToContextInBody() async {
28+
await assertNoDiagnostics(r'''
29+
import 'package:flutter/widgets.dart';
30+
31+
void foo(BuildContext context) async {
32+
Navigator.of(context);
33+
await f();
34+
}
35+
36+
Future<void> f() async {}
37+
''');
38+
}
39+
40+
test_await_beforeReferenceToContextInBody() async {
41+
await assertDiagnostics(r'''
42+
import 'package:flutter/widgets.dart';
43+
44+
void foo(BuildContext context) async {
45+
await f();
46+
Navigator.of(context);
47+
}
48+
49+
Future<void> f() async {}
50+
''', [
51+
lint(94, 21),
52+
]);
53+
}
54+
55+
test_awaitBeforeIf_mountedExitGuardInIfConditionWithOr_beforeReferenceToContext() async {
56+
await assertNoDiagnostics(r'''
57+
import 'package:flutter/widgets.dart';
58+
59+
void foo(BuildContext context) async {
60+
await f();
61+
if (c || !mounted) return;
62+
Navigator.of(context);
63+
}
64+
65+
bool mounted = false;
66+
Future<void> f() async {}
67+
bool get c => true;
68+
''');
69+
}
70+
71+
test_awaitBeforeIf_mountedGuardInIfCondition_referenceToContextInIfBody() async {
72+
await assertNoDiagnostics(r'''
73+
import 'package:flutter/widgets.dart';
74+
75+
void foo(BuildContext context) async {
76+
await f();
77+
if (mounted) {
78+
Navigator.of(context);
79+
}
80+
}
81+
82+
bool mounted = false;
83+
Future<void> f() async {}
84+
''');
85+
}
86+
87+
test_awaitBeforeIf_mountedGuardInIfConditionWithAnd_referenceToContextInIfBody() async {
88+
await assertNoDiagnostics(r'''
89+
import 'package:flutter/widgets.dart';
90+
91+
void foo(BuildContext context) async {
92+
await f();
93+
if (c && mounted) {
94+
Navigator.of(context);
95+
}
96+
}
97+
98+
bool mounted = false;
99+
Future<void> f() async {}
100+
bool get c => true;
101+
''');
102+
}
103+
27104
test_awaitBeforeIfStatement_beforeReferenceToContext() async {
28105
await assertDiagnostics(r'''
29106
import 'package:flutter/widgets.dart';
30107
31-
void foo(BuildContext context, Future<bool> condition) async {
32-
var b = await condition;
108+
void foo(BuildContext context) async {
109+
var b = await c();
33110
if (b) {
34111
Navigator.of(context);
35112
}
36113
}
114+
Future<bool> c() async => true;
37115
''', [
38-
lint(145, 21),
116+
lint(115, 21),
39117
]);
40118
}
41119

@@ -44,15 +122,36 @@ void foo(BuildContext context, Future<bool> condition) async {
44122
await assertNoDiagnostics(r'''
45123
import 'package:flutter/widgets.dart';
46124
47-
void foo(BuildContext context, Future<bool> condition) async {
48-
await condition;
125+
void foo(BuildContext context) async {
126+
await c();
49127
f1(() {
50128
f2(context);
51129
});
52130
}
53131
54132
void f1(Function f) {}
55133
void f2(BuildContext c) {}
134+
Future<bool> c() async => true;
135+
''');
136+
}
137+
138+
test_awaitBeforeSwitch_mountedExitGuardInCase_beforeReferenceToContext() async {
139+
await assertNoDiagnostics(r'''
140+
import 'package:flutter/widgets.dart';
141+
142+
void foo(BuildContext context) async {
143+
await f();
144+
145+
switch ('') {
146+
case 'a':
147+
if (!mounted) {
148+
break;
149+
}
150+
Navigator.of(context);
151+
}
152+
}
153+
bool mounted = false;
154+
Future<void> f() async {}
56155
''');
57156
}
58157

@@ -61,27 +160,28 @@ void f2(BuildContext c) {}
61160
await assertDiagnostics(r'''
62161
import 'package:flutter/widgets.dart';
63162
64-
void foo(BuildContext context, Future<bool> condition) async {
65-
if (await condition) {
163+
void foo(BuildContext context) async {
164+
if (await c()) {
66165
Navigator.of(context);
67166
}
68167
}
69-
168+
Future<bool> c() async => true;
70169
''', [
71-
lint(132, 21),
170+
lint(102, 21),
72171
]);
73172
}
74173

75174
test_awaitInIfCondition_beforeReferenceToContext() async {
76175
await assertDiagnostics(r'''
77176
import 'package:flutter/widgets.dart';
78177
79-
void foo(BuildContext context, Future<bool> condition) async {
80-
if (await condition) return;
178+
void foo(BuildContext context) async {
179+
if (await c()) return;
81180
Navigator.of(context);
82181
}
182+
Future<bool> c() async => true;
83183
''', [
84-
lint(136, 21),
184+
lint(106, 21),
85185
]);
86186
}
87187

@@ -104,13 +204,14 @@ void foo(
104204
await assertNoDiagnostics(r'''
105205
import 'package:flutter/widgets.dart';
106206
107-
void foo(BuildContext context, Future<bool> condition) async {
207+
void foo(BuildContext context) async {
108208
Navigator.of(context);
109209
if (1 == 2) {
110-
await condition;
210+
await c();
111211
return;
112212
}
113213
}
214+
Future<bool> c() async => true;
114215
''');
115216
}
116217

@@ -120,114 +221,141 @@ void foo(BuildContext context, Future<bool> condition) async {
120221
await assertNoDiagnostics(r'''
121222
import 'package:flutter/widgets.dart';
122223
123-
void foo(BuildContext context, Future<bool> condition) async {
224+
void foo(BuildContext context) async {
124225
if (1 == 2) {
125-
await condition;
226+
await c();
126227
return;
127228
}
128229
Navigator.of(context);
129230
}
231+
Future<bool> c() async => true;
130232
''');
131233
}
132234

133235
test_awaitInIfThenAndExitInElse_afterReferenceToContext() async {
134236
await assertNoDiagnostics(r'''
135237
import 'package:flutter/widgets.dart';
136238
137-
void foo(BuildContext context, Future<bool> condition) async {
239+
void foo(BuildContext context) async {
138240
Navigator.of(context);
139241
if (1 == 2) {
140-
await condition;
242+
await c();
141243
} else {
142-
await condition;
244+
await c();
143245
return;
144246
}
145247
}
248+
Future<bool> c() async => true;
146249
''');
147250
}
148251

149252
test_awaitInIfThenAndExitInElse_beforeReferenceToContext() async {
150253
await assertDiagnostics(r'''
151254
import 'package:flutter/widgets.dart';
152255
153-
void foo(BuildContext context, Future<bool> condition) async {
256+
void foo(BuildContext context) async {
154257
if (1 == 2) {
155-
await condition;
258+
await c();
156259
} else {
157-
await condition;
260+
await c();
158261
return;
159262
}
160263
Navigator.of(context);
161264
}
265+
Future<bool> c() async => true;
162266
''', [
163-
lint(190, 21),
267+
lint(154, 21),
164268
]);
165269
}
166270

271+
@FailingTest(reason: 'Logic not implemented yet.')
167272
test_awaitInWhileBody_afterReferenceToContext() async {
273+
await assertDiagnostics(r'''
274+
import 'package:flutter/widgets.dart';
275+
276+
void foo(BuildContext context) async {
277+
while (true) {
278+
// OK the first time only!
279+
Navigator.of(context);
280+
await f();
281+
}
282+
}
283+
284+
bool mounted = false;
285+
Future<void> f() async {}
286+
''', [
287+
lint(149, 21),
288+
]);
289+
}
290+
291+
test_awaitInWhileBody_afterReferenceToContextOutsideWait() async {
168292
await assertNoDiagnostics(r'''
169293
import 'package:flutter/widgets.dart';
170294
171-
void foo(BuildContext context, Future<void> condition) async {
295+
void foo(BuildContext context) async {
172296
Navigator.of(context);
173297
while (true) {
174-
await condition;
298+
await f();
175299
break;
176300
}
177301
}
302+
Future<void> f() async {}
178303
''');
179304
}
180305

181306
test_awaitInWhileBody_beforeReferenceToContext() async {
182307
await assertDiagnostics(r'''
183308
import 'package:flutter/widgets.dart';
184309
185-
void foo(BuildContext context, Future<void> condition) async {
310+
void foo(BuildContext context) async {
186311
while (true) {
187-
await condition;
312+
await f();
188313
break;
189314
}
190315
Navigator.of(context);
191316
}
317+
Future<void> f() async {}
192318
''', [
193-
lint(158, 21),
319+
lint(128, 21),
194320
]);
195321
}
196322

197323
test_awaitThenExitInIfThenAndElse_afterReferenceToContext() async {
198324
await assertNoDiagnostics(r'''
199325
import 'package:flutter/widgets.dart';
200326
201-
void foo(BuildContext context, Future<bool> condition) async {
327+
void foo(BuildContext context) async {
202328
Navigator.of(context);
203329
if (1 == 2) {
204-
await condition;
330+
await c();
205331
return;
206332
} else {
207-
await condition;
333+
await c();
208334
return;
209335
}
210336
}
337+
Future<bool> c() async => true;
211338
''');
212339
}
213340

214341
test_awaitThenExitInIfThenAndElse_beforeReferenceToContext() async {
215342
await assertDiagnostics(r'''
216343
import 'package:flutter/widgets.dart';
217344
218-
void foo(BuildContext context, Future<bool> condition) async {
345+
void foo(BuildContext context) async {
219346
if (1 == 2) {
220-
await condition;
347+
await c();
221348
return;
222349
} else {
223-
await condition;
350+
await c();
224351
return;
225352
}
226353
Navigator.of(context);
227354
}
355+
Future<bool> c() async => true;
228356
''', [
229357
// No lint.
230-
error(WarningCode.DEAD_CODE, 202, 22),
358+
error(WarningCode.DEAD_CODE, 166, 22),
231359
]);
232360
}
233361

0 commit comments

Comments
 (0)