forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput_test.go
112 lines (88 loc) · 3.11 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
package termui
import (
"strings"
"testing"
)
// TODO: More tests!
const TESTING_LINE = "testing"
func TestInput_SingleLine_NoNewLines(t *testing.T) {
input := NewInput("", false)
input.addString(TESTING_LINE)
input.addString(NEW_LINE)
if input.Text() != TESTING_LINE {
t.Errorf("Expected test to only contains %s (%d) but found %s (%d)",
TESTING_LINE, len(TESTING_LINE), input.Text(), len(input.Text()))
}
if strings.HasSuffix(input.Text(), NEW_LINE) {
t.Error("Unexpected newline at the end of TEXT")
}
}
func TestInput_MultiLine_SingleEntry(t *testing.T) {
input := NewInput("", true)
input.addString(TESTING_LINE)
if len(input.Lines()) != 1 {
t.Errorf("Invalid number of lines in input, expected 1 but found %d", len(input.Lines()))
}
if input.Text() != TESTING_LINE {
t.Errorf("Expected test to only contains %s (%d) but found %s (%d)",
TESTING_LINE, len(TESTING_LINE), input.Text(), len(input.Text()))
}
}
func TestInput_MultiLine_ShiftLineDown(t *testing.T) {
input := NewInput("", true)
input.addString(TESTING_LINE)
input.addString(NEW_LINE)
input.addString(TESTING_LINE)
if len(input.Lines()) != 2 {
t.Errorf("Expected 2 lines in input but found %d", len(input.Lines()))
}
if input.cursorLineIndex != 1 {
t.Errorf("Expected line cursor to be at index 1, found it at %d", input.cursorLineIndex)
}
input.moveUp()
input.addString(NEW_LINE)
if len(input.Lines()) != 3 {
t.Errorf("Expected 3 lines in input but found %d", len(input.Lines()))
}
if input.Lines()[0] != "" {
t.Errorf("Expected first line to be blank but found %s", input.Lines()[0])
}
}
func TestInput_MultiLine_MoveLeftToPreviousLine(t *testing.T) {
input := NewInput("", true)
input.addString(TESTING_LINE)
input.addString(NEW_LINE)
input.addString(TESTING_LINE)
if input.cursorLinePos != len(TESTING_LINE) {
t.Errorf("Expected cursor to be at position %d, found it at %d", len(TESTING_LINE), input.cursorLinePos)
}
// reset to 0 and move left
input.cursorLinePos = 0
input.moveLeft()
if input.cursorLineIndex != 0 {
t.Errorf("Expcted line cursor to be at index 0, found it at %d", input.cursorLineIndex)
}
if input.cursorLinePos != len(TESTING_LINE) {
t.Errorf("Expected cursor to be at position %d, found it at %d", len(TESTING_LINE), input.cursorLinePos)
}
}
// Test issues mentioned in the pull request: https://github.com/gizak/termui/pull/129
func TestInput_BackspaceOnFirstChar_NoAction(t *testing.T) {
input := NewInput("", false)
// backspace here should not throw an error
input.backspace()
input.addString(TESTING_LINE)
if input.cursorLinePos != len(TESTING_LINE) {
t.Errorf("Expected cursor to be at position %d, found it at %d", len(TESTING_LINE), input.cursorLinePos)
}
}
// Test issues mentioned in the pull request: https://github.com/gizak/termui/pull/129
func TestInput_LeftOnFirstChar_NoAction(t *testing.T) {
input := NewInput("", false)
// backspace here should not throw an error
input.moveLeft()
input.addString(TESTING_LINE)
if input.cursorLinePos != len(TESTING_LINE) {
t.Errorf("Expected cursor to be at position %d, found it at %d", len(TESTING_LINE), input.cursorLinePos)
}
}