-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor_test.go
184 lines (162 loc) · 4.41 KB
/
editor_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
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
package noter
import (
"reflect"
"testing"
)
func TestGetLineNumber(t *testing.T) {
line1 := &editorLine{}
line2 := &editorLine{}
line1.next = line2
line2.prev = line1
editor := &Editor{
start: line1,
cursor: &editorCursor{
line2,
0,
},
}
lineNum := editor.getLineNumber()
want := 1
if lineNum != want {
t.Fatalf(`Expected current line number to be %v, got: %v`, want, lineNum)
}
}
func TestGetAllRunes(t *testing.T) {
line1 := &editorLine{values: []rune{'a', '\n'}}
line2 := &editorLine{values: []rune{'b', '\n'}}
line1.next = line2
line2.prev = line1
editor := &Editor{
start: line1,
cursor: &editorCursor{
line2,
0,
},
}
allRunes := editor.getAllRunes()
if reflect.DeepEqual(allRunes, []rune{'a', '\n', 'b', '\n'}) != true {
t.Fatalf(`Expected allRunes to return document runes, got: %v`, allRunes)
}
}
func TestDeleteRune(t *testing.T) {
line1 := &editorLine{values: []rune{'a', '\n'}}
editor := &Editor{
start: line1,
cursor: &editorCursor{
line1,
1,
},
}
editor.fnDeleteSinglePrevious()
if len(line1.values) != 0 && line1.values[0] != '\n' {
t.Fatalf("Delete operation did not work correctly, got: %v", line1.values)
}
}
func TestDeleteLine(t *testing.T) {
line1 := &editorLine{values: []rune{'a', '\n'}}
line2 := &editorLine{values: []rune{'b', '\n'}}
line1.next = line2
line2.prev = line1
editor := &Editor{
start: line1,
cursor: &editorCursor{
line2,
1,
},
}
editor.fnDeleteSinglePrevious()
editor.fnDeleteSinglePrevious()
if len(line1.values) != 0 && line1.next != nil {
t.Fatalf("Delete operation did not work correctly, got: %v, %v", line1.values, line1.next)
}
}
func TestHighlightLineAndGetHighlightedRunes(t *testing.T) {
line1 := &editorLine{values: []rune{'a', '\n'}}
line2 := &editorLine{values: []rune{'b', '\n'}}
line1.next = line2
line2.prev = line1
editor := &Editor{
start: line1,
cursor: &editorCursor{
line2,
1,
},
// This would normally happen in editor.Load()
highlighted: make(map[*editorLine]map[int]bool),
}
editor.highlightLine()
if reflect.DeepEqual(editor.getHighlightedRunes(), []rune{'b', '\n'}) != true {
t.Fatalf(`Expected GetHighlightedRunes to return line2's runes, got: %v`, editor.getHighlightedRunes())
}
}
func TestSearch(t *testing.T) {
line1 := &editorLine{values: []rune{'a', '\n'}}
line2 := &editorLine{values: []rune{'b', '\n'}}
line1.next = line2
line2.prev = line1
editor := &Editor{
start: line1,
cursor: &editorCursor{
line2,
1,
},
}
editor.mode = SEARCH_MODE
// This would normally happen in editor.Load()
editor.searchHighlights = map[*editorLine]map[int]bool{}
editor.searchTerm = []rune{'b'}
editor.search()
if _, ok := editor.searchHighlights[line2]; !ok {
t.Fatalf("Incorrect search highlights: line2 wasn't highlighted")
}
if _, ok := editor.searchHighlights[line2][0]; !ok {
t.Fatalf("Incorrect search highlights: line index wasn't highlighted")
}
if editor.searchHighlights[line2][0] != true {
t.Fatalf("Incorrect search highlights: boolean was false instead of true")
}
}
func TestLayout(t *testing.T) {
editor := NewEditor(
WithWidth(123),
WithHeight(456),
)
table := [](struct{ screen_w, screen_h, layout_w, layout_h int }){
{123, 456, 123, 456},
{0, 0, 123, 456},
{1024, 768, 123, 456},
}
for _, entry := range table {
w, h := editor.Layout(entry.screen_w, entry.screen_h)
if w != entry.layout_w || h != entry.layout_h {
t.Fatalf("Incorrect result (%v,%v) from Editor.Layout(%v,%v); expected (%v,%v)",
w, h, entry.screen_w, entry.screen_h, entry.layout_w, entry.layout_h)
}
}
}
func TestMoveCursorScroll(t *testing.T) {
editor := NewEditor(
WithRows(3),
WithColumns(4),
)
editor.WriteText([]byte("1\n2\n3\n4\n5\n6\n7\n8\n"))
table := [](struct{ row, first_visible int }){
{0, 0}, // Move to first line.
{1, 0}, // Move to middle line.
{2, 0}, // Move to end of first page.
{3, 1}, // Scrolls down by one.
{4, 2}, // Scrolls down by one mode.
{2, 2}, // Back to top.
{7, 5}, // Move to end.
{5, 5}, // Move to top view of end.
{4, 4}, // Move up one line moves one line up.
{1, 1}, // Move up one line moves one line up.
{0, 0}, // Move up to first page.
}
for _, entry := range table {
editor.MoveCursor(entry.row, 0)
if entry.first_visible != editor.firstVisible {
t.Fatalf("Incorrect move to row %v, expected first visible to %v, was %v", entry.row, entry.first_visible, editor.firstVisible)
}
}
}