| 1 |
package check |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"slices" |
| 8 |
"strings" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
type counter struct { |
| 14 |
name string |
| 15 |
value int |
| 16 |
force bool |
| 17 |
color string |
| 18 |
size int |
| 19 |
} |
| 20 |
|
| 21 |
func (c counter) String() (s string) { |
| 22 |
if c.value != 0 || c.force { |
| 23 |
color := c.color |
| 24 |
if c.value == 0 { |
| 25 |
color = ansiReset |
| 26 |
} |
| 27 |
s = fmt.Sprintf("%s%*d %s%s", color, c.size, c.value, c.name, ansiReset) |
| 28 |
} else { |
| 29 |
s = strings.Repeat(" ", c.size+1+len(c.name)) |
| 30 |
} |
| 31 |
return s |
| 32 |
} |
| 33 |
|
| 34 |
type testStat struct { |
| 35 |
name string |
| 36 |
passed counter |
| 37 |
forged counter |
| 38 |
failed counter |
| 39 |
} |
| 40 |
|
| 41 |
func newTestStat(desc string, force bool) *testStat { |
| 42 |
return &testStat{ |
| 43 |
name: desc, |
| 44 |
passed: counter{force: force, name: "passed", color: ansiGreen}, |
| 45 |
forged: counter{force: force, name: "todo", color: ansiYellow}, |
| 46 |
failed: counter{force: force, name: "failed", color: ansiRed}, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func (c testStat) String() string { |
| 51 |
return fmt.Sprintf("checks: %s %s %s\t%s", c.passed, c.forged, c.failed, c.name) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
var ( |
| 56 |
statsMu sync.Mutex |
| 57 |
stats = make(map[testing.TB]*testStat) |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func Report() { |
| 74 |
statsMu.Lock() |
| 75 |
defer statsMu.Unlock() |
| 76 |
|
| 77 |
total := newTestStat("(total)", true) |
| 78 |
ts := make([]testing.TB, 0, len(stats)) |
| 79 |
for t := range stats { |
| 80 |
ts = append(ts, t) |
| 81 |
total.passed.value += stats[t].passed.value |
| 82 |
total.forged.value += stats[t].forged.value |
| 83 |
total.failed.value += stats[t].failed.value |
| 84 |
} |
| 85 |
|
| 86 |
total.passed.size = digits(total.passed.value) |
| 87 |
total.forged.size = digits(total.forged.value) |
| 88 |
total.failed.size = digits(total.failed.value) |
| 89 |
|
| 90 |
if testing.Verbose() { |
| 91 |
slices.SortFunc(ts, func(a, b testing.TB) int { |
| 92 |
return cmp.Compare(a.Name(), b.Name()) |
| 93 |
}) |
| 94 |
for _, t := range ts { |
| 95 |
stats[t].passed.size = total.passed.size |
| 96 |
stats[t].forged.size = total.forged.size |
| 97 |
stats[t].failed.size = total.failed.size |
| 98 |
fmt.Fprintf(os.Stderr, " %s\n", stats[t]) |
| 99 |
} |
| 100 |
} |
| 101 |
fmt.Fprintf(os.Stderr, " %s\n", total) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func TestMain(m *testing.M) { |
| 113 |
code := m.Run() |
| 114 |
Report() |
| 115 |
os.Exit(code) |
| 116 |
} |