Skip to content

Commit fffb74c

Browse files
authored
Move several if-concerned use_build_context_synchronously tests (#4255)
1 parent c094272 commit fffb74c

File tree

3 files changed

+214
-77
lines changed

3 files changed

+214
-77
lines changed

test/rule_test_support.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,14 @@ abstract class BuildContext {
458458
Widget get widget;
459459
}
460460
461-
class Widget {
461+
class Navigator {
462+
static NavigatorState of(
463+
BuildContext context, {bool rootNavigator = false}) => NavigatorState();
462464
}
465+
466+
class NavigatorState {}
467+
468+
class Widget {}
463469
''');
464470
}
465471
}

test/rules/use_build_context_synchronously_test.dart

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,213 @@ class UseBuildContextSynchronouslyTest extends LintRuleTest {
2424
@override
2525
String get testPackageRootPath => '$workspaceRootPath/lib';
2626

27+
test_awaitBeforeIfStatement_beforeReferenceToContext() async {
28+
await assertDiagnostics(r'''
29+
import 'package:flutter/widgets.dart';
30+
31+
void foo(BuildContext context, Future<bool> condition) async {
32+
var b = await condition;
33+
if (b) {
34+
Navigator.of(context);
35+
}
36+
}
37+
''', [
38+
lint(145, 21),
39+
]);
40+
}
41+
42+
test_awaitBeforeReferenceToContext_inClosure() async {
43+
// todo (pq): what about closures?
44+
await assertNoDiagnostics(r'''
45+
import 'package:flutter/widgets.dart';
46+
47+
void foo(BuildContext context, Future<bool> condition) async {
48+
await condition;
49+
f1(() {
50+
f2(context);
51+
});
52+
}
53+
54+
void f1(Function f) {}
55+
void f2(BuildContext c) {}
56+
''');
57+
}
58+
59+
// https://github.com/dart-lang/linter/issues/3457
60+
test_awaitInIfCondition_aboveReferenceToContext() async {
61+
await assertDiagnostics(r'''
62+
import 'package:flutter/widgets.dart';
63+
64+
void foo(BuildContext context, Future<bool> condition) async {
65+
if (await condition) {
66+
Navigator.of(context);
67+
}
68+
}
69+
70+
''', [
71+
lint(132, 21),
72+
]);
73+
}
74+
75+
test_awaitInIfCondition_beforeReferenceToContext() async {
76+
await assertDiagnostics(r'''
77+
import 'package:flutter/widgets.dart';
78+
79+
void foo(BuildContext context, Future<bool> condition) async {
80+
if (await condition) return;
81+
Navigator.of(context);
82+
}
83+
''', [
84+
lint(136, 21),
85+
]);
86+
}
87+
88+
test_awaitInIfReferencesContext_beforeReferenceToContext() async {
89+
await assertDiagnostics(r'''
90+
import 'package:flutter/widgets.dart';
91+
92+
void foo(
93+
BuildContext context, Future<bool> Function(BuildContext) condition) async {
94+
if (await condition(context)) {
95+
Navigator.of(context);
96+
}
97+
}
98+
''', [
99+
lint(169, 21),
100+
]);
101+
}
102+
103+
test_awaitInIfThen_afterReferenceToContext() async {
104+
await assertNoDiagnostics(r'''
105+
import 'package:flutter/widgets.dart';
106+
107+
void foo(BuildContext context, Future<bool> condition) async {
108+
Navigator.of(context);
109+
if (1 == 2) {
110+
await condition;
111+
return;
112+
}
113+
}
114+
''');
115+
}
116+
117+
test_awaitInIfThen_beforeReferenceToContext() async {
118+
// TODO(srawlins): I think this should report a lint, since an `await` is
119+
// encountered in the if-body.
120+
await assertNoDiagnostics(r'''
121+
import 'package:flutter/widgets.dart';
122+
123+
void foo(BuildContext context, Future<bool> condition) async {
124+
if (1 == 2) {
125+
await condition;
126+
return;
127+
}
128+
Navigator.of(context);
129+
}
130+
''');
131+
}
132+
133+
test_awaitInIfThenAndExitInElse_afterReferenceToContext() async {
134+
await assertNoDiagnostics(r'''
135+
import 'package:flutter/widgets.dart';
136+
137+
void foo(BuildContext context, Future<bool> condition) async {
138+
Navigator.of(context);
139+
if (1 == 2) {
140+
await condition;
141+
} else {
142+
await condition;
143+
return;
144+
}
145+
}
146+
''');
147+
}
148+
149+
test_awaitInIfThenAndExitInElse_beforeReferenceToContext() async {
150+
await assertDiagnostics(r'''
151+
import 'package:flutter/widgets.dart';
152+
153+
void foo(BuildContext context, Future<bool> condition) async {
154+
if (1 == 2) {
155+
await condition;
156+
} else {
157+
await condition;
158+
return;
159+
}
160+
Navigator.of(context);
161+
}
162+
''', [
163+
lint(190, 21),
164+
]);
165+
}
166+
167+
test_awaitInWhileBody_afterReferenceToContext() async {
168+
await assertNoDiagnostics(r'''
169+
import 'package:flutter/widgets.dart';
170+
171+
void foo(BuildContext context, Future<void> condition) async {
172+
Navigator.of(context);
173+
while (true) {
174+
await condition;
175+
break;
176+
}
177+
}
178+
''');
179+
}
180+
181+
test_awaitInWhileBody_beforeReferenceToContext() async {
182+
await assertDiagnostics(r'''
183+
import 'package:flutter/widgets.dart';
184+
185+
void foo(BuildContext context, Future<void> condition) async {
186+
while (true) {
187+
await condition;
188+
break;
189+
}
190+
Navigator.of(context);
191+
}
192+
''', [
193+
lint(158, 21),
194+
]);
195+
}
196+
197+
test_awaitThenExitInIfThenAndElse_afterReferenceToContext() async {
198+
await assertNoDiagnostics(r'''
199+
import 'package:flutter/widgets.dart';
200+
201+
void foo(BuildContext context, Future<bool> condition) async {
202+
Navigator.of(context);
203+
if (1 == 2) {
204+
await condition;
205+
return;
206+
} else {
207+
await condition;
208+
return;
209+
}
210+
}
211+
''');
212+
}
213+
214+
test_awaitThenExitInIfThenAndElse_beforeReferenceToContext() async {
215+
await assertDiagnostics(r'''
216+
import 'package:flutter/widgets.dart';
217+
218+
void foo(BuildContext context, Future<bool> condition) async {
219+
if (1 == 2) {
220+
await condition;
221+
return;
222+
} else {
223+
await condition;
224+
return;
225+
}
226+
Navigator.of(context);
227+
}
228+
''', [
229+
// No lint.
230+
error(WarningCode.DEAD_CODE, 202, 22),
231+
]);
232+
}
233+
27234
/// https://github.com/dart-lang/linter/issues/3818
28235
test_context_propertyAccess() async {
29236
await assertDiagnostics(r'''

test_data/rules/use_build_context_synchronously.dart

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -353,79 +353,3 @@ void topLevel5(BuildContext context) async {
353353
default: //nothing.
354354
}
355355
}
356-
357-
void topLevel6(BuildContext context) async {
358-
Navigator.of(context).pushNamed('route1'); // OK
359-
if (true) {
360-
await Future<void>.delayed(Duration());
361-
return;
362-
}
363-
Navigator.of(context).pushNamed('route2'); // OK
364-
}
365-
366-
void topLevel7(BuildContext context) async {
367-
Navigator.of(context).pushNamed('route1'); // OK
368-
if (true) {
369-
await Future<void>.delayed(Duration());
370-
return;
371-
} else {
372-
await Future<void>.delayed(Duration());
373-
return;
374-
}
375-
Navigator.of(context).pushNamed('route2'); // OK
376-
}
377-
378-
void topLevel8(BuildContext context) async {
379-
Navigator.of(context).pushNamed('route1'); // OK
380-
if (true) {
381-
await Future<void>.delayed(Duration());
382-
} else {
383-
await Future<void>.delayed(Duration());
384-
return;
385-
}
386-
Navigator.of(context).pushNamed('route2'); // LINT
387-
}
388-
389-
void topLevel9(BuildContext context) async {
390-
Navigator.of(context).pushNamed('route1'); // OK
391-
while (true) {
392-
await Future<void>.delayed(Duration());
393-
break;
394-
}
395-
Navigator.of(context).pushNamed('route2'); // LINT
396-
}
397-
398-
void closure(BuildContext context) async {
399-
await Future<void>.delayed(Duration());
400-
401-
// todo (pq): what about closures?
402-
func(() {
403-
f(context); // TODO: LINT
404-
});
405-
}
406-
407-
// https://github.com/dart-lang/linter/issues/3457
408-
void awaitInIf1(BuildContext context, Future<bool> condition) async {
409-
if (await condition) {
410-
Navigator.of(context).pushNamed('routeName'); // LINT
411-
}
412-
}
413-
414-
void awaitInIf2(BuildContext context, Future<bool> condition) async {
415-
var b = await condition;
416-
if (b) {
417-
Navigator.of(context).pushNamed('routeName'); // LINT
418-
}
419-
}
420-
421-
void awaitInIf3(BuildContext context, Future<bool> condition) async {
422-
if (await condition) return;
423-
Navigator.of(context).pushNamed('routeName'); // LINT
424-
}
425-
426-
void awaitInIf4(
427-
BuildContext context, Future<bool> Function(BuildContext) condition) async {
428-
if (await condition(context)) { // OK
429-
Navigator.of(context).pushNamed('routeName'); // LINT
430-
}
431-
}

0 commit comments

Comments
 (0)