OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / vendor / github.com / spf13 / cobra / zsh_completions_test.go
1 package cobra
2
3 import (
4         "bytes"
5         "strings"
6         "testing"
7 )
8
9 func TestZshCompletion(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{"#compdef trivial"},
19                 },
20                 {
21                         name: "linear",
22                         root: func() *Command {
23                                 r := &Command{Use: "linear"}
24
25                                 sub1 := &Command{Use: "sub1"}
26                                 r.AddCommand(sub1)
27
28                                 sub2 := &Command{Use: "sub2"}
29                                 sub1.AddCommand(sub2)
30
31                                 sub3 := &Command{Use: "sub3"}
32                                 sub2.AddCommand(sub3)
33                                 return r
34                         }(),
35                         expectedExpressions: []string{"sub1", "sub2", "sub3"},
36                 },
37                 {
38                         name: "flat",
39                         root: func() *Command {
40                                 r := &Command{Use: "flat"}
41                                 r.AddCommand(&Command{Use: "c1"})
42                                 r.AddCommand(&Command{Use: "c2"})
43                                 return r
44                         }(),
45                         expectedExpressions: []string{"(c1 c2)"},
46                 },
47                 {
48                         name: "tree",
49                         root: func() *Command {
50                                 r := &Command{Use: "tree"}
51
52                                 sub1 := &Command{Use: "sub1"}
53                                 r.AddCommand(sub1)
54
55                                 sub11 := &Command{Use: "sub11"}
56                                 sub12 := &Command{Use: "sub12"}
57
58                                 sub1.AddCommand(sub11)
59                                 sub1.AddCommand(sub12)
60
61                                 sub2 := &Command{Use: "sub2"}
62                                 r.AddCommand(sub2)
63
64                                 sub21 := &Command{Use: "sub21"}
65                                 sub22 := &Command{Use: "sub22"}
66
67                                 sub2.AddCommand(sub21)
68                                 sub2.AddCommand(sub22)
69
70                                 return r
71                         }(),
72                         expectedExpressions: []string{"(sub11 sub12)", "(sub21 sub22)"},
73                 },
74         }
75
76         for _, tc := range tcs {
77                 t.Run(tc.name, func(t *testing.T) {
78                         buf := new(bytes.Buffer)
79                         tc.root.GenZshCompletion(buf)
80                         completion := buf.String()
81                         for _, expectedExpression := range tc.expectedExpressions {
82                                 if !strings.Contains(completion, expectedExpression) {
83                                         t.Errorf("expected completion to contain '%v' somewhere; got '%v'", expectedExpression, completion)
84                                 }
85                         }
86                 })
87         }
88 }