This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobra-prompt_internal_test.go
68 lines (55 loc) · 1.91 KB
/
cobra-prompt_internal_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
package cobraprompt
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/verkada/go-prompt"
)
func newTestCommand(use string, short string) *cobra.Command {
return &cobra.Command{
Use: use,
Short: short,
Run: func(cmd *cobra.Command, args []string) {},
}
}
var rootCmd = newTestCommand("root", "The root cmd")
var getCmd = newTestCommand("get", "Get something")
var getObjectCmd = newTestCommand("object", "Get the object")
var getThingCmd = newTestCommand("thing", "The thing")
func init() {
rootCmd.AddCommand(getCmd)
getCmd.AddCommand(getObjectCmd)
getCmd.AddCommand(getThingCmd)
getObjectCmd.Flags().BoolP("verbose", "v", false, "Verbose log")
}
func TestFindSuggestions(t *testing.T) {
cp := &CobraPrompt{
RootCmd: rootCmd,
}
buf := prompt.NewBuffer()
buf.InsertText("", false, true)
suggestions := findSuggestions(cp, buf.Document())
hasLen := assert.Len(t, suggestions, 1, "Should find 1 suggestion")
if hasLen {
assert.Equal(t, getCmd.Name(), suggestions[0].Text, "Should find get command")
}
buf.InsertText("get ", false, true)
suggestions = findSuggestions(cp, buf.Document())
hasLen = assert.Len(t, suggestions, 2, "Should find 2 sub commands under get")
if hasLen {
assert.Equal(t, getObjectCmd.Name(), suggestions[0].Text, "Should find object command")
assert.Equal(t, getThingCmd.Name(), suggestions[1].Text, "Should find thing command")
}
buf.InsertText("object -", false, true)
suggestions = findSuggestions(cp, buf.Document())
hasLen = assert.Len(t, suggestions, 1, "Should find verbose flag")
if hasLen {
assert.Equal(t, "-v", suggestions[0].Text, "Should find verbose short flag")
}
buf.InsertText("-", false, true)
suggestions = findSuggestions(cp, buf.Document())
hasLen = assert.Len(t, suggestions, 1, "Should find verbose flag")
if hasLen {
assert.Equal(t, "--verbose", suggestions[0].Text, "Should find verbose flag")
}
}