package buffer import "testing" func TestUndoOnAFreshBufferReportsNothingToDo(t *testing.T) { b := NewFromString("abc") if b.CanUndo() { t.Error("CanUndo() = true on a freshly loaded buffer") } if b.Undo() { t.Error("Undo() = true, want false with an empty history") } if b.CanRedo() || b.Redo() { t.Error("a fresh buffer must have nothing to redo either") } } func TestUndoRevertsAnInsertionAndRestoresTheCursor(t *testing.T) { b := NewFromString("hello") b.MoveBufferEnd() b.Insert(" world") if got := b.Text(); got != "hello world" { t.Fatalf("Text() = %q before undo, want %q", got, "hello world") } if !b.Undo() { t.Fatal("Undo() = false, want true") } if got := b.Text(); got != "hello" { t.Errorf("Text() = %q, want %q", got, "hello") } if got := b.Cursor(); got != (Position{Line: 0, Col: 5}) { t.Errorf("Cursor() = %+v, want where it was before the insertion", got) } } func TestUndoRevertsAMultiLineDeletion(t *testing.T) { b := NewFromString("one\ntwo\nthree") b.DeleteRange(Range{Start: Position{Line: 0, Col: 1}, End: Position{Line: 2, Col: 2}}) b.Undo() if got := b.Text(); got != "one\ntwo\nthree" { t.Errorf("Text() = %q, want the original text back", got) } } func TestRedoReappliesTheChange(t *testing.T) { b := NewFromString("one\ntwo") b.MoveBufferEnd() b.Insert("\nthree") b.Undo() if !b.Redo() { t.Fatal("Redo() = false, want true") } if got := b.Text(); got != "one\ntwo\nthree" { t.Errorf("Text() = %q, want the change back", got) } if got := b.Cursor(); got != (Position{Line: 2, Col: 5}) { t.Errorf("Cursor() = %+v, want where it was after the insertion", got) } } func TestANewEditClearsTheRedoStack(t *testing.T) { b := NewFromString("a") b.MoveBufferEnd() b.Insert("b") b.Undo() b.Insert("c") if b.CanRedo() { t.Error("CanRedo() = true after a new edit, want the redo stack cleared") } if got := b.Text(); got != "ac" { t.Errorf("Text() = %q, want %q", got, "ac") } } func TestUndoWalksBackThroughSeveralSteps(t *testing.T) { b := NewFromString("") b.Insert("one") b.Insert("-two") b.Insert("-three") steps := []string{"one-two", "one", ""} for i, want := range steps { if !b.Undo() { t.Fatalf("Undo() = false at step %d", i) } if got := b.Text(); got != want { t.Errorf("after undo %d, Text() = %q, want %q", i+1, got, want) } } if b.Undo() { t.Error("Undo() = true once the history is exhausted") } } func TestTypedCharactersUndoAsOneStep(t *testing.T) { b := NewFromString("") for _, r := range "func" { b.InsertRune(r) } if got := b.Text(); got != "func" { t.Fatalf("Text() = %q, want %q", got, "func") } if !b.Undo() { t.Fatal("Undo() = false, want true") } if got := b.Text(); got != "" { t.Errorf("Text() = %q, want the whole typed run removed in one step", got) } if b.CanUndo() { t.Error("the run of typing must have produced a single undo entry") } } func TestMovingTheCursorEndsTheTypedRun(t *testing.T) { b := NewFromString("") b.InsertRune('a') b.InsertRune('b') b.MoveLeft() b.InsertRune('c') if got := b.Text(); got != "acb" { t.Fatalf("Text() = %q, want %q", got, "acb") } b.Undo() if got := b.Text(); got != "ab" { t.Errorf("Text() = %q, want only the character typed after the move removed", got) } b.Undo() if got := b.Text(); got != "" { t.Errorf("Text() = %q, want the earlier run removed by the second undo", got) } } func TestBackspacesUndoAsOneStep(t *testing.T) { b := NewFromString("abcdef") b.MoveBufferEnd() for range 3 { b.Backspace() } if got := b.Text(); got != "abc" { t.Fatalf("Text() = %q, want %q", got, "abc") } if !b.Undo() { t.Fatal("Undo() = false, want true") } if got := b.Text(); got != "abcdef" { t.Errorf("Text() = %q, want the whole run of backspaces undone at once", got) } if got := b.Cursor(); got != (Position{Line: 0, Col: 6}) { t.Errorf("Cursor() = %+v, want where the run of backspaces started", got) } if b.CanUndo() { t.Error("the run of backspaces must have produced a single undo entry") } } func TestTypingAndBackspacingDoNotMergeTogether(t *testing.T) { b := NewFromString("") b.InsertRune('a') b.InsertRune('b') b.Backspace() if got := b.Text(); got != "a" { t.Fatalf("Text() = %q, want %q", got, "a") } b.Undo() if got := b.Text(); got != "ab" { t.Errorf("Text() = %q, want only the backspace undone", got) } b.Undo() if got := b.Text(); got != "" { t.Errorf("Text() = %q, want the typing undone by the second step", got) } } func TestUndoRedoRoundTripAcrossManyEdits(t *testing.T) { b := NewFromString("package main\n") b.MoveBufferEnd() b.Insert("\nfunc main() {\n}") b.SetCursor(Position{Line: 2, Col: 13}) b.InsertNewlineAndIndent() b.Insert("\tprintln(1)") final := b.Text() depth := 0 for b.Undo() { depth++ } if got := b.Text(); got != "package main\n" { t.Fatalf("after undoing everything, Text() = %q, want the original", got) } for range depth { if !b.Redo() { t.Fatal("Redo() ran out before the history did") } } if got := b.Text(); got != final { t.Errorf("after redoing everything, Text() = %q, want %q", got, final) } } func TestUndoHistoryIsCappedAtItsMaximumDepth(t *testing.T) { b := NewFromString("") for i := range maxUndoDepth + 50 { b.Insert(string(rune('a' + i%26))) } steps := 0 for b.Undo() { steps++ } if steps != maxUndoDepth { t.Errorf("undid %d steps, want the history capped at %d", steps, maxUndoDepth) } } func TestUndoClearsTheSelection(t *testing.T) { b := NewFromString("abc") b.MoveBufferEnd() b.Insert("d") b.StartSelection() b.SetCursorKeepingSelection(Position{Line: 0, Col: 0}) b.Undo() if _, ok := b.Selection(); ok { t.Error("a selection must not survive an undo, whose text it may no longer match") } }