Adds `make coverage` (per-package + total via -coverpkg=./...), `make coverage-html`, and `make coverage-total` (CI-friendly). Wires coverage.out/coverage.html through `make clean` and .gitignore. Closes the two easy zero-coverage packages: namegen (77.8%) and sessionstream (93.5%). Total statement coverage 51.7% -> 52.1%.
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package namegen
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
adjSet := make(map[string]struct{}, len(adjectives))
|
|
for _, a := range adjectives {
|
|
adjSet[a] = struct{}{}
|
|
}
|
|
subSet := make(map[string]struct{}, len(substantives))
|
|
for _, s := range substantives {
|
|
subSet[s] = struct{}{}
|
|
}
|
|
|
|
seen := make(map[string]int)
|
|
for i := 0; i < 200; i++ {
|
|
name := Generate()
|
|
parts := strings.Split(name, "-")
|
|
if len(parts) != 2 {
|
|
t.Fatalf("expected adj-noun form, got %q", name)
|
|
}
|
|
if _, ok := adjSet[parts[0]]; !ok {
|
|
t.Fatalf("unknown adjective %q in %q", parts[0], name)
|
|
}
|
|
if _, ok := subSet[parts[1]]; !ok {
|
|
t.Fatalf("unknown substantive %q in %q", parts[1], name)
|
|
}
|
|
seen[name]++
|
|
}
|
|
|
|
// Minimal variety check: adj-noun cartesian product is thousands of
|
|
// combinations; 200 draws should hit more than a couple.
|
|
if len(seen) < 10 {
|
|
t.Fatalf("expected varied output, only saw %d distinct names", len(seen))
|
|
}
|
|
}
|
|
|
|
func TestRandomIndex(t *testing.T) {
|
|
if got := randomIndex(0); got != 0 {
|
|
t.Fatalf("randomIndex(0) = %d, want 0", got)
|
|
}
|
|
if got := randomIndex(1); got != 0 {
|
|
t.Fatalf("randomIndex(1) = %d, want 0", got)
|
|
}
|
|
for i := 0; i < 100; i++ {
|
|
n := randomIndex(7)
|
|
if n < 0 || n >= 7 {
|
|
t.Fatalf("randomIndex(7) = %d, out of range", n)
|
|
}
|
|
}
|
|
}
|