-
Notifications
You must be signed in to change notification settings - Fork 8
/
undoredo_test.go
109 lines (95 loc) · 2.76 KB
/
undoredo_test.go
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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package commands
import (
"testing"
"github.com/limetext/backend"
"github.com/limetext/text"
)
func TestUndoRedos(t *testing.T) {
ed := backend.GetEditor()
ch := ed.CommandHandler()
w := ed.NewWindow()
defer w.Close()
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
edit := v.BeginEdit()
v.Insert(edit, 0, "abcd")
v.EndEdit(edit)
v.Sel().Clear()
r := []text.Region{
{0, 0},
{1, 1},
{2, 2},
{3, 3},
}
for _, r2 := range r {
v.Sel().Add(r2)
}
edit = v.BeginEdit()
for _, ins := range "1234" {
for i := 0; i < v.Sel().Len(); i++ {
v.Insert(edit, v.Sel().Get(i).Begin(), string(ins))
}
}
v.EndEdit(edit)
if v.Substr(text.Region{0, v.Size()}) != "1234a1234b1234c1234d" {
t.Error(v.Substr(text.Region{0, v.Size()}))
}
ch.RunTextCommand(v, "undo", nil)
if v.Substr(text.Region{0, v.Size()}) != "abcd" {
t.Error("expected 'abcd', but got: ", v.Substr(text.Region{0, v.Size()}))
}
ch.RunTextCommand(v, "redo", nil)
if v.Substr(text.Region{0, v.Size()}) != "1234a1234b1234c1234d" {
t.Error("expected '1234a1234b1234c1234d', but got: ", v.Substr(text.Region{0, v.Size()}))
}
v.Sel().Clear()
r = []text.Region{
{0, 0},
{5, 5},
{10, 10},
{15, 15},
}
for _, r2 := range r {
v.Sel().Add(r2)
}
edit = v.BeginEdit()
for _, ins := range []string{"hello ", "world"} {
for i := 0; i < v.Sel().Len(); i++ {
v.Insert(edit, v.Sel().Get(i).Begin(), ins)
}
}
v.EndEdit(edit)
if v.Substr(text.Region{0, v.Size()}) != "hello world1234ahello world1234bhello world1234chello world1234d" {
t.Error(v.Substr(text.Region{0, v.Size()}))
}
ch.RunTextCommand(v, "undo", nil)
if v.Substr(text.Region{0, v.Size()}) != "1234a1234b1234c1234d" {
t.Error("expected '1234a1234b1234c1234d', but got: ", v.Substr(text.Region{0, v.Size()}))
}
ch.RunTextCommand(v, "undo", nil)
if v.Substr(text.Region{0, v.Size()}) != "abcd" {
t.Error("expected 'abcd', but got: ", v.Substr(text.Region{0, v.Size()}))
}
ch.RunTextCommand(v, "undo", nil)
if v.Substr(text.Region{0, v.Size()}) != "" {
t.Error("expected '', but got: ", v.Substr(text.Region{0, v.Size()}))
}
v.UndoStack().Redo(true)
if v.Substr(text.Region{0, v.Size()}) != "abcd" {
t.Error("expected 'abcd', but got: ", v.Substr(text.Region{0, v.Size()}))
}
v.UndoStack().Redo(true)
if v.Substr(text.Region{0, v.Size()}) != "1234a1234b1234c1234d" {
t.Error("expected '1234a1234b1234c1234d', but got: ", v.Substr(text.Region{0, v.Size()}))
}
v.UndoStack().Redo(true)
if v.Substr(text.Region{0, v.Size()}) != "hello world1234ahello world1234bhello world1234chello world1234d" {
t.Error(v.Substr(text.Region{0, v.Size()}))
}
}