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

97 lines
2.6 KiB
Go

package app
import (
"path/filepath"
"testing"
"github.com/egoist/mygo/ui/editor"
)
// TestApplyPendingCursorClamps 存档里的位置越界时(文件被删了几行)不能跑出界。
func TestApplyPendingCursorClamps(t *testing.T) {
isolateConfig(t)
dir := t.TempDir()
a := &App{set: defaultSettings(), root: dir}
d := a.newDoc(filepath.Join(dir, "a.go"), "package main\n\nfunc main() {}\n")
a.docs = append(a.docs, d)
a.pendingCursor = []cursorRestore{{doc: d, cur: cursorJSON{Line: 99, Column: 999}}}
a.applyPendingCursor()
pos := d.st.CursorPos()
last := d.buf.LineCount() - 1
if pos.Line != last {
t.Fatalf("行应被夹到最后一行 %d,实际 %d", last, pos.Line)
}
if pos.Col > len(d.buf.Line(last)) {
t.Fatalf("列超出该行长度: %d > %d", pos.Col, len(d.buf.Line(last)))
}
if len(a.pendingCursor) != 0 {
t.Fatal("应用后应清空待办,避免重复覆盖用户的位置")
}
}
// TestApplyPendingCursorRestores 正常位置要能原样恢复。
func TestApplyPendingCursorRestores(t *testing.T) {
isolateConfig(t)
dir := t.TempDir()
a := &App{set: defaultSettings(), root: dir}
d := a.newDoc(filepath.Join(dir, "a.go"), "aaa\nbbb\nccc\n")
a.docs = append(a.docs, d)
a.pendingCursor = []cursorRestore{{doc: d, cur: cursorJSON{Line: 1, Column: 2}}}
a.applyPendingCursor()
pos := d.st.CursorPos()
if pos.Line != 1 || pos.Col != 2 {
t.Fatalf("位置没恢复: %+v", pos)
}
}
// TestSessionCursorRoundTrip 会话文件里要能存下并读回光标位置。
func TestSessionCursorRoundTrip(t *testing.T) {
isolateConfig(t)
root := t.TempDir()
path := filepath.Join(root, "a.go")
a := &App{set: defaultSettings(), root: root}
d := a.newDoc(path, "package main\n\nfunc main() {}\n")
d.st.GoTo(editor.Pos{Line: 2, Col: 4})
a.docs = append(a.docs, d)
a.active = path
a.saveSession()
st, ok := loadSession(root)
if !ok {
t.Fatal("会话没写出来")
}
c, found := st.Cursors[path]
if !found {
t.Fatalf("会话里没有光标记录: %+v", st.Cursors)
}
if c.Line != 2 || c.Column != 4 {
t.Fatalf("位置不对: %+v", c)
}
}
// TestSaveSessionSkipsTruncated 只读截断的大文件不记位置,没意义。
func TestSaveSessionSkipsTruncated(t *testing.T) {
isolateConfig(t)
root := t.TempDir()
path := filepath.Join(root, "big.log")
a := &App{set: defaultSettings(), root: root}
d := a.newDoc(path, "x\n")
d.truncated = true
a.docs = append(a.docs, d)
a.saveSession()
st, ok := loadSession(root)
if !ok {
t.Fatal("会话没写出来")
}
if _, found := st.Cursors[path]; found {
t.Fatal("截断文件不该记录光标位置")
}
}