add case for filterProfile func
This commit is contained in:
parent
ca15b18860
commit
d648bef19e
@ -187,7 +187,7 @@ func profile(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// filterProfile output profiles of the packages matching the coverPkg
|
// filterProfile filters profiles of the packages matching the coverPkg
|
||||||
func filterProfile(coverPkg []string, profiles []*cover.Profile) ([]*cover.Profile, error) {
|
func filterProfile(coverPkg []string, profiles []*cover.Profile) ([]*cover.Profile, error) {
|
||||||
var out = make([]*cover.Profile, 0)
|
var out = make([]*cover.Profile, 0)
|
||||||
|
|
||||||
|
@ -6,11 +6,13 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
|
"golang.org/x/tools/cover"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MockStore is mock store mainly for unittest
|
// MockStore is mock store mainly for unittest
|
||||||
@ -216,10 +218,77 @@ func TestInitService(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterProfile(t *testing.T) {
|
func TestFilterProfile(t *testing.T) {
|
||||||
// var tcs = []struct {
|
var tcs = []struct {
|
||||||
// pattern []string
|
name string
|
||||||
// profile []*cover.Profile
|
pattern []string
|
||||||
// expected []*cover.Profile
|
input []*cover.Profile
|
||||||
// err error
|
output []*cover.Profile
|
||||||
// }{}
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid test",
|
||||||
|
pattern: []string{"some/fancy/gopath", "a/fancy/gopath"},
|
||||||
|
input: []*cover.Profile{
|
||||||
|
{
|
||||||
|
FileName: "some/fancy/gopath/a.go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FileName: "some/fancy/gopath/b/a.go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FileName: "a/fancy/gopath/a.go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FileName: "b/fancy/gopath/a.go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FileName: "b/a/fancy/gopath/a.go",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output: []*cover.Profile{
|
||||||
|
{
|
||||||
|
FileName: "some/fancy/gopath/a.go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FileName: "some/fancy/gopath/b/a.go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FileName: "a/fancy/gopath/a.go",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
FileName: "b/a/fancy/gopath/a.go",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tcs {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
out, err := filterProfile(tc.pattern, tc.input)
|
||||||
|
if err != nil {
|
||||||
|
if !tc.expectErr {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.expectErr {
|
||||||
|
t.Errorf("Expected an error, but got value %s", stringifyCoverProfile(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(out, tc.output) {
|
||||||
|
t.Errorf("Mismatched results. \nExpected: %s\nActual:%s", stringifyCoverProfile(tc.output), stringifyCoverProfile(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringifyCoverProfile(profiles []*cover.Profile) string {
|
||||||
|
res := make([]cover.Profile, 0, len(profiles))
|
||||||
|
for _, p := range profiles {
|
||||||
|
res = append(res, *p)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%#v", res)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user