1package cobra
2
3import (
4	"bytes"
5	"strings"
6	"testing"
7)
8
9func TestPowerShellCompletion(t *testing.T) {
10	tcs := []struct {
11		name                string
12		root                *Command
13		expectedExpressions []string
14	}{
15		{
16			name: "trivial",
17			root: &Command{Use: "trivialapp"},
18			expectedExpressions: []string{
19				"Register-ArgumentCompleter -Native -CommandName 'trivialapp' -ScriptBlock",
20				"$command = @(\n        'trivialapp'\n",
21			},
22		},
23		{
24			name: "tree",
25			root: func() *Command {
26				r := &Command{Use: "tree"}
27
28				sub1 := &Command{Use: "sub1"}
29				r.AddCommand(sub1)
30
31				sub11 := &Command{Use: "sub11"}
32				sub12 := &Command{Use: "sub12"}
33
34				sub1.AddCommand(sub11)
35				sub1.AddCommand(sub12)
36
37				sub2 := &Command{Use: "sub2"}
38				r.AddCommand(sub2)
39
40				sub21 := &Command{Use: "sub21"}
41				sub22 := &Command{Use: "sub22"}
42
43				sub2.AddCommand(sub21)
44				sub2.AddCommand(sub22)
45
46				return r
47			}(),
48			expectedExpressions: []string{
49				"'tree'",
50				"[CompletionResult]::new('sub1', 'sub1', [CompletionResultType]::ParameterValue, '')",
51				"[CompletionResult]::new('sub2', 'sub2', [CompletionResultType]::ParameterValue, '')",
52				"'tree;sub1'",
53				"[CompletionResult]::new('sub11', 'sub11', [CompletionResultType]::ParameterValue, '')",
54				"[CompletionResult]::new('sub12', 'sub12', [CompletionResultType]::ParameterValue, '')",
55				"'tree;sub1;sub11'",
56				"'tree;sub1;sub12'",
57				"'tree;sub2'",
58				"[CompletionResult]::new('sub21', 'sub21', [CompletionResultType]::ParameterValue, '')",
59				"[CompletionResult]::new('sub22', 'sub22', [CompletionResultType]::ParameterValue, '')",
60				"'tree;sub2;sub21'",
61				"'tree;sub2;sub22'",
62			},
63		},
64		{
65			name: "flags",
66			root: func() *Command {
67				r := &Command{Use: "flags"}
68				r.Flags().StringP("flag1", "a", "", "")
69				r.Flags().String("flag2", "", "")
70
71				sub1 := &Command{Use: "sub1"}
72				sub1.Flags().StringP("flag3", "c", "", "")
73				r.AddCommand(sub1)
74
75				return r
76			}(),
77			expectedExpressions: []string{
78				"'flags'",
79				"[CompletionResult]::new('-a', 'a', [CompletionResultType]::ParameterName, '')",
80				"[CompletionResult]::new('--flag1', 'flag1', [CompletionResultType]::ParameterName, '')",
81				"[CompletionResult]::new('--flag2', 'flag2', [CompletionResultType]::ParameterName, '')",
82				"[CompletionResult]::new('sub1', 'sub1', [CompletionResultType]::ParameterValue, '')",
83				"'flags;sub1'",
84				"[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, '')",
85				"[CompletionResult]::new('--flag3', 'flag3', [CompletionResultType]::ParameterName, '')",
86			},
87		},
88		{
89			name: "usage",
90			root: func() *Command {
91				r := &Command{Use: "usage"}
92				r.Flags().String("flag", "", "this describes the usage of the 'flag' flag")
93
94				sub1 := &Command{
95					Use:   "sub1",
96					Short: "short describes 'sub1'",
97				}
98				r.AddCommand(sub1)
99
100				return r
101			}(),
102			expectedExpressions: []string{
103				"[CompletionResult]::new('--flag', 'flag', [CompletionResultType]::ParameterName, 'this describes the usage of the ''flag'' flag')",
104				"[CompletionResult]::new('sub1', 'sub1', [CompletionResultType]::ParameterValue, 'short describes ''sub1''')",
105			},
106		},
107	}
108
109	for _, tc := range tcs {
110		t.Run(tc.name, func(t *testing.T) {
111			buf := new(bytes.Buffer)
112			tc.root.GenPowerShellCompletion(buf)
113			output := buf.String()
114
115			for _, expectedExpression := range tc.expectedExpressions {
116				if !strings.Contains(output, expectedExpression) {
117					t.Errorf("Expected completion to contain %q somewhere; got %q", expectedExpression, output)
118				}
119			}
120		})
121	}
122}
123