Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit d68ebff

Browse files
committed
Use skips instead of ifs in tests
1 parent 3bd79c3 commit d68ebff

File tree

3 files changed

+119
-38
lines changed

3 files changed

+119
-38
lines changed

test/collect_coverage_api_test.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,12 @@ void main() {
114114
return map;
115115
});
116116

117-
if (platformVersionCheck(2, 17)) {
118-
// Dart VM versions before 2.17 don't support branch coverage.
119-
for (var sampleCoverageData in sources[_sampleAppFileUri]!) {
120-
expect(sampleCoverageData['branchHits'], isNotEmpty);
121-
}
122-
123-
for (var sampleCoverageData in sources[_isolateLibFileUri]!) {
124-
expect(sampleCoverageData['branchHits'], isNotEmpty);
125-
}
126-
}
127-
});
117+
// Dart VM versions before 2.17 don't support branch coverage.
118+
expect(sources[_sampleAppFileUri],
119+
everyElement(containsPair('branchHits', isNotEmpty)));
120+
expect(sources[_isolateLibFileUri],
121+
everyElement(containsPair('branchHits', isNotEmpty)));
122+
}, skip: !platformVersionCheck(2, 17));
128123
}
129124

130125
Future<Map<String, dynamic>> _collectCoverage(

test/collect_coverage_test.dart

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,78 @@ void main() {
179179
28: 'fooAsync',
180180
38: 'isolateTask'
181181
});
182-
if (platformVersionCheck(2, 17)) {
183-
// Dart VM versions before 2.17 don't support branch coverage.
184-
expect(isolateFile?.branchHits,
185-
{11: 1, 12: 1, 15: 0, 19: 1, 23: 1, 28: 1, 32: 0, 38: 1, 42: 1});
182+
expect(isolateFile?.branchHits,
183+
{11: 1, 12: 1, 15: 0, 19: 1, 23: 1, 28: 1, 32: 0, 38: 1, 42: 1});
184+
}, skip: !platformVersionCheck(2, 17));
185+
186+
test('HitMap.parseJson, old VM without branch coverage', () async {
187+
final resultString = await _collectCoverage(true, true);
188+
final jsonResult = json.decode(resultString) as Map<String, dynamic>;
189+
final coverage = jsonResult['coverage'] as List;
190+
final hitMap = await HitMap.parseJson(
191+
coverage.cast<Map<String, dynamic>>(),
192+
);
193+
expect(hitMap, contains(_sampleAppFileUri));
194+
195+
final isolateFile = hitMap[_isolateLibFileUri];
196+
final expectedHits = {
197+
11: 1,
198+
12: 1,
199+
13: 1,
200+
15: 0,
201+
19: 1,
202+
23: 1,
203+
24: 2,
204+
28: 1,
205+
29: 1,
206+
30: 1,
207+
32: 0,
208+
38: 1,
209+
39: 1,
210+
41: 1,
211+
42: 3,
212+
43: 1,
213+
44: 3,
214+
45: 1,
215+
48: 1,
216+
49: 1,
217+
51: 1,
218+
54: 1,
219+
55: 1,
220+
56: 1,
221+
59: 1,
222+
60: 1,
223+
62: 1,
224+
63: 1,
225+
64: 1,
226+
66: 1,
227+
67: 1,
228+
68: 1
229+
};
230+
if (Platform.version.startsWith('1.')) {
231+
// Dart VMs prior to 2.0.0-dev.5.0 contain a bug that emits coverage on the
232+
// closing brace of async function blocks.
233+
// See: https://github.com/dart-lang/coverage/issues/196
234+
expectedHits[23] = 0;
235+
} else {
236+
// Dart VMs version 2.0.0-dev.6.0 mark the opening brace of a function as
237+
// coverable.
238+
expectedHits[11] = 1;
239+
expectedHits[28] = 1;
240+
expectedHits[38] = 1;
241+
expectedHits[42] = 3;
186242
}
187-
});
243+
expect(isolateFile?.lineHits, expectedHits);
244+
expect(isolateFile?.funcHits, {11: 1, 19: 1, 21: 0, 23: 1, 28: 1, 38: 1});
245+
expect(isolateFile?.funcNames, {
246+
11: 'fooSync',
247+
19: 'BarClass.BarClass',
248+
21: 'BarClass.x=',
249+
23: 'BarClass.baz',
250+
28: 'fooAsync',
251+
38: 'isolateTask'
252+
});
253+
}, skip: platformVersionCheck(2, 17));
188254

189255
test('parseCoverage', () async {
190256
final tempDir = await Directory.systemTemp.createTemp('coverage.test.');

test/lcov_test.dart

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,35 @@ void main() {
4545
reason: 'be careful if you modify the test file');
4646
expect(sampleAppFuncNames, containsPair(45, 'usedMethod'),
4747
reason: 'be careful if you modify the test file');
48-
if (platformVersionCheck(2, 17)) {
49-
// Dart VM versions before 2.17 don't support branch coverage.
50-
expect(sampleAppBranchHits, containsPair(41, 1),
51-
reason: 'be careful if you modify the test file');
52-
}
53-
});
48+
expect(sampleAppBranchHits, containsPair(41, 1),
49+
reason: 'be careful if you modify the test file');
50+
}, skip: !platformVersionCheck(2, 17));
51+
52+
test('validate hitMap, old VM without branch coverage', () async {
53+
final hitmap = await _getHitMap();
54+
55+
expect(hitmap, contains(_sampleAppFileUri));
56+
expect(hitmap, contains(_isolateLibFileUri));
57+
expect(hitmap, contains('package:coverage/src/util.dart'));
58+
59+
final sampleAppHitMap = hitmap[_sampleAppFileUri];
60+
final sampleAppHitLines = sampleAppHitMap?.lineHits;
61+
final sampleAppHitFuncs = sampleAppHitMap?.funcHits;
62+
final sampleAppFuncNames = sampleAppHitMap?.funcNames;
63+
64+
expect(sampleAppHitLines, containsPair(46, greaterThanOrEqualTo(1)),
65+
reason: 'be careful if you modify the test file');
66+
expect(sampleAppHitLines, containsPair(50, 0),
67+
reason: 'be careful if you modify the test file');
68+
expect(sampleAppHitLines, isNot(contains(32)),
69+
reason: 'be careful if you modify the test file');
70+
expect(sampleAppHitFuncs, containsPair(45, 1),
71+
reason: 'be careful if you modify the test file');
72+
expect(sampleAppHitFuncs, containsPair(49, 0),
73+
reason: 'be careful if you modify the test file');
74+
expect(sampleAppFuncNames, containsPair(45, 'usedMethod'),
75+
reason: 'be careful if you modify the test file');
76+
}, skip: platformVersionCheck(2, 17));
5477

5578
group('LcovFormatter', () {
5679
test('format()', () async {
@@ -206,25 +229,22 @@ void main() {
206229
expect(res, contains(' | return a + b;'));
207230
});
208231

209-
if (platformVersionCheck(2, 17)) {
210-
// Dart VM versions before 2.17 don't support branch coverage.
211-
test('prettyPrint() branches', () async {
212-
final hitmap = await _getHitMap();
232+
test('prettyPrint() branches', () async {
233+
final hitmap = await _getHitMap();
213234

214-
final resolver = Resolver(packagesPath: '.packages');
215-
final res =
216-
await hitmap.prettyPrint(resolver, Loader(), reportBranches: true);
235+
final resolver = Resolver(packagesPath: '.packages');
236+
final res =
237+
await hitmap.prettyPrint(resolver, Loader(), reportBranches: true);
217238

218-
expect(res, contains(p.absolute(_sampleAppPath)));
219-
expect(res, contains(p.absolute(_isolateLibPath)));
220-
expect(res, contains(p.absolute(p.join('lib', 'src', 'util.dart'))));
239+
expect(res, contains(p.absolute(_sampleAppPath)));
240+
expect(res, contains(p.absolute(_isolateLibPath)));
241+
expect(res, contains(p.absolute(p.join('lib', 'src', 'util.dart'))));
221242

222-
// be very careful if you change the test file
223-
expect(res, contains(' 1| if (x == answer) {'));
224-
expect(res, contains(' 0| while (i < lines.length) {'));
225-
expect(res, contains(' | bar.baz();'));
226-
});
227-
}
243+
// be very careful if you change the test file
244+
expect(res, contains(' 1| if (x == answer) {'));
245+
expect(res, contains(' 0| while (i < lines.length) {'));
246+
expect(res, contains(' | bar.baz();'));
247+
}, skip: !platformVersionCheck(2, 17));
228248
});
229249
}
230250

0 commit comments

Comments
 (0)