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("截断文件不该记录光标位置") } }