forked from manifoldco/promptui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursor_test.go
103 lines (89 loc) · 2.65 KB
/
cursor_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
package promptui
import "testing"
func TestDefinedCursors(t *testing.T) {
t.Run("pipeCursor", func(t *testing.T) {
p := string(pipeCursor([]rune{}))
if p != "|" {
t.Fatalf("%x!=%x", "|", p)
}
})
}
func TestCursor(t *testing.T) {
t.Run("empty", func(t *testing.T) {
cursor := Cursor{Cursor: pipeCursor}
cursor.End()
f := cursor.Format()
if f != "|" {
t.Errorf("% x!=% x", "|", cursor.Format())
}
cursor.Update("sup")
if cursor.Format() != "sup|" {
t.Errorf("% x!=% x", "sup|", cursor.Format())
}
})
t.Run("Cursor at end, append additional", func(t *testing.T) {
cursor := Cursor{input: []rune("a"), Cursor: pipeCursor}
cursor.End()
f := cursor.Format()
if f != "a|" {
t.Errorf("% x!=% x", "a|", cursor.Format())
}
cursor.Update(" hi")
if cursor.Format() != "a hi|" {
t.Errorf("% x!=% x", "a hi!", cursor.Format())
}
})
t.Run("Cursor at at end, backspace", func(t *testing.T) {
cursor := Cursor{input: []rune("default"), Cursor: pipeCursor}
cursor.Place(len(cursor.input))
cursor.Backspace()
if cursor.Format() != "defaul|" {
t.Errorf("expected defaul|; found %s", cursor.Format())
}
cursor.Update(" hi")
if cursor.Format() != "defaul hi|" {
t.Errorf("expected 'defaul hi|'; found '%s'", cursor.Format())
}
})
t.Run("Cursor at beginning, append additional", func(t *testing.T) {
cursor := Cursor{input: []rune("default"), Cursor: pipeCursor}
t.Log("init", cursor.String())
cursor.Backspace()
if cursor.Format() != "|default" {
t.Errorf("expected |default; found %s", cursor.Format())
}
cursor.Update("hi ")
t.Log("after add", cursor.String())
if cursor.Format() != "hi |default" {
t.Errorf("expected 'hi |default'; found '%s'", cursor.Format())
}
cursor.Backspace()
t.Log("after backspace", cursor.String())
if cursor.Format() != "hi|default" {
t.Errorf("expected 'hi|default'; found '%s'", cursor.Format())
}
cursor.Backspace()
t.Log("after backspace", cursor.String())
if cursor.Format() != "h|default" {
t.Errorf("expected 'h|default'; found '%s'", cursor.Format())
}
})
t.Run("Move", func(t *testing.T) {
cursor := Cursor{input: []rune("default"), Cursor: pipeCursor}
if cursor.Format() != "|default" {
t.Errorf("expected |default; found %s", cursor.Format())
}
cursor.Move(-1)
if cursor.Format() != "|default" {
t.Errorf("moved backwards from beginning |default; found %s", cursor.Format())
}
cursor.Move(1)
if cursor.Format() != "d|efault" {
t.Errorf("expected 'd|efault'; found '%s'", cursor.Format())
}
cursor.Move(10)
if cursor.Format() != "default|" {
t.Errorf("expected 'default|'; found '%s'", cursor.Format())
}
})
}