Debian Codegraph @

complete_test.go

raw symbols SCIP
1 package opt_test
2
3 import (
4 "strings"
5 "testing"
6
7 "git.sr.ht/~rjarry/go-opt/v2"
8 "github.com/stretchr/testify/assert"
9 )
10
11 type CompleteStruct struct {
12 Name string `opt:"-n,--name" required:"true" complete:"CompleteName" desc:"Ninja turtle name."`
13 Delay float64 `opt:"--delay"`
14 Zero bool `opt:"-z" description:"Print zero values"`
15 Backoff bool `opt:"-B,--backoff" desc:"Increase delay on error"`
16 Tags []string `opt:"..." complete:"CompleteTag"`
17 }
18
19 func (c *CompleteStruct) CompleteName(arg string) []string {
20 var names []string
21 for _, n := range []string{"leonardo", "michelangelo", "rafaelo", "donatello"} {
22 if strings.HasPrefix(n, arg) {
23 names = append(names, n)
24 }
25 }
26 return names
27 }
28
29 func (c *CompleteStruct) CompleteTag(arg string) []string {
30 var results []string
31 prefix := ""
32 if strings.HasPrefix(arg, "-") {
33 prefix = "-"
34 } else if strings.HasPrefix(arg, "+") {
35 prefix = "+"
36 }
37 tags := []string{"unread", "sent", "important", "inbox", "trash"}
38 for _, t := range tags {
39 t = prefix + t
40 if strings.HasPrefix(t, arg) {
41 results = append(results, t)
42 }
43 }
44 return results
45 }
46
47 func TestComplete(t *testing.T) {
48 vectors := []struct {
49 cmdline string
50 completions []opt.Completion
51 prefix string
52 }{
53 {
54 "foo --delay 33..33.3 -n",
55 []opt.Completion{{Value: "-n ", Description: "Ninja turtle name."}},
56 "foo --delay 33..33.3 ",
57 },
58 {
59 "foo --delay 33..33.3 -n ",
60 []opt.Completion{
61 {Value: "leonardo", Description: "Ninja turtle name."},
62 {Value: "michelangelo", Description: "Ninja turtle name."},
63 {Value: "rafaelo", Description: "Ninja turtle name."},
64 {Value: "donatello", Description: "Ninja turtle name."},
65 },
66 "foo --delay 33..33.3 -n ",
67 },
68 {
69 "foo --delay 33..33.3 -n don",
70 []opt.Completion{{Value: "donatello", Description: "Ninja turtle name."}},
71 "foo --delay 33..33.3 -n ",
72 },
73 {
74 "foo --delay 33..33.3 --name=",
75 []opt.Completion{
76 {Value: "leonardo", Description: "Ninja turtle name."},
77 {Value: "michelangelo", Description: "Ninja turtle name."},
78 {Value: "rafaelo", Description: "Ninja turtle name."},
79 {Value: "donatello", Description: "Ninja turtle name."},
80 },
81 "foo --delay 33..33.3 --name=",
82 },
83 {
84 "foo --delay 33..33.3 --name=leo",
85 []opt.Completion{{Value: "leonardo", Description: "Ninja turtle name."}},
86 "foo --delay 33..33.3 --name=",
87 },
88 {
89 "foo --nam",
90 []opt.Completion{{Value: "--name ", Description: "Ninja turtle name."}},
91 "foo ",
92 },
93 {
94 "foo --delay 33..33.3 --backoff",
95 []opt.Completion{{Value: "--backoff ", Description: "Increase delay on error"}},
96 "foo --delay 33..33.3 ",
97 },
98 {
99 "foo --delay 33..33.3 -",
100 []opt.Completion{
101 {Value: "-unread"},
102 {Value: "-sent"},
103 {Value: "-important"},
104 {Value: "-inbox"},
105 {Value: "-trash"},
106 {Value: "-n ", Description: "Ninja turtle name."},
107 {Value: "--name ", Description: "Ninja turtle name."},
108 {Value: "-z ", Description: "Print zero values"},
109 {Value: "-B ", Description: "Increase delay on error"},
110 {Value: "--backoff ", Description: "Increase delay on error"},
111 },
112 "foo --delay 33..33.3 ",
113 },
114 {
115 "foo --delay 33..33.3 ",
116 []opt.Completion{
117 {Value: "unread"},
118 {Value: "sent"},
119 {Value: "important"},
120 {Value: "inbox"},
121 {Value: "trash"},
122 {Value: "-n ", Description: "Ninja turtle name."},
123 {Value: "--name ", Description: "Ninja turtle name."},
124 {Value: "-z ", Description: "Print zero values"},
125 {Value: "-B ", Description: "Increase delay on error"},
126 {Value: "--backoff ", Description: "Increase delay on error"},
127 },
128 "foo --delay 33..33.3 ",
129 },
130 {
131 "foo --delay 33..33.3 -n leonardo i",
132 []opt.Completion{{Value: "important"}, {Value: "inbox"}},
133 "foo --delay 33..33.3 -n leonardo ",
134 },
135 {
136 "foo +",
137 []opt.Completion{
138 {Value: "+unread"},
139 {Value: "+sent"},
140 {Value: "+important"},
141 {Value: "+inbox"},
142 {Value: "+trash"},
143 },
144 "foo ",
145 },
146 {
147 "foo -i",
148 []opt.Completion{{Value: "-important"}, {Value: "-inbox"}},
149 "foo ",
150 },
151 }
152
153 for _, v := range vectors {
154 t.Run(v.cmdline, func(t *testing.T) {
155 args := opt.LexArgs(v.cmdline)
156 spec := opt.NewCmdSpec(args.Arg(0), new(CompleteStruct))
157 completions, prefix := spec.GetCompletions(args)
158 assert.Equal(t, v.completions, completions)
159 assert.Equal(t, v.prefix, prefix)
160 })
161 }
162 }