Skip to content

Commit

Permalink
Merge pull request #2386 from Build-Squad/coverage-report-add-reset-m…
Browse files Browse the repository at this point in the history
…ethod
  • Loading branch information
turbolent authored Mar 14, 2023
2 parents e9451ba + 2ede447 commit 2e80137
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions runtime/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ func (r *CoverageReport) CoveredStatementsPercentage() string {
return fmt.Sprintf("Coverage: %v of statements", percentage)
}

// Reset flushes the collected coverage information for all locations
// and inspected programs. Excluded locations remain intact.
func (r *CoverageReport) Reset() {
for location := range r.Coverage { // nolint:maprange
delete(r.Coverage, location)
}
for program := range r.Programs { // nolint:maprange
delete(r.Programs, program)
}
}

// NewCoverageReport creates and returns a *CoverageReport.
func NewCoverageReport() *CoverageReport {
return &CoverageReport{
Expand Down
43 changes: 43 additions & 0 deletions runtime/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,49 @@ func TestCoverageReportWithAddressLocation(t *testing.T) {
}
`
require.JSONEq(t, expected, string(actual))

}
func TestCoverageReportReset(t *testing.T) {

t.Parallel()

script := []byte(`
pub fun answer(): Int {
var i = 0
while i < 42 {
i = i + 1
}
return i
}
`)

program, err := parser.ParseProgram(nil, script, parser.Config{})
require.NoError(t, err)

coverageReport := NewCoverageReport()

location := common.StringLocation("AnswerScript")
coverageReport.InspectProgram(location, program)
coverageReport.AddLineHit(location, 3)
coverageReport.AddLineHit(location, 3)
coverageReport.AddLineHit(location, 5)

excludedLocation := common.StringLocation("XLocation")
coverageReport.ExcludeLocation(excludedLocation)

assert.Equal(t, 1, len(coverageReport.Coverage))
assert.Equal(t, 1, len(coverageReport.Programs))
assert.Equal(t, 1, len(coverageReport.ExcludedLocations))
assert.Equal(t, true, coverageReport.IsProgramInspected(location))
assert.Equal(t, true, coverageReport.IsLocationExcluded(excludedLocation))

coverageReport.Reset()

assert.Equal(t, 0, len(coverageReport.Coverage))
assert.Equal(t, 0, len(coverageReport.Programs))
assert.Equal(t, 1, len(coverageReport.ExcludedLocations))
assert.Equal(t, false, coverageReport.IsProgramInspected(location))
assert.Equal(t, true, coverageReport.IsLocationExcluded(excludedLocation))
}

func TestCoverageReportAddLineHitForExcludedLocation(t *testing.T) {
Expand Down

0 comments on commit 2e80137

Please sign in to comment.