Skip to content

Commit

Permalink
Merge pull request #18 from commentcov/sorted_report_output
Browse files Browse the repository at this point in the history
feat: sort the report output
  • Loading branch information
terakoya76 authored Jun 16, 2022
2 parents 4629ee9 + 95e4f6c commit 9033662
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 39 deletions.
14 changes: 10 additions & 4 deletions pkg/report/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/commentcov/commentcov/proto"
)

// scopedCounter holds counters by scope.
type scopedCounter map[string]*Counter
// ScopedCounter holds counters by scope.
type ScopedCounter map[string]*Counter

// Counter is a coverage conuter.
type Counter struct {
Expand All @@ -21,6 +21,12 @@ func NewCounter() *Counter {
}
}

// Merge merges 2 counters into 1.
func (c *Counter) Merge(other *Counter) {
c.Covered = c.Covered + other.Covered
c.Total = c.Total + other.Total
}

// CalcRate returns a rate from the current counter's state.
func (c *Counter) CalcRate() float64 {
var t int
Expand All @@ -33,8 +39,8 @@ func (c *Counter) CalcRate() float64 {
return 100.0 * float64(c.Covered) / float64(t)
}

// Profile returns a rate from the current counter's state.
func (c *Counter) Profile(item *proto.CoverageItem) {
// Add adds the values of the given item.
func (c *Counter) Add(item *proto.CoverageItem) {
if len(item.HeaderComments) > 0 {
c.Covered++
c.Total++
Expand Down
39 changes: 37 additions & 2 deletions pkg/report/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,41 @@ func TestNewCounter(t *testing.T) {
}
}

func TestMerge(t *testing.T) {
tests := []struct {
name string
counter *report.Counter
other *report.Counter
want *report.Counter
}{

{
name: "standard",
counter: &report.Counter{
Covered: 5,
Total: 10,
},
other: &report.Counter{
Covered: 5,
Total: 10,
},
want: &report.Counter{
Covered: 10,
Total: 20,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.counter.Merge(tt.other)
if diff := cmp.Diff(tt.want, tt.counter); diff != "" {
t.Errorf("*Counter values are mismatch (-want +got):%s\n", diff)
}
})
}
}

func TestCalcRate(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -79,7 +114,7 @@ func TestCalcRate(t *testing.T) {
}
}

func TestProfile(t *testing.T) {
func TestAdd(t *testing.T) {
tests := []struct {
name string
counter *report.Counter
Expand Down Expand Up @@ -158,7 +193,7 @@ func TestProfile(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.counter.Profile(tt.item)
tt.counter.Add(tt.item)
got := tt.counter
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("*Counter values are mismatch (-want +got):%s\n", diff)
Expand Down
87 changes: 54 additions & 33 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package report

import (
"fmt"
"sort"

"github.com/commentcov/commentcov/proto"
)
Expand Down Expand Up @@ -50,53 +51,69 @@ func Report(mode Mode, items []*proto.CoverageItem) {

// byFile reports coverage per file.
func byFile(items []*proto.CoverageItem) {
sc := make(scopedCounter)
cc, files, scopes := Profile(items)

for _, item := range items {
scope := item.File
sc := make(ScopedCounter)
for _, file := range files {
sc[file] = NewCounter()

if _, ok := sc[scope]; !ok {
sc[scope] = NewCounter()
for _, scope := range scopes {
if counter, ok := cc[file][scope]; ok {
sc[file].Merge(counter)
}
}

sc[scope].Profile(item)
}

for scope, counter := range sc {
percent := counter.CalcRate()
fmt.Printf("%v: %v\n", scope, percent)
for _, file := range files {
percent := sc[file].CalcRate()
fmt.Printf("%v: %v\n", file, percent)
}
}

// byScope reports coverage by node type.
func byScope(items []*proto.CoverageItem) {
sc := make(scopedCounter)
cc, files, scopes := Profile(items)

unknownScope := proto.CoverageItem_UNKNOWN.String()
for _, item := range items {
scope := item.Scope.String()

if scope == unknownScope {
continue
}
sc := make(ScopedCounter)
for _, scope := range scopes {
sc[scope] = NewCounter()
}

if _, ok := sc[scope]; !ok {
sc[scope] = NewCounter()
for _, file := range files {
for _, scope := range scopes {
if counter, ok := cc[file][scope]; ok {
sc[scope].Merge(counter)
}
}

sc[scope].Profile(item)
}

for scope, counter := range sc {
percent := counter.CalcRate()
for _, scope := range scopes {
percent := sc[scope].CalcRate()
fmt.Printf("%v: %v\n", scope, percent)
}
}

// byFileScope reports coverage by node type per file.
func byFileScope(items []*proto.CoverageItem) {
cc := make(map[string]scopedCounter)
cc, files, scopes := Profile(items)

for _, file := range files {
for _, scope := range scopes {
if counter, ok := cc[file][scope]; ok {
percent := counter.CalcRate()
fmt.Printf("%v,%v: %v\n", file, scope, percent)
}
}
}
}

// Profile converts a list of items into the map of filename:scope:couter.
func Profile(items []*proto.CoverageItem) (map[string]ScopedCounter, []string, []string) {
cc := make(map[string]ScopedCounter)
files := []string{}
scopes := []string{}

scopeMemo := make(map[string]struct{})
unknownScope := proto.CoverageItem_UNKNOWN.String()
for _, item := range items {
scope := item.Scope.String()
Expand All @@ -105,21 +122,25 @@ func byFileScope(items []*proto.CoverageItem) {
continue
}

if _, ok := scopeMemo[scope]; !ok {
scopeMemo[scope] = struct{}{}
scopes = append(scopes, scope)
}

if _, ok := cc[item.File]; !ok {
cc[item.File] = make(scopedCounter)
cc[item.File] = make(ScopedCounter)
files = append(files, item.File)
}

if _, ok := cc[item.File][scope]; !ok {
cc[item.File][scope] = NewCounter()
}

cc[item.File][scope].Profile(item)
cc[item.File][scope].Add(item)
}

for file, sc := range cc {
for scope, counter := range sc {
percent := counter.CalcRate()
fmt.Printf("%v,%v: %v\n", file, scope, percent)
}
}
sort.Strings(files)
sort.Strings(scopes)

return cc, files, scopes
}
Loading

0 comments on commit 9033662

Please sign in to comment.