From 07c51e1c022dc1642957a481159d732575492e3f 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 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/editor/editor_test.go b/editor/editor_test.go index b5d9cbb..c3e2530 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.DeleteByte}) + 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, wold!"; 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())