package app import ( "strings" "testing" "github.com/egoist/mygo/ui" ) func themeForTest() *ui.Theme { return ui.DarkTheme() } const sampleProfile = `mode: set testdemo/calc.go:5.13,7.2 1 1 testdemo/calc.go:10.6,14.2 3 0 testdemo/util.go:3.20,5.2 2 5 ` // TestParseCoverProfile 解析 coverprofile 的块与统计。 func TestParseCoverProfile(t *testing.T) { files := parseCoverProfile(sampleProfile) if len(files) != 2 { t.Fatalf("应解析出 2 个文件,实际 %d: %v", len(files), files) } calc := files["testdemo/calc.go"] if len(calc.Blocks) != 2 { t.Fatalf("calc.go 应有 2 个块,实际 %d", len(calc.Blocks)) } // 一块命中(1 语句)、一块没命中(3 语句) → 1/4 if calc.Total != 4 || calc.Hit != 1 { t.Fatalf("统计不对: total=%d hit=%d", calc.Total, calc.Hit) } if got := calc.percent(); got != 25 { t.Fatalf("覆盖率应为 25%%,实际 %v", got) } if b := calc.Blocks[0]; b.Start != 5 || b.End != 7 || b.Count != 1 { t.Fatalf("块解析不对: %+v", b) } } // TestParseCoverProfileSkipsMode 第一行 mode 要跳过,空行也不能算块。 func TestParseCoverProfileSkipsMode(t *testing.T) { files := parseCoverProfile("mode: atomic\n\n") if len(files) != 0 { t.Fatalf("没有块时应为空: %v", files) } } // TestCoverForSuffix 覆盖报告的路径可能带模块前缀,要能按后缀对上。 func TestCoverForSuffix(t *testing.T) { a := &App{} a.cover.files = parseCoverProfile(sampleProfile) if _, ok := a.coverFor(`D:\WorkSpaces\testdemo\calc.go`); !ok { t.Fatal("应该按「包目录/文件名」后缀匹配上") } if _, ok := a.coverFor(`D:\WorkSpaces\other\calc.go`); ok { t.Fatal("不同目录的同名文件不该匹配") } } // TestCoverBackgrounds 命中的行绿、没命中的行红,且行号从 0 起。 func TestCoverBackgrounds(t *testing.T) { a := &App{th: themeForTest()} a.cover.files = parseCoverProfile(`mode: set testdemo/calc.go:3.1,3.10 1 1 testdemo/calc.go:4.1,4.10 1 0 `) d := a.newDoc(`D:\WorkSpaces\testdemo\calc.go`, "a\nb\nc\nd\n") bgs := a.coverBackgrounds(d) if len(bgs) != 2 { t.Fatalf("应有 2 行底色,实际 %d", len(bgs)) } byLine := map[int]int{} for _, bg := range bgs { byLine[bg.First] = 1 } if _, ok := byLine[2]; !ok { t.Fatalf("第 3 行(0 起是 2)应有着色: %+v", bgs) } if _, ok := byLine[3]; !ok { t.Fatalf("第 4 行(0 起是 3)应有着色: %+v", bgs) } } // TestCoverStatText 状态栏摘要。 func TestCoverStatText(t *testing.T) { a := &App{} a.cover.files = parseCoverProfile(sampleProfile) if got := a.coverStatText(`D:\WorkSpaces\testdemo\calc.go`); got != "覆盖 25%" { t.Fatalf("摘要不对: %q", got) } if got := a.coverStatText(`D:\WorkSpaces\testdemo\none.go`); got != "" { t.Fatalf("没有数据的文件不该显示: %q", got) } } // sampleFailOut 是一段真实的 `go test ./...` 失败输出(混合了成功包)。 const sampleFailOut = `ok testdemo/util 0.004s --- FAIL: TestAdd (0.00s) calc_test.go:12: 2+2 = 5, want 4 --- FAIL: TestSub/deep (0.00s) calc_test.go:20: 1-1 = 1, want 0 FAIL FAIL testdemo 0.006s FAIL ` // TestCoverageFailLines 失败行筛选:只留能说明问题的,丢掉 ok 与空行。 func TestCoverageFailLines(t *testing.T) { lines := strings.Split(sampleFailOut, "\n") got := coverageFailLines(lines) var hasCase, hasLoc, hasOk bool for _, s := range got { if strings.HasPrefix(s, "--- FAIL: TestAdd") { hasCase = true } if strings.Contains(s, "calc_test.go:12:") { hasLoc = true } if strings.HasPrefix(s, "ok ") { hasOk = true } } if !hasCase || !hasLoc { t.Fatalf("应保留用例行与断言行:%v", got) } if hasOk { t.Fatalf("不该回放 ok 行:%v", got) } } // TestCoverageFailLinesBuildError 编译失败也要能带出来(# 包头 + 位置行)。 func TestCoverageFailLinesBuildError(t *testing.T) { out := "# testdemo\n./main.go:6:2: undefined: fmtx\nFAIL\ttestdemo [build failed]\n" got := coverageFailLines(strings.Split(out, "\n")) joined := strings.Join(got, "\n") if !strings.Contains(joined, "undefined: fmtx") { t.Fatalf("编译错误被丢掉:%v", got) } if !strings.Contains(joined, "# testdemo") { t.Fatalf("包名行被丢掉:%v", got) } } // TestCoverageFailLinesCap 超量时截断,并追加提示。 func TestCoverageFailLinesCap(t *testing.T) { var lines []string for i := 0; i <= coverFailCap+10; i++ { lines = append(lines, "--- FAIL: TestX") } got := coverageFailLines(lines) if len(got) > coverFailCap+1 { t.Fatalf("未按上限截断,%d 行", len(got)) } last := got[len(got)-1] if !strings.Contains(last, "已截断") { t.Fatalf("最后一行应为截断提示,得到 %q", last) } } // TestCoverageFailSummary 摘要:数包、数用例、带退出码。 func TestCoverageFailSummary(t *testing.T) { got := coverageFailSummary(1, strings.Split(sampleFailOut, "\n")) if !strings.Contains(got, "1 个包失败") { t.Fatalf("包计数不对:%q", got) } if !strings.Contains(got, "2 个用例失败") { t.Fatalf("用例计数不对:%q", got) } if !strings.Contains(got, "退出码 1") { t.Fatalf("缺退出码:%q", got) } } // TestCoverageFailSummaryNoDetail 输出里什么都没有时也要给一句可读的话。 func TestCoverageFailSummaryNoDetail(t *testing.T) { got := coverageFailSummary(2, nil) if !strings.Contains(got, "go test 未通过") || !strings.Contains(got, "退出码 2") { t.Fatalf("兜底文案不对:%q", got) } }