-
Notifications
You must be signed in to change notification settings - Fork 12
/
input_test.go
181 lines (136 loc) · 4.09 KB
/
input_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
package input_autocomplete
import (
"runtime"
"testing"
)
func TestCanDeleteCharWhenPositionIsStartOfText(t *testing.T) {
// Position is in the start of text
input := NewInput("test: ")
input.AddChar('1')
input.cursor.SetPosition(0)
canDeleteCharResult := input.canDeleteChar()
if canDeleteCharResult == true {
t.Errorf("when position is 0 should enable deleting")
}
}
func TestCanDeleteCharWhenPositionIsEndOfText(t *testing.T) {
// Move position to end of text
input := NewInput("test: ")
input.AddChar('1')
canDeleteCharResult := input.canDeleteChar()
if canDeleteCharResult == false {
t.Errorf("when position is in the end of the text should disable deleting")
}
}
func TestAddCharInEndOfText(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
AssertTextAndPosition(t, input, "a", 1)
input.AddChar('b')
AssertTextAndPosition(t, input, "ab", 2)
}
func TestAddCharInMiddleOfText(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
AssertTextAndPosition(t, input, "a", 1)
input.AddChar('b')
AssertTextAndPosition(t, input, "ab", 2)
input.cursor.MoveLeft()
input.AddChar('c')
AssertTextAndPosition(t, input, "acb", 2)
}
func TestRemoveChar(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
input.RemoveChar()
AssertTextAndPosition(t, input, "", 0)
}
func TestRemoveCharWithOneLetter(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
input.RemoveChar()
AssertTextAndPosition(t, input, "", 0)
}
func TestRemoveCharWhenInStartPosition(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
input.MoveCursorLeft()
input.RemoveChar()
// should not delete anything
AssertTextAndPosition(t, input, "a", 0)
}
func TestRemoveCharWithThreeLetter(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
input.AddChar('b')
input.AddChar('c')
input.MoveCursorLeft()
input.RemoveChar()
AssertTextAndPosition(t, input, "ac", 1)
}
func TestMoveCursorLeft(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
input.MoveCursorLeft()
AssertPosition(t, input, 0)
}
func TestMoveCursorRightWhenInStartPosition(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
input.cursor.SetPosition(0)
input.MoveCursorRight()
AssertPosition(t, input, 1)
}
func TestMoveCursorRightWhenInEndPosition(t *testing.T) {
input := NewInput("test: ")
input.AddChar('a')
input.MoveCursorRight()
AssertPosition(t, input, 1)
}
func TestGetCurrentText(t *testing.T) {
expectedText := "a"
input := NewInput("test: ")
input.AddChar('a')
currentTextResult := input.GetCurrentText()
if currentTextResult != expectedText {
t.Errorf("Current text should be equal to %v, got %v ", expectedText, currentTextResult)
}
}
func TestAutocompleteOnNonUnixOS(t *testing.T) {
expectedAutocomplete := "C:\\ProgramData\\"
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
t.Skipf("Skip test because OS is %v", runtime.GOOS)
}
input := NewInput("test: ")
for _, ch := range "C:\\ProgramDa" {
input.AddChar(ch)
}
input.Autocomplete()
AssertTextAndPosition(t, input, expectedAutocomplete, len(expectedAutocomplete))
}
func TestAutocompleteOnUnixOS(t *testing.T) {
expectedAutocomplete := "/etc/passwd"
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("Skip test because OS is %v", runtime.GOOS)
}
input := NewInput("test: ")
for _, ch := range "/etc/passw" {
input.AddChar(ch)
}
input.Autocomplete()
AssertTextAndPosition(t, input, expectedAutocomplete, len(expectedAutocomplete))
}
func AssertTextAndPosition(t *testing.T, input *Input, expectedText string, expectedPosition int) {
AssertText(t, input, expectedText)
AssertPosition(t, input, expectedPosition)
}
func AssertText(t *testing.T, input *Input, expectedText string) {
if input.currentText != expectedText {
t.Errorf("Current text should be equal to %v, got %v ", expectedText, input.currentText)
}
}
func AssertPosition(t *testing.T, input *Input, expectedPosition int) {
if input.cursor.GetPosition() != expectedPosition {
t.Errorf("current position should be equal to %v, got %v ", expectedPosition, input.cursor.GetPosition())
}
}