- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package app
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/egoist/mygo/ui/editor"
|
|
)
|
|
|
|
// newTestDoc 造一个指向临时文件的文档。
|
|
func newTestDoc(t *testing.T, content string) *Doc {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "main.go")
|
|
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &Doc{path: p, buf: editor.NewBuffer(content)}
|
|
}
|
|
|
|
func TestDirtyDocs(t *testing.T) {
|
|
a := &App{}
|
|
d1 := newTestDoc(t, "package main\n")
|
|
d2 := newTestDoc(t, "package main\n")
|
|
a.docs = []*Doc{d1, d2}
|
|
|
|
if got := a.dirtyDocs(); len(got) != 0 {
|
|
t.Fatalf("刚打开不应有脏文件,实际 %d", len(got))
|
|
}
|
|
|
|
d2.buf.Edit(editor.Edit{Start: 0, End: 0, Text: "// 改动\n"})
|
|
got := a.dirtyDocs()
|
|
if len(got) != 1 || got[0] != d2 {
|
|
t.Fatalf("脏文件识别错误: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestWriteDocFile(t *testing.T) {
|
|
d := newTestDoc(t, "package main\n")
|
|
d.buf.Edit(editor.Edit{Start: 0, End: 0, Text: "// 头部\n"})
|
|
if !d.buf.Modified() {
|
|
t.Fatal("编辑后应当是脏的")
|
|
}
|
|
if err := writeDocFile(d); err != nil {
|
|
t.Fatalf("写盘失败: %v", err)
|
|
}
|
|
if d.buf.Modified() {
|
|
t.Fatal("写盘后不应再是脏的")
|
|
}
|
|
got, err := os.ReadFile(d.path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(string(got), "// 头部\n") {
|
|
t.Fatalf("磁盘内容不对: %q", string(got))
|
|
}
|
|
}
|
|
|
|
// TestSaveDocsNilWindow 确认没有窗口时批量保存也不会崩。
|
|
func TestSaveDocsNilWindow(t *testing.T) {
|
|
a := &App{}
|
|
d := newTestDoc(t, "package main\n")
|
|
d.buf.Edit(editor.Edit{Start: 0, End: 0, Text: "// x\n"})
|
|
a.docs = []*Doc{d}
|
|
if !a.saveDocs(a.dirtyDocs()) {
|
|
t.Fatal("保存应当成功")
|
|
}
|
|
if d.buf.Modified() {
|
|
t.Fatal("保存后仍是脏的")
|
|
}
|
|
}
|
|
|
|
// TestWriteDocFileError 确认目标不可写时返回错误而不是 panic。
|
|
func TestWriteDocFileError(t *testing.T) {
|
|
dir := t.TempDir()
|
|
d := &Doc{path: filepath.Join(dir, "no-such-dir", "x.go"), buf: editor.NewBuffer("package x\n")}
|
|
if err := writeDocFile(d); err == nil {
|
|
t.Fatal("应当返回错误")
|
|
}
|
|
}
|