Files
MyGolandRelease/internal/app/coverage_test.go
T
zerorigin a80c0f886a MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
2026-11-04 16:37:05 +08:00

94 lines
2.8 KiB
Go

package app
import (
"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)
}
}