Skip to content

Commit

Permalink
fix nil pointer dereference in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 28, 2024
1 parent 96f7198 commit 3848992
Show file tree
Hide file tree
Showing 4 changed files with 580 additions and 153 deletions.
4 changes: 2 additions & 2 deletions editor/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ func TestEditorShowBinary(t *testing.T) {
if err := editor.Run(); err != nil {
t.Errorf("err should be nil but got: %v", err)
}
if expected := "01001000"; editor.err.Error() != expected {
if expected := "01001000"; editor.err == nil || editor.err.Error() != expected {
t.Errorf("err should be %q but got: %v", expected, editor.err)
}
if editor.errtyp != state.MessageInfo {
Expand Down Expand Up @@ -796,7 +796,7 @@ func TestEditorShowDecimal(t *testing.T) {
if err := editor.Run(); err != nil {
t.Errorf("err should be nil but got: %v", err)
}
if expected := "72"; editor.err.Error() != expected {
if expected := "72"; editor.err == nil || editor.err.Error() != expected {
t.Errorf("err should be %q but got: %v", expected, editor.err)
}
if editor.errtyp != state.MessageInfo {
Expand Down
6 changes: 6 additions & 0 deletions history/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func TestHistoryUndo(t *testing.T) {

buf := make([]byte, 8)
b, index, offset, cursor, tick = history.Undo()
if b == nil {
t.Fatalf("history.Undo should return buffer but got nil")
}
_, err := b.Read(buf)
if err != nil {
t.Errorf("err should be nil but got: %v", err)
Expand All @@ -56,6 +59,9 @@ func TestHistoryUndo(t *testing.T) {

buf = make([]byte, 8)
b, offset, cursor, tick = history.Redo()
if b == nil {
t.Fatalf("history.Redo should return buffer but got nil")
}
_, err = b.Read(buf)
if err != nil {
t.Errorf("err should be nil but got: %v", err)
Expand Down
58 changes: 40 additions & 18 deletions window/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ func TestManagerOpenEmpty(t *testing.T) {
t.Fatalf("err should be nil but got: %v", err)
}
windowStates, _, windowIndex, err := wm.State()
ws := windowStates[0]
if windowIndex != 0 {
t.Errorf("window index should be %d but got %d", 0, windowIndex)
if expected := 0; windowIndex != expected {
t.Errorf("windowIndex should be %d but got %d", expected, windowIndex)
}
ws, ok := windowStates[windowIndex]
if !ok {
t.Fatalf("windowStates should contain %d but got: %v", windowIndex, windowStates)
}
if expected := ""; ws.Name != expected {
t.Errorf("name should be %q but got %q", expected, ws.Name)
Expand Down Expand Up @@ -91,9 +94,12 @@ func TestManagerOpenStates(t *testing.T) {
t.Fatalf("err should be nil but got: %v", err)
}
windowStates, _, windowIndex, err := wm.State()
ws := windowStates[0]
if windowIndex != 0 {
t.Errorf("window index should be %d but got %d", 0, windowIndex)
if expected := 0; windowIndex != expected {
t.Errorf("windowIndex should be %d but got %d", expected, windowIndex)
}
ws, ok := windowStates[windowIndex]
if !ok {
t.Fatalf("windowStates should contain %d but got: %v", windowIndex, windowStates)
}
if expected := filepath.Base(f.Name()); ws.Name != expected {
t.Errorf("name should be %q but got %q", expected, ws.Name)
Expand Down Expand Up @@ -148,9 +154,12 @@ func TestManagerOpenNonExistsWrite(t *testing.T) {
wm.Emit(event.Event{Type: event.ExitInsert})
wm.Emit(event.Event{Type: event.WriteQuit})
windowStates, _, windowIndex, err := wm.State()
ws := windowStates[0]
if windowIndex != 0 {
t.Errorf("window index should be %d but got %d", 0, windowIndex)
if expected := 0; windowIndex != expected {
t.Errorf("windowIndex should be %d but got %d", expected, windowIndex)
}
ws, ok := windowStates[windowIndex]
if !ok {
t.Fatalf("windowStates should contain %d but got: %v", windowIndex, windowStates)
}
if expected := filepath.Base(fname); ws.Name != expected {
t.Errorf("name should be %q but got %q", expected, ws.Name)
Expand Down Expand Up @@ -192,8 +201,11 @@ func TestManagerOpenExpandBacktick(t *testing.T) {
if err := wm.Open(cmd); err != nil {
t.Fatalf("err should be nil but got: %v", err)
}
windowStates, _, _, err := wm.State()
ws := windowStates[0]
windowStates, _, windowIndex, err := wm.State()
ws, ok := windowStates[windowIndex]
if !ok {
t.Fatalf("windowStates should contain %d but got: %v", windowIndex, windowStates)
}
if ws.Name != name {
t.Errorf("name should be %q but got %q", name, ws.Name)
}
Expand Down Expand Up @@ -238,9 +250,12 @@ func TestManagerOpenExpandHomedir(t *testing.T) {
t.Fatalf("err should be nil but got: %v", err)
}
windowStates, _, windowIndex, err := wm.State()
ws := windowStates[i]
if windowIndex != i {
t.Errorf("window index should be %d but got %d", i, windowIndex)
t.Errorf("windowIndex should be %d but got %d", i, windowIndex)
}
ws, ok := windowStates[windowIndex]
if !ok {
t.Fatalf("windowStates should contain %d but got: %v", windowIndex, windowStates)
}
if expected := filepath.Base(f.Name()); ws.Name != expected {
t.Errorf("name should be %q but got %q", expected, ws.Name)
Expand Down Expand Up @@ -353,9 +368,12 @@ func TestManagerRead(t *testing.T) {
t.Fatalf("err should be nil but got: %v", err)
}
windowStates, _, windowIndex, err := wm.State()
ws := windowStates[0]
if windowIndex != 0 {
t.Errorf("window index should be %d but got %d", 0, windowIndex)
if expected := 0; windowIndex != expected {
t.Errorf("windowIndex should be %d but got %d", expected, windowIndex)
}
ws, ok := windowStates[windowIndex]
if !ok {
t.Fatalf("windowStates should contain %d but got: %v", windowIndex, windowStates)
}
if ws.Name != "" {
t.Errorf("name should be %q but got %q", "", ws.Name)
Expand Down Expand Up @@ -640,8 +658,12 @@ func TestManagerCopyCutPaste(t *testing.T) {
if !strings.HasPrefix(string(p), "lo, wo") {
t.Errorf("buffer string should be %q but got: %q", "", string(p))
}
windowStates, _, _, _ := wm.State()
ws := windowStates[0]
windowStates, _, windowIndex, _ := wm.State()
ws, ok := windowStates[windowIndex]
if !ok {
t.Errorf("windowStates should contain %d but got: %v", windowIndex, windowStates)
return
}
if ws.Length != int64(7) {
t.Errorf("Length should be %d but got %d", int64(7), ws.Length)
}
Expand Down
Loading

0 comments on commit 3848992

Please sign in to comment.