forked from peco/peco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_test.go
166 lines (131 loc) · 3.86 KB
/
action_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
package peco
import (
"testing"
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
func TestActionNames(t *testing.T) {
// These names MUST exist
names := []string{
"peco.ForwardChar",
"peco.BackwardChar",
"peco.ForwardWord",
"peco.BackwardWord",
"peco.BeginningOfLine",
"peco.EndOfLine",
"peco.EndOfFile",
"peco.DeleteForwardChar",
"peco.DeleteBackwardChar",
"peco.DeleteForwardWord",
"peco.DeleteBackwardWord",
"peco.KillEndOfLine",
"peco.DeleteAll",
"peco.SelectPreviousPage",
"peco.SelectNextPage",
"peco.SelectPrevious",
"peco.SelectNext",
"peco.ToggleSelection",
"peco.ToggleSelectionAndSelectNext",
"peco.RotateMatcher",
"peco.Finish",
"peco.Cancel",
}
for _, name := range names {
if _, ok := nameToActions[name]; !ok {
t.Errorf("Action %s should exist, but it does not", name)
}
}
}
func expectCaretPos(t *testing.T, c interface {
CaretPos() int
}, expect int) bool {
if c.CaretPos() != expect {
t.Errorf("Expected caret position %d, got %d", expect, c.CaretPos())
return false
}
return true
}
func expectQueryString(t *testing.T, c interface {
QueryString() string
}, expect string) bool {
if c.QueryString() != expect {
t.Errorf("Expected '%s', got '%s'", expect, c.QueryString())
return false
}
return true
}
func TestDoDeleteForwardChar(t *testing.T) {
ctx := NewCtx(nil)
input := ctx.NewInput()
ctx.SetQuery([]rune("Hello, World!"))
ctx.SetCaretPos(5)
doDeleteForwardChar(input, termbox.Event{})
expectQueryString(t, ctx, "Hello World!")
expectCaretPos(t, ctx, 5)
ctx.SetCaretPos(runewidth.StringWidth(ctx.QueryString()))
doDeleteForwardChar(input, termbox.Event{})
expectQueryString(t, ctx, "Hello World!")
expectCaretPos(t, ctx, runewidth.StringWidth(ctx.QueryString()))
ctx.SetCaretPos(0)
doDeleteForwardChar(input, termbox.Event{})
expectQueryString(t, ctx, "ello World!")
expectCaretPos(t, ctx, 0)
}
func TestDoDeleteForwardWord(t *testing.T) {
ctx := NewCtx(nil)
input := ctx.NewInput()
ctx.SetQuery([]rune("Hello, World!"))
ctx.SetCaretPos(5)
doDeleteForwardWord(input, termbox.Event{})
expectQueryString(t, ctx, "Hello World!")
expectCaretPos(t, ctx, 5)
ctx.SetCaretPos(runewidth.StringWidth(ctx.QueryString()))
doDeleteForwardWord(input, termbox.Event{})
expectQueryString(t, ctx, "Hello World!")
expectCaretPos(t, ctx, runewidth.StringWidth(ctx.QueryString()))
ctx.SetCaretPos(0)
doDeleteForwardWord(input, termbox.Event{})
expectQueryString(t, ctx, " World!")
expectCaretPos(t, ctx, 0)
ctx.SetCaretPos(1)
doDeleteForwardWord(input, termbox.Event{})
expectQueryString(t, ctx, " ")
}
func TestDoDeleteBackwardChar(t *testing.T) {
ctx := NewCtx(nil)
input := ctx.NewInput()
ctx.SetQuery([]rune("Hello, World!"))
ctx.SetCaretPos(5)
doDeleteBackwardChar(input, termbox.Event{})
expectQueryString(t, ctx, "Hell, World!")
expectCaretPos(t, ctx, 4)
ctx.SetCaretPos(runewidth.StringWidth(ctx.QueryString()))
doDeleteBackwardChar(input, termbox.Event{})
expectQueryString(t, ctx, "Hell, World")
expectCaretPos(t, ctx, runewidth.StringWidth(ctx.QueryString()))
ctx.SetCaretPos(0)
doDeleteBackwardChar(input, termbox.Event{})
expectQueryString(t, ctx, "Hell, World")
expectCaretPos(t, ctx, 0)
}
func TestDoDeleteBackwardWord(t *testing.T) {
ctx := NewCtx(nil)
input := ctx.NewInput()
// In case of an overflow (bug)
ctx.SetQuery([]rune("foo"))
ctx.SetCaretPos(5)
doDeleteBackwardWord(input, termbox.Event{})
// https://github.com/peco/peco/pull/184#issuecomment-54026739
// Case 1. " foo<caret>" -> " "
ctx.SetQuery([]rune(" foo"))
ctx.SetCaretPos(4)
doDeleteBackwardWord(input, termbox.Event{})
expectQueryString(t, ctx, " ")
expectCaretPos(t, ctx, 1)
// Case 2. "foo bar<caret>" -> "foo "
ctx.SetQuery([]rune("foo bar"))
ctx.SetCaretPos(7)
doDeleteBackwardWord(input, termbox.Event{})
expectQueryString(t, ctx, "foo ")
expectCaretPos(t, ctx, 4)
}