forked from carapace-sh/carapace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat_test.go
63 lines (47 loc) · 1.22 KB
/
compat_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
package carapace
import (
"io"
"os"
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestRegisterValidArgsFunction(t *testing.T) {
cmd := &cobra.Command{}
Gen(cmd).PositionalCompletion(
ActionValues("1"),
ActionValuesDescribed("2", "second"),
)
Gen(cmd).PositionalAnyCompletion(
ActionValues("any"),
)
registerValidArgsFunction(cmd)
if vals, _ := cmd.ValidArgsFunction(cmd, []string{}, ""); vals[0] != "1" {
t.Error("first position wrong")
}
if vals, _ := cmd.ValidArgsFunction(cmd, []string{""}, ""); vals[0] != "2\tsecond" {
t.Error("second position wrong")
}
if vals, _ := cmd.ValidArgsFunction(cmd, []string{"", ""}, ""); vals[0] != "any" {
t.Error("third position wrong")
}
}
func TestRegisterFlagCompletion(t *testing.T) {
cmd := &cobra.Command{}
cmd.Flags().String("flag", "", "")
Gen(cmd).FlagCompletion(ActionMap{
"flag": ActionValuesDescribed("1", "one"),
})
registerFlagCompletion(cmd)
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
os.Args = []string{"", "__complete", "--flag", ""}
_ = cmd.Execute()
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout
if lines := strings.Split(string(out), "\n"); lines[0] != "1\tone" {
t.Error("flag wrong")
}
}