summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/spf13/cobra/command_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/spf13/cobra/command_test.go')
-rw-r--r--vendor/github.com/spf13/cobra/command_test.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/vendor/github.com/spf13/cobra/command_test.go b/vendor/github.com/spf13/cobra/command_test.go
index d3dde1525..d874a9a51 100644
--- a/vendor/github.com/spf13/cobra/command_test.go
+++ b/vendor/github.com/spf13/cobra/command_test.go
@@ -1563,3 +1563,66 @@ func TestUpdateName(t *testing.T) {
t.Error("c.Name() should be updated on changed c.Use")
}
}
+
+type calledAsTestcase struct {
+ args []string
+ call string
+ want string
+ epm bool
+ tc bool
+}
+
+func (tc *calledAsTestcase) test(t *testing.T) {
+ defer func(ov bool) { EnablePrefixMatching = ov }(EnablePrefixMatching)
+ EnablePrefixMatching = tc.epm
+
+ var called *Command
+ run := func(c *Command, _ []string) { t.Logf("called: %q", c.Name()); called = c }
+
+ parent := &Command{Use: "parent", Run: run}
+ child1 := &Command{Use: "child1", Run: run, Aliases: []string{"this"}}
+ child2 := &Command{Use: "child2", Run: run, Aliases: []string{"that"}}
+
+ parent.AddCommand(child1)
+ parent.AddCommand(child2)
+ parent.SetArgs(tc.args)
+
+ output := new(bytes.Buffer)
+ parent.SetOutput(output)
+
+ parent.Execute()
+
+ if called == nil {
+ if tc.call != "" {
+ t.Errorf("missing expected call to command: %s", tc.call)
+ }
+ return
+ }
+
+ if called.Name() != tc.call {
+ t.Errorf("called command == %q; Wanted %q", called.Name(), tc.call)
+ } else if got := called.CalledAs(); got != tc.want {
+ t.Errorf("%s.CalledAs() == %q; Wanted: %q", tc.call, got, tc.want)
+ }
+}
+
+func TestCalledAs(t *testing.T) {
+ tests := map[string]calledAsTestcase{
+ "find/no-args": {nil, "parent", "parent", false, false},
+ "find/real-name": {[]string{"child1"}, "child1", "child1", false, false},
+ "find/full-alias": {[]string{"that"}, "child2", "that", false, false},
+ "find/part-no-prefix": {[]string{"thi"}, "", "", false, false},
+ "find/part-alias": {[]string{"thi"}, "child1", "this", true, false},
+ "find/conflict": {[]string{"th"}, "", "", true, false},
+ "traverse/no-args": {nil, "parent", "parent", false, true},
+ "traverse/real-name": {[]string{"child1"}, "child1", "child1", false, true},
+ "traverse/full-alias": {[]string{"that"}, "child2", "that", false, true},
+ "traverse/part-no-prefix": {[]string{"thi"}, "", "", false, true},
+ "traverse/part-alias": {[]string{"thi"}, "child1", "this", true, true},
+ "traverse/conflict": {[]string{"th"}, "", "", true, true},
+ }
+
+ for name, tc := range tests {
+ t.Run(name, tc.test)
+ }
+}