返回 DeepSeek-Reasonix
baseline_test.go
根目录 / tools / repolint / baseline_test.go
1 package main
2
3 import "testing"
4
5 func budget(files map[string]map[string]int, limits map[string]int) *Baseline {
6 return &Baseline{Limits: limits, Files: files}
7 }
8
9 func TestBaselineAllowsWhatItRecorded(t *testing.T) {
10 b := budget(map[string]map[string]int{"a.go": {ruleEssay: 4}}, map[string]int{ruleEssay: 4})
11 _, msgs := b.exceeded([]Finding{{"a.go", 3, ruleEssay, "", 4}})
12 if len(msgs) != 0 {
13 t.Fatalf("recorded debt reported as new: %v", msgs)
14 }
15 }
16
17 func TestSwappingAShortEssayForALongOneTripsTheRatchet(t *testing.T) {
18 b := budget(map[string]map[string]int{"a.go": {ruleEssay: 1}}, map[string]int{ruleEssay: 1})
19 _, msgs := b.exceeded([]Finding{{"a.go", 3, ruleEssay, "", 27}})
20 if len(msgs) == 0 {
21 t.Fatal("a 27-line-over block replacing a 1-line-over block must fail")
22 }
23 }
24
25 func TestUnbaselinedFileMustBeClean(t *testing.T) {
26 b := budget(map[string]map[string]int{"a.go": {ruleEssay: 9}}, map[string]int{ruleEssay: 9})
27 over, msgs := b.exceeded([]Finding{{"new.go", 1, ruleEssay, "", 1}})
28 if len(msgs) != 1 || len(over) != 1 || over[0].File != "new.go" {
29 t.Fatalf("new file not gated: over=%v msgs=%v", over, msgs)
30 }
31 }
32
33 func TestRepoCeilingCatchesDebtMovedBetweenFiles(t *testing.T) {
34 b := budget(map[string]map[string]int{"a.go": {ruleBanner: 2}, "b.go": {ruleBanner: 2}}, map[string]int{ruleBanner: 2})
35 _, msgs := b.exceeded([]Finding{
36 {"a.go", 1, ruleBanner, "", 1}, {"a.go", 2, ruleBanner, "", 1},
37 {"b.go", 1, ruleBanner, "", 1}, {"b.go", 2, ruleBanner, "", 1},
38 })
39 if len(msgs) != 1 {
40 t.Fatalf("per-file budgets pass but the repo ceiling of 2 must fail: %v", msgs)
41 }
42 }
43
44 func TestBaselineRoundTripsWeights(t *testing.T) {
45 b := baselineFrom([]Finding{
46 {"a.go", 1, ruleEssay, "", 3},
47 {"a.go", 9, ruleEssay, "", 2},
48 {"a.go", 4, ruleBanner, "", 1},
49 })
50 if got := b.Files["a.go"][ruleEssay]; got != 5 {
51 t.Fatalf("essay budget = %d, want 5", got)
52 }
53 if got := b.Limits[ruleEssay]; got != 5 {
54 t.Fatalf("essay ceiling = %d, want 5", got)
55 }
56 if got := b.Limits[ruleBanner]; got != 1 {
57 t.Fatalf("banner ceiling = %d, want 1", got)
58 }
59 }
60
60 lines GO