Skip to content

Commit 8e47c8e

Browse files
authored
fix: add rounding of percentage (#145)
1 parent d67bcce commit 8e47c8e

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

pkg/testcoverage/coverage/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"math"
78
"regexp"
89
"strconv"
910
"strings"
@@ -64,7 +65,10 @@ func coveredPercentageF(total, covered int64) float64 {
6465
return 100
6566
}
6667

67-
return float64(covered*100) / float64(total)
68+
p := float64(covered*100) / float64(total)
69+
70+
// round to %.1f
71+
return float64(int(math.Round(p*10))) / 10
6872
}
6973

7074
func stripPrefix(name, prefix string) string {

pkg/testcoverage/coverage/types_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestCoveredPercentage(t *testing.T) {
2222
{percentage: 10, total: 10, covered: 1},
2323
{percentage: 22, total: 9, covered: 2}, // 22.222.. should round down to 22
2424
{percentage: 66, total: 9, covered: 6}, // 66.666.. should round down to 66
25+
{percentage: 73, total: 274, covered: 200},
2526
}
2627

2728
for _, tc := range tests {
@@ -35,6 +36,7 @@ func TestStatStr(t *testing.T) {
3536
assert.Equal(t, " 0.0% (0/0)", Stats{}.Str())
3637
assert.Equal(t, " 9.1% (1/11)", Stats{Covered: 1, Total: 11}.Str())
3738
assert.Equal(t, "22.2% (2/9)", Stats{Covered: 2, Total: 9}.Str())
39+
assert.Equal(t, "73.0% (200/274)", Stats{Covered: 200, Total: 274}.Str())
3840
assert.Equal(t, "100% (10/10)", Stats{Covered: 10, Total: 10}.Str())
3941
}
4042

0 commit comments

Comments
 (0)