Skip to content

Commit f23418d

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Flow analysis: Refactor some common unit testing code
Change-Id: I227bc09ed8e0c2161b5de9a06e70089b5f623b65 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/109840 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent c8587ce commit f23418d

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

pkg/analyzer/test/src/dart/resolution/flow_analysis_unit_test.dart

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,59 @@ import 'package:test/test.dart';
88
main() {
99
group('API', () {
1010
test('conditionNotEqNull promotes true branch', () {
11-
var flow = _Harness().flow;
12-
var x = _Var('x', _Type('int?'));
13-
flow.add(x, assigned: true);
11+
var h = _Harness();
12+
var x = h.addAssignedVar('x', 'int?');
1413
var expr = _Expression();
15-
flow.conditionNotEqNull(expr, x);
16-
flow.ifStatement_thenBegin(expr);
17-
expect(flow.promotedType(x).type, 'int');
18-
flow.ifStatement_elseBegin();
19-
expect(flow.promotedType(x), isNull);
20-
flow.ifStatement_end(true);
21-
flow.verifyStackEmpty();
14+
h.flow.conditionNotEqNull(expr, x);
15+
h.flow.ifStatement_thenBegin(expr);
16+
expect(h.flow.promotedType(x).type, 'int');
17+
h.flow.ifStatement_elseBegin();
18+
expect(h.flow.promotedType(x), isNull);
19+
h.flow.ifStatement_end(true);
20+
h.flow.verifyStackEmpty();
2221
});
2322

2423
test('conditionEqNull promotes false branch', () {
25-
var flow = _Harness().flow;
26-
var x = _Var('x', _Type('int?'));
27-
flow.add(x, assigned: true);
24+
var h = _Harness();
25+
var x = h.addAssignedVar('x', 'int?');
2826
var expr = _Expression();
29-
flow.conditionEqNull(expr, x);
30-
flow.ifStatement_thenBegin(expr);
31-
expect(flow.promotedType(x), isNull);
32-
flow.ifStatement_elseBegin();
33-
expect(flow.promotedType(x).type, 'int');
34-
flow.ifStatement_end(true);
35-
flow.verifyStackEmpty();
27+
h.flow.conditionEqNull(expr, x);
28+
h.flow.ifStatement_thenBegin(expr);
29+
expect(h.flow.promotedType(x), isNull);
30+
h.flow.ifStatement_elseBegin();
31+
expect(h.flow.promotedType(x).type, 'int');
32+
h.flow.ifStatement_end(true);
33+
h.flow.verifyStackEmpty();
3634
});
3735

3836
test('ifStatement_end(false) keeps else branch if then branch exits', () {
39-
var flow = _Harness().flow;
40-
var x = _Var('x', _Type('int?'));
41-
flow.add(x, assigned: true);
37+
var h = _Harness();
38+
var x = h.addAssignedVar('x', 'int?');
4239
var expr = _Expression();
43-
flow.conditionEqNull(expr, x);
44-
flow.ifStatement_thenBegin(expr);
45-
flow.handleExit();
46-
flow.ifStatement_end(false);
47-
expect(flow.promotedType(x).type, 'int');
48-
flow.verifyStackEmpty();
40+
h.flow.conditionEqNull(expr, x);
41+
h.flow.ifStatement_thenBegin(expr);
42+
h.flow.handleExit();
43+
h.flow.ifStatement_end(false);
44+
expect(h.flow.promotedType(x).type, 'int');
45+
h.flow.verifyStackEmpty();
4946
});
5047

5148
void _checkIs(String declaredType, String tryPromoteType,
5249
String expectedPromotedType) {
53-
var flow = _Harness().flow;
54-
var x = _Var('x', _Type(declaredType));
55-
flow.add(x, assigned: true);
50+
var h = _Harness();
51+
var x = h.addAssignedVar('x', 'int?');
5652
var expr = _Expression();
57-
flow.isExpression_end(expr, x, false, _Type(tryPromoteType));
58-
flow.ifStatement_thenBegin(expr);
53+
h.flow.isExpression_end(expr, x, false, _Type(tryPromoteType));
54+
h.flow.ifStatement_thenBegin(expr);
5955
if (expectedPromotedType == null) {
60-
expect(flow.promotedType(x), isNull);
56+
expect(h.flow.promotedType(x), isNull);
6157
} else {
62-
expect(flow.promotedType(x).type, 'int');
58+
expect(h.flow.promotedType(x).type, 'int');
6359
}
64-
flow.ifStatement_elseBegin();
65-
expect(flow.promotedType(x), isNull);
66-
flow.ifStatement_end(true);
67-
flow.verifyStackEmpty();
60+
h.flow.ifStatement_elseBegin();
61+
expect(h.flow.promotedType(x), isNull);
62+
h.flow.ifStatement_end(true);
63+
h.flow.verifyStackEmpty();
6864
}
6965

7066
test('isExpression_end promotes to a subtype', () {
@@ -161,6 +157,12 @@ class _Harness
161157
flow = FlowAnalysis<_Statement, _Expression, _Var, _Type>(this, this, this);
162158
}
163159

160+
_Var addAssignedVar(String name, String type) {
161+
var v = _Var(name, _Type(type));
162+
flow.add(v, assigned: true);
163+
return v;
164+
}
165+
164166
@override
165167
bool isLocalVariable(_Var variable) {
166168
throw UnimplementedError('TODO(paulberry)');
@@ -189,6 +191,7 @@ class _Harness
189191
'int <: String': false,
190192
'int? <: int': false,
191193
'String <: int': false,
194+
'String <: int?': false,
192195
};
193196

194197
if (leftType.type == rightType.type) return true;

0 commit comments

Comments
 (0)