From eb3dd8de5a882378b9a4132a5d932630e6ca3f4c Mon Sep 17 00:00:00 2001 From: itchyny Date: Sun, 20 Oct 2024 09:36:28 +0900 Subject: [PATCH] add editor test for moving cursor from command line --- editor/editor_test.go | 74 +++++++++++++++++++++++++++++++++++------- window/manager_test.go | 2 +- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/editor/editor_test.go b/editor/editor_test.go index b5d9cbb..48f853c 100644 --- a/editor/editor_test.go +++ b/editor/editor_test.go @@ -341,9 +341,6 @@ func TestEditorWriteVisualSelection(t *testing.T) { } func TestEditorWriteUndo(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("skip on Windows") - } ui := newTestUI() editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline()) if err := editor.Init(); err != nil { @@ -358,9 +355,9 @@ func TestEditorWriteUndo(t *testing.T) { } go func() { ui.Emit(event.Event{Type: event.DeleteByte}) - ui.Emit(event.Event{Type: event.Write, Arg: f.Name()}) + ui.Emit(event.Event{Type: event.Write, Arg: f.Name() + ".out"}) ui.Emit(event.Event{Type: event.Undo}) - ui.Emit(event.Event{Type: event.WriteQuit, Arg: f.Name()}) + ui.Emit(event.Event{Type: event.Quit}) }() if err := editor.Run(); err != nil { t.Errorf("err should be nil but got: %v", err) @@ -382,12 +379,16 @@ func TestEditorWriteUndo(t *testing.T) { if expected := "abc"; string(bs) != expected { t.Errorf("file contents should be %q but got %q", expected, string(bs)) } + bs, err = os.ReadFile(f.Name() + ".out") + if err != nil { + t.Errorf("err should be nil but got: %v", err) + } + if expected := "bc"; string(bs) != expected { + t.Errorf("file contents should be %q but got %q", expected, string(bs)) + } } func TestEditorSearch(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("skip on Windows") - } ui := newTestUI() editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline()) if err := editor.Init(); err != nil { @@ -418,8 +419,8 @@ func TestEditorSearch(t *testing.T) { ui.Emit(event.Event{Type: event.DeleteByte}) ui.Emit(event.Event{Type: event.PreviousSearch}) ui.Emit(event.Event{Type: event.DeleteByte}) - ui.Emit(event.Event{Type: event.Write}) - ui.Emit(event.Event{Type: event.Quit}) + ui.Emit(event.Event{Type: event.Write, Arg: f.Name() + ".out"}) + ui.Emit(event.Event{Type: event.Quit, Bang: true}) }() if err := editor.Run(); err != nil { t.Errorf("err should be nil but got: %v", err) @@ -434,7 +435,7 @@ func TestEditorSearch(t *testing.T) { if err := editor.Close(); err != nil { t.Errorf("err should be nil but got: %v", err) } - bs, err := os.ReadFile(f.Name()) + bs, err := os.ReadFile(f.Name() + ".out") if err != nil { t.Errorf("err should be nil but got: %v", err) } @@ -443,6 +444,57 @@ func TestEditorSearch(t *testing.T) { } } +func TestEditorCmdlineCursorGoto(t *testing.T) { + ui := newTestUI() + editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline()) + if err := editor.Init(); err != nil { + t.Fatalf("err should be nil but got: %v", err) + } + f, err := createTemp(t.TempDir(), "Hello, world!") + if err != nil { + t.Fatalf("err should be nil but got: %v", err) + } + if err := editor.Open(f.Name()); err != nil { + t.Fatalf("err should be nil but got: %v", err) + } + go func() { + ui.Emit(event.Event{Type: event.StartCmdlineCommand}) + ui.Emit(event.Event{Type: event.Rune, Rune: '6'}) + ui.Emit(event.Event{Type: event.ExecuteCmdline}) + ui.Emit(event.Event{Type: event.DeleteByte}) + ui.Emit(event.Event{Type: event.Write, Arg: f.Name() + ".out1"}) + ui.Emit(event.Event{Type: event.Undo}) + ui.Emit(event.Event{Type: event.StartCmdlineCommand}) + ui.Emit(event.Event{Type: event.Rune, Rune: '7'}) + ui.Emit(event.Event{Type: event.Rune, Rune: '0'}) + ui.Emit(event.Event{Type: event.Rune, Rune: '%'}) + ui.Emit(event.Event{Type: event.ExecuteCmdline}) + ui.Emit(event.Event{Type: event.DeletePrevByte}) + ui.Emit(event.Event{Type: event.Write, Arg: f.Name() + ".out2"}) + ui.Emit(event.Event{Type: event.Quit, Bang: true}) + }() + if err := editor.Run(); err != nil { + t.Errorf("err should be nil but got: %v", err) + } + if err := editor.Close(); err != nil { + t.Errorf("err should be nil but got: %v", err) + } + bs, err := os.ReadFile(f.Name() + ".out1") + if err != nil { + t.Errorf("err should be nil but got: %v", err) + } + if expected := "Hello,world!"; string(bs) != expected { + t.Errorf("file contents should be %q but got %q", expected, string(bs)) + } + bs, err = os.ReadFile(f.Name() + ".out2") + if err != nil { + t.Errorf("err should be nil but got: %v", err) + } + if expected := "Hello, wrld!"; string(bs) != expected { + t.Errorf("file contents should be %q but got %q", expected, string(bs)) + } +} + func TestEditorCmdlineQuit(t *testing.T) { ui := newTestUI() editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline()) diff --git a/window/manager_test.go b/window/manager_test.go index e2dbfb4..7ef3c4b 100644 --- a/window/manager_test.go +++ b/window/manager_test.go @@ -212,7 +212,7 @@ func TestManagerOpenExpandBacktick(t *testing.T) { wm.Close() } -func TestEditorOpenExpandHomedir(t *testing.T) { +func TestManagerOpenExpandHomedir(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("skip on Windows") }