turbo-editors/turbo-corepublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on main · k33g · 3h ago
undo_test.go · 245 lines · 5.7 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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")
	}
}