goc/pkg/cover/cover_test.go
Li Yiyang c2391c5b18
Migrate qbox/goc to qiniu/goc (#8)
* build/install

* all branches

* e2e test

* goc binary

PATH

add checkout code

add +x

test github action

test for github action

test for github actions

test for github actions

test for github actions

fix ci

fix

fix temp path error

* go1.11 1.10 not support go env command

fix

* fix build dir error for go 1.11/1.12 mod project

* e2e test

test

test

fix yaml syntax error

fix

fix

fix

* fix env error

fix

fix

* add unit test

* add unit test coverage report

update

update

* add license
2020-05-21 14:30:41 +08:00

98 lines
3.1 KiB
Go

/*
Copyright 2020 Qiniu Cloud (七牛云)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cover
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func testCoverage() (c *Coverage) {
return &Coverage{FileName: "fake-coverage", NCoveredStmts: 200, NAllStmts: 300}
}
func TestCoverageRatio(t *testing.T) {
c := testCoverage()
actualRatio, _ := c.Ratio()
assert.Equal(t, float32(c.NCoveredStmts)/float32(c.NAllStmts), actualRatio)
}
func TestRatioErr(t *testing.T) {
c := &Coverage{FileName: "fake-coverage", NCoveredStmts: 200, NAllStmts: 0}
_, err := c.Ratio()
assert.NotNil(t, err)
}
func TestPercentageNA(t *testing.T) {
c := &Coverage{FileName: "fake-coverage", NCoveredStmts: 200, NAllStmts: 0}
assert.Equal(t, "N/A", c.Percentage())
}
func TestGenLocalCoverDiffReport(t *testing.T) {
//coverage increase
newList := &CoverageList{Groups: []Coverage{{FileName: "fake-coverage", NCoveredStmts: 15, NAllStmts: 20}}}
baseList := &CoverageList{Groups: []Coverage{{FileName: "fake-coverage", NCoveredStmts: 10, NAllStmts: 20}}}
rows := GenLocalCoverDiffReport(newList, baseList)
assert.Equal(t, 1, len(rows))
assert.Equal(t, []string{"fake-coverage", "50.0%", "75.0%", "25.0%"}, rows[0])
//coverage decrease
baseList = &CoverageList{Groups: []Coverage{{FileName: "fake-coverage", NCoveredStmts: 20, NAllStmts: 20}}}
rows = GenLocalCoverDiffReport(newList, baseList)
assert.Equal(t, []string{"fake-coverage", "100.0%", "75.0%", "-25.0%"}, rows[0])
//diff file
baseList = &CoverageList{Groups: []Coverage{{FileName: "fake-coverage-v1", NCoveredStmts: 10, NAllStmts: 20}}}
rows = GenLocalCoverDiffReport(newList, baseList)
assert.Equal(t, []string{"fake-coverage", "None", "75.0%", "75.0%"}, rows[0])
}
func TestCovList(t *testing.T) {
fileName := "qiniu.com/kodo/apiserver/server/main.go"
// percentage is 100%
p := strings.NewReader("mode: atomic\n" +
fileName + ":32.49,33.13 1 30\n")
covL, err := CovList(p)
covF := covL.Map()[fileName]
assert.Nil(t, err)
assert.Equal(t, "100.0%", covF.Percentage())
// percentage is 50%
p = strings.NewReader("mode: atomic\n" +
fileName + ":32.49,33.13 1 30\n" +
fileName + ":42.49,43.13 1 0\n")
covL, err = CovList(p)
covF = covL.Map()[fileName]
assert.Nil(t, err)
assert.Equal(t, "50.0%", covF.Percentage())
// two files
fileName1 := "qiniu.com/kodo/apiserver/server/svr.go"
p = strings.NewReader("mode: atomic\n" +
fileName + ":32.49,33.13 1 30\n" +
fileName1 + ":42.49,43.13 1 0\n")
covL, err = CovList(p)
covF = covL.Map()[fileName]
covF1 := covL.Map()[fileName1]
assert.Nil(t, err)
assert.Equal(t, "100.0%", covF.Percentage())
assert.Equal(t, "0.0%", covF1.Percentage())
}