forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_search_test.go
48 lines (38 loc) · 1.16 KB
/
reverse_search_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
package prompt
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRevSearch(t *testing.T) {
history := NewHistory()
history.Add("aa")
history.Add("ab")
history.Add("ac")
history.Add("ad")
history.Add("ae")
revSearchState := NewReverseSearch(history)
assert.Equal(t, len(history.tmp)-1, revSearchState.searchFromIndex)
assert.Equal(t, "", revSearchState.matchedCmd)
assert.Equal(t, -1, revSearchState.matchedIndex)
// Update 1.
revSearchState.update("a")
assert.Equal(t, "ae", revSearchState.matchedCmd)
assert.Equal(t, 4, revSearchState.matchedIndex)
assert.Equal(t, 5, revSearchState.searchFromIndex)
// Activate again.
revSearchState.reducePrefix()
assert.Equal(t, 3, revSearchState.searchFromIndex)
// Update 2.
revSearchState.update("a")
assert.Equal(t, "ad", revSearchState.matchedCmd)
assert.Equal(t, 3, revSearchState.matchedIndex)
// Accurate update.
revSearchState.update("aa")
assert.Equal(t, "aa", revSearchState.matchedCmd)
assert.Equal(t, 0, revSearchState.matchedIndex)
// Activate too many times.
for i := 0; i < 10; i++ {
revSearchState.reducePrefix()
}
assert.Equal(t, 0, revSearchState.searchFromIndex)
}