Skip to content

Commit 3a1eba8

Browse files
Modify test_with_coverage_test to also test for branch hits in coverage percentage
Signed-off-by: vvs-personalstash <[email protected]>
1 parent 86a8eb9 commit 3a1eba8

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

pkgs/coverage/test/test_with_coverage_package/lib/validate_lib.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ int product(Iterable<int> values) {
1717
}
1818
return val;
1919
}
20+
21+
String evaluateScore(int score) {
22+
if (score < 0) {
23+
return 'Invalid';
24+
} else if (score < 50) {
25+
return 'Fail';
26+
} else if (score < 70) {
27+
return 'Pass';
28+
} else if (score <= 100) {
29+
return 'Excellent';
30+
} else {
31+
return 'Overflow';
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2022, 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 '../lib/validate_lib.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('evaluateScore', () {
10+
test('returns Invalid for negative score', () {
11+
expect(evaluateScore(-10), equals('Invalid'));
12+
});
13+
14+
test('returns Fail for score < 50', () {
15+
expect(evaluateScore(30), equals('Fail'));
16+
});
17+
18+
test('returns Excellent for score 85', () {
19+
expect(evaluateScore(85), equals('Excellent'));
20+
});
21+
});
22+
}

pkgs/coverage/test/test_with_coverage_test.dart

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ dependency_overrides:
8686
{
8787
'product': 1,
8888
'sum': 1,
89+
'evaluateScore': 1,
8990
},
9091
);
9192
});
@@ -104,6 +105,7 @@ dependency_overrides:
104105
{
105106
'product': 0,
106107
'sum': 1,
108+
'evaluateScore': 0,
107109
},
108110
reason: 'only `sum` tests should be run',
109111
);
@@ -129,29 +131,53 @@ dependency_overrides:
129131
final process = await _run([
130132
'run',
131133
_testWithCoveragePath,
132-
'-f',
133-
'--fail-under=50', // This should pass as coverage=100% when all tests run
134+
'--fail-under=100', // This should pass as coverage=100% when all tests run
134135
'--port',
135136
'${_port++}',
136137
]);
137138
await process.shouldExit(0);
138139
});
139-
140140
test(
141141
'dart run bin/test_with_coverage.dart --fail-under fails when coverage is below threshold',
142142
() async {
143143
final process = await _run([
144144
'run',
145145
_testWithCoveragePath,
146-
'--fail-under=90', // This should throw an exit(1) as coverage =50% when
147-
'--port', // only the `sum` test is run
148-
'${_port++}',
146+
'--fail-under=90', // This should throw an exit(1) as coverage =27.27% when
147+
'--port', // only the `sum` test is run i.e. out of 11 lines only 3 lines
148+
'${_port++}', //i.e. [5,7,8]will have hits>0
149149
'--',
150150
'-N',
151151
'sum',
152152
]);
153153
await process.shouldExit(1);
154154
});
155+
test(
156+
'dart run bin/test_with_coverage.dart -b --fail-under succeeds when coverage meets threshold',
157+
() async {
158+
final process = await _run([
159+
'run',
160+
_testWithCoveragePath,
161+
'-b',
162+
'--fail-under=95', // This should pass as total lines+branches covered=20
163+
'--port', // and total lines (11)+branches covered(10)=21 => percentage_covered=95.23
164+
'${_port++}',
165+
]);
166+
await process.shouldExit(0);
167+
});
168+
test(
169+
'dart run bin/test_with_coverage.dart -b --fail-under fails when coverage is below threshold',
170+
() async {
171+
final process = await _run([
172+
'run',
173+
_testWithCoveragePath,
174+
'-b',
175+
'--fail-under=96', // This should throw an exit(1) as percentage_covered=95.24 approx
176+
'--port',
177+
'${_port++}',
178+
]);
179+
await process.shouldExit(1);
180+
});
155181
}
156182

157183
Future<TestProcess> _run(List<String> args) => TestProcess.start(

0 commit comments

Comments
 (0)