Skip to content

Commit

Permalink
add editor test for moving cursor from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 20, 2024
1 parent c702044 commit eb3dd8d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
74 changes: 63 additions & 11 deletions editor/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion window/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit eb3dd8d

Please sign in to comment.