1package cobra
2
3import (
4	"strings"
5	"testing"
6)
7
8func TestNoArgs(t *testing.T) {
9	c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
10
11	output, err := executeCommand(c)
12	if output != "" {
13		t.Errorf("Unexpected string: %v", output)
14	}
15	if err != nil {
16		t.Fatalf("Unexpected error: %v", err)
17	}
18}
19
20func TestNoArgsWithArgs(t *testing.T) {
21	c := &Command{Use: "c", Args: NoArgs, Run: emptyRun}
22
23	_, err := executeCommand(c, "illegal")
24	if err == nil {
25		t.Fatal("Expected an error")
26	}
27
28	got := err.Error()
29	expected := `unknown command "illegal" for "c"`
30	if got != expected {
31		t.Errorf("Expected: %q, got: %q", expected, got)
32	}
33}
34
35func TestOnlyValidArgs(t *testing.T) {
36	c := &Command{
37		Use:       "c",
38		Args:      OnlyValidArgs,
39		ValidArgs: []string{"one", "two"},
40		Run:       emptyRun,
41	}
42
43	output, err := executeCommand(c, "one", "two")
44	if output != "" {
45		t.Errorf("Unexpected output: %v", output)
46	}
47	if err != nil {
48		t.Fatalf("Unexpected error: %v", err)
49	}
50}
51
52func TestOnlyValidArgsWithInvalidArgs(t *testing.T) {
53	c := &Command{
54		Use:       "c",
55		Args:      OnlyValidArgs,
56		ValidArgs: []string{"one", "two"},
57		Run:       emptyRun,
58	}
59
60	_, err := executeCommand(c, "three")
61	if err == nil {
62		t.Fatal("Expected an error")
63	}
64
65	got := err.Error()
66	expected := `invalid argument "three" for "c"`
67	if got != expected {
68		t.Errorf("Expected: %q, got: %q", expected, got)
69	}
70}
71
72func TestArbitraryArgs(t *testing.T) {
73	c := &Command{Use: "c", Args: ArbitraryArgs, Run: emptyRun}
74	output, err := executeCommand(c, "a", "b")
75	if output != "" {
76		t.Errorf("Unexpected output: %v", output)
77	}
78	if err != nil {
79		t.Errorf("Unexpected error: %v", err)
80	}
81}
82
83func TestMinimumNArgs(t *testing.T) {
84	c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
85	output, err := executeCommand(c, "a", "b", "c")
86	if output != "" {
87		t.Errorf("Unexpected output: %v", output)
88	}
89	if err != nil {
90		t.Errorf("Unexpected error: %v", err)
91	}
92}
93
94func TestMinimumNArgsWithLessArgs(t *testing.T) {
95	c := &Command{Use: "c", Args: MinimumNArgs(2), Run: emptyRun}
96	_, err := executeCommand(c, "a")
97
98	if err == nil {
99		t.Fatal("Expected an error")
100	}
101
102	got := err.Error()
103	expected := "requires at least 2 arg(s), only received 1"
104	if got != expected {
105		t.Fatalf("Expected %q, got %q", expected, got)
106	}
107}
108
109func TestMaximumNArgs(t *testing.T) {
110	c := &Command{Use: "c", Args: MaximumNArgs(3), Run: emptyRun}
111	output, err := executeCommand(c, "a", "b")
112	if output != "" {
113		t.Errorf("Unexpected output: %v", output)
114	}
115	if err != nil {
116		t.Errorf("Unexpected error: %v", err)
117	}
118}
119
120func TestMaximumNArgsWithMoreArgs(t *testing.T) {
121	c := &Command{Use: "c", Args: MaximumNArgs(2), Run: emptyRun}
122	_, err := executeCommand(c, "a", "b", "c")
123
124	if err == nil {
125		t.Fatal("Expected an error")
126	}
127
128	got := err.Error()
129	expected := "accepts at most 2 arg(s), received 3"
130	if got != expected {
131		t.Fatalf("Expected %q, got %q", expected, got)
132	}
133}
134
135func TestExactArgs(t *testing.T) {
136	c := &Command{Use: "c", Args: ExactArgs(3), Run: emptyRun}
137	output, err := executeCommand(c, "a", "b", "c")
138	if output != "" {
139		t.Errorf("Unexpected output: %v", output)
140	}
141	if err != nil {
142		t.Errorf("Unexpected error: %v", err)
143	}
144}
145
146func TestExactArgsWithInvalidCount(t *testing.T) {
147	c := &Command{Use: "c", Args: ExactArgs(2), Run: emptyRun}
148	_, err := executeCommand(c, "a", "b", "c")
149
150	if err == nil {
151		t.Fatal("Expected an error")
152	}
153
154	got := err.Error()
155	expected := "accepts 2 arg(s), received 3"
156	if got != expected {
157		t.Fatalf("Expected %q, got %q", expected, got)
158	}
159}
160
161func TestRangeArgs(t *testing.T) {
162	c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
163	output, err := executeCommand(c, "a", "b", "c")
164	if output != "" {
165		t.Errorf("Unexpected output: %v", output)
166	}
167	if err != nil {
168		t.Errorf("Unexpected error: %v", err)
169	}
170}
171
172func TestRangeArgsWithInvalidCount(t *testing.T) {
173	c := &Command{Use: "c", Args: RangeArgs(2, 4), Run: emptyRun}
174	_, err := executeCommand(c, "a")
175
176	if err == nil {
177		t.Fatal("Expected an error")
178	}
179
180	got := err.Error()
181	expected := "accepts between 2 and 4 arg(s), received 1"
182	if got != expected {
183		t.Fatalf("Expected %q, got %q", expected, got)
184	}
185}
186
187func TestRootTakesNoArgs(t *testing.T) {
188	rootCmd := &Command{Use: "root", Run: emptyRun}
189	childCmd := &Command{Use: "child", Run: emptyRun}
190	rootCmd.AddCommand(childCmd)
191
192	_, err := executeCommand(rootCmd, "illegal", "args")
193	if err == nil {
194		t.Fatal("Expected an error")
195	}
196
197	got := err.Error()
198	expected := `unknown command "illegal" for "root"`
199	if !strings.Contains(got, expected) {
200		t.Errorf("expected %q, got %q", expected, got)
201	}
202}
203
204func TestRootTakesArgs(t *testing.T) {
205	rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}
206	childCmd := &Command{Use: "child", Run: emptyRun}
207	rootCmd.AddCommand(childCmd)
208
209	_, err := executeCommand(rootCmd, "legal", "args")
210	if err != nil {
211		t.Errorf("Unexpected error: %v", err)
212	}
213}
214
215func TestChildTakesNoArgs(t *testing.T) {
216	rootCmd := &Command{Use: "root", Run: emptyRun}
217	childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}
218	rootCmd.AddCommand(childCmd)
219
220	_, err := executeCommand(rootCmd, "child", "illegal", "args")
221	if err == nil {
222		t.Fatal("Expected an error")
223	}
224
225	got := err.Error()
226	expected := `unknown command "illegal" for "root child"`
227	if !strings.Contains(got, expected) {
228		t.Errorf("expected %q, got %q", expected, got)
229	}
230}
231
232func TestChildTakesArgs(t *testing.T) {
233	rootCmd := &Command{Use: "root", Run: emptyRun}
234	childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}
235	rootCmd.AddCommand(childCmd)
236
237	_, err := executeCommand(rootCmd, "child", "legal", "args")
238	if err != nil {
239		t.Fatalf("Unexpected error: %v", err)
240	}
241}
242