summaryrefslogtreecommitdiff
path: root/complete_test.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-08-24 23:05:41 +0300
committerGitHub <[email protected]>2017-08-24 23:05:41 +0300
commit5075f6d6e69fb7b5a20a79ca0b87b16a484e2d2a (patch)
tree6b8f907761228fc3693b41a04a56e993fb9a2f38 /complete_test.go
parentf4461a52b6329c11190f11fe3384ec8aa964e21c (diff)
parent1c43d25e3519cc70fc238d669f7b2aa826ac716a (diff)
Merge pull request #48 from dadgar/b-shared-prefix
Fix a subcommand matching
Diffstat (limited to 'complete_test.go')
-rw-r--r--complete_test.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/complete_test.go b/complete_test.go
index 135c6ad..1a42da1 100644
--- a/complete_test.go
+++ b/complete_test.go
@@ -184,6 +184,81 @@ func TestCompleter_Complete(t *testing.T) {
}
}
+func TestCompleter_Complete_SharedPrefix(t *testing.T) {
+ t.Parallel()
+ initTests()
+
+ c := Command{
+ Sub: Commands{
+ "status": {
+ Flags: Flags{
+ "-f3": PredictNothing,
+ },
+ },
+ "job": {
+ Sub: Commands{
+ "status": {
+ Flags: Flags{
+ "-f4": PredictNothing,
+ },
+ },
+ },
+ },
+ },
+ Flags: Flags{
+ "-o": PredictFiles("*.txt"),
+ },
+ GlobalFlags: Flags{
+ "-h": PredictNothing,
+ "-global1": PredictAnything,
+ },
+ }
+
+ tests := []struct {
+ args string
+ want []string
+ }{
+ {
+ args: "",
+ want: []string{"status", "job", "-h", "-global1", "-o"},
+ },
+ {
+ args: "-",
+ want: []string{"-h", "-global1", "-o"},
+ },
+ {
+ args: "j",
+ want: []string{"job"},
+ },
+ {
+ args: "job ",
+ want: []string{"-h", "-global1", "status"},
+ },
+ {
+ args: "job status ",
+ want: []string{"-f4", "-h", "-global1"},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.args, func(t *testing.T) {
+
+ tt.args = "cmd " + tt.args
+ os.Setenv(envComplete, tt.args)
+ line, _ := getLine()
+
+ got := c.Predict(newArgs(line))
+
+ sort.Strings(tt.want)
+ sort.Strings(got)
+
+ if !equalSlices(got, tt.want) {
+ t.Errorf("failed '%s'\ngot = %s\nwant: %s", t.Name(), got, tt.want)
+ }
+ })
+ }
+}
+
func equalSlices(a, b []string) bool {
if len(a) != len(b) {
return false