| | | 1 | | package domain |
| | | 2 | | |
| | | 3 | | // FileCoverage holds line-level execution counts for one source file. |
| | | 4 | | // Lines is a map from 1-based line number to the number of times that line |
| | | 5 | | // was executed during the test run. |
| | | 6 | | type FileCoverage struct { |
| | | 7 | | // Path is the file path as reported by Lua's debug.getinfo, typically |
| | | 8 | | // relative to the project root. |
| | | 9 | | Path string |
| | | 10 | | // Lines maps 1-based line numbers to execution counts. |
| | | 11 | | Lines map[int]int |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | // HitLines returns the number of lines executed at least once. |
| | | 15 | | func (f *FileCoverage) HitLines() int { |
| | | 16 | | count := 0 |
| | | 17 | | for _, hits := range f.Lines { |
| | | 18 | | if hits > 0 { |
| | | 19 | | count++ |
| | | 20 | | } |
| | | 21 | | } |
| | | 22 | | return count |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | // TotalLines returns the number of instrumented lines. |
| | | 26 | | func (f *FileCoverage) TotalLines() int { |
| | | 27 | | return len(f.Lines) |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | // Percentage returns the line coverage percentage for this file, or 0 if there |
| | | 31 | | // are no instrumented lines. |
| | | 32 | | func (f *FileCoverage) Percentage() float64 { |
| | | 33 | | total := f.TotalLines() |
| | | 34 | | if total == 0 { |
| | | 35 | | return 0 |
| | | 36 | | } |
| | | 37 | | return float64(f.HitLines()) / float64(total) * 100 |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // CoverageData is the aggregated coverage for an entire test run. |
| | | 41 | | type CoverageData struct { |
| | | 42 | | Files []*FileCoverage |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | // TotalLines returns the total number of instrumented lines across all files. |
| | 3 | 46 | | func (c *CoverageData) TotalLines() int { |
| | 3 | 47 | | n := 0 |
| | 3 | 48 | | for _, f := range c.Files { |
| | 4 | 49 | | n += f.TotalLines() |
| | 4 | 50 | | } |
| | 3 | 51 | | return n |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // HitLines returns the total number of lines hit at least once across all files. |
| | 2 | 55 | | func (c *CoverageData) HitLines() int { |
| | 2 | 56 | | n := 0 |
| | 2 | 57 | | for _, f := range c.Files { |
| | 4 | 58 | | n += f.HitLines() |
| | 4 | 59 | | } |
| | 2 | 60 | | return n |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // Percentage returns the overall line coverage percentage across all files, |
| | | 64 | | // or 0 if no lines are instrumented. |
| | 2 | 65 | | func (c *CoverageData) Percentage() float64 { |
| | 2 | 66 | | total := c.TotalLines() |
| | 1 | 67 | | if total == 0 { |
| | 1 | 68 | | return 0 |
| | 1 | 69 | | } |
| | 1 | 70 | | return float64(c.HitLines()) / float64(total) * 100 |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // FileByPath returns the FileCoverage for a given path, or nil. |
| | 2 | 74 | | func (c *CoverageData) FileByPath(path string) *FileCoverage { |
| | 2 | 75 | | for _, f := range c.Files { |
| | 1 | 76 | | if f.Path == path { |
| | 1 | 77 | | return f |
| | 1 | 78 | | } |
| | | 79 | | } |
| | 1 | 80 | | return nil |
| | | 81 | | } |