Files
MyGolandRelease/internal/app/testlens_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

124 lines
3.2 KiB
Go

package app
import (
"os"
"path/filepath"
"reflect"
"testing"
)
func TestFailureLocPicksTestFile(t *testing.T) {
lines := []string{
"=== RUN TestFoo",
" main_test.go:12: boom",
"--- FAIL: TestFoo (0.00s)",
"FAIL",
}
file, line := failureLoc(lines)
if file != "main_test.go" || line != 12 {
t.Fatalf("应解析出 main_test.go:12,实际 %s:%d", file, line)
}
}
func TestFailureLocAbsolutePath(t *testing.T) {
file, line := failureLoc([]string{` D:\x\y\sub_test.go:7: nope`})
if file != `D:\x\y\sub_test.go` || line != 7 {
t.Fatalf("绝对路径应原样保留,实际 %s:%d", file, line)
}
}
func TestFailureLocNone(t *testing.T) {
if f, l := failureLoc([]string{"--- FAIL: TestX (0.1s)", "panic: boom"}); f != "" || l != 0 {
t.Fatalf("没有 file:line 时应为空,实际 %s:%d", f, l)
}
}
func TestRunFilterFor(t *testing.T) {
cases := map[string]string{
"TestFoo": "^TestFoo$",
"TestFoo/case 1": "^TestFoo$/^case 1$",
"TestFoo/a[b]/sub": "^TestFoo$/^a\\[b\\]$/^sub$",
}
for name, want := range cases {
if got := runFilterFor(name); got != want {
t.Errorf("runFilterFor(%q) = %q,期望 %q", name, got, want)
}
}
}
func TestScanTestFuncs(t *testing.T) {
src := `package app
func helper(t *testing.T) {}
func TestFoo(t *testing.T) {
}
func BenchmarkX(b *testing.B) {
}
func TestBar(t *testing.T) {
}
`
got := scanTestFuncs(src)
want := []testFuncInfo{
{Name: "TestFoo", Line: 4},
{Name: "BenchmarkX", Line: 7},
{Name: "TestBar", Line: 10},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("扫描结果不符,got %+v want %+v", got, want)
}
}
func TestTestFuncAt(t *testing.T) {
src := "package app\n\nfunc TestFoo(t *testing.T) {\n\tx()\n}\n\nfunc TestBar(t *testing.T) {\n}\n"
cases := []struct {
line int
name string
found bool
}{
{0, "", false}, // package 行
{1, "", false}, // 第一个函数之前
{2, "TestFoo", true}, // 声明行本身
{3, "TestFoo", true}, // 函数体内
{4, "TestFoo", true}, // 收尾 } 上仍算在函数里
{5, "", false}, // 函数之间的空行
{6, "TestBar", true},
}
for _, c := range cases {
name, ok := testFuncAt(src, c.line)
if ok != c.found || name != c.name {
t.Errorf("testFuncAt(line=%d) = (%q,%v),期望 (%q,%v)", c.line, name, ok, c.name, c.found)
}
}
}
func TestResolveTestFile(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "go.mod"), []byte("module demo\n\ngo 1.21\n"), 0o644); err != nil {
t.Fatal(err)
}
dir := filepath.Join(root, "internal", "app")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "foo_test.go"), []byte("package app\n"), 0o644); err != nil {
t.Fatal(err)
}
a := &App{root: root}
got := a.resolveTestFile("demo/internal/app", "foo_test.go")
if want := filepath.Join(dir, "foo_test.go"); got != want {
t.Fatalf("应按 module 前缀还原,got %q want %q", got, want)
}
// 绝对路径直接透传。
if got := a.resolveTestFile("demo", root); got != root {
t.Fatalf("绝对路径应透传,got %q", got)
}
// 包路径不在 module 里且文件不存在:不 panic,返回一个猜测路径。
_ = a.resolveTestFile("other/pkg", "zzz_test.go")
}