-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathox_test.go
More file actions
213 lines (205 loc) · 5.07 KB
/
ox_test.go
File metadata and controls
213 lines (205 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package ox
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
)
func TestSuggestions(t *testing.T) {
for i, test := range []struct {
args []string
exp string
}{
{
ss(`subfoo`), `error: unknown command "subfoo" for "cmd"`,
},
{
ss(`on`), `error: unknown command "on" for "cmd"
Did you mean this?
one
`,
},
{
ss(`remove`), `error: unknown command "remove" for "cmd"
Did you mean this?
one
`,
},
{
ss(`rmove`), `error: unknown command "rmove" for "cmd"
Did you mean this?
one
`,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var code int
c := testContext(t, &code, test.args...)
err := c.Run(context.Background())
if err == nil {
t.Fatalf("expected non-nil error")
}
if !c.Handler(err) {
t.Fatalf("expected Handler to return true")
}
if code == 0 {
t.Fatalf("expected Handler to set non-zero code")
}
if s := strings.TrimSuffix(c.Stderr.(*bytes.Buffer).String(), "\n"); s != test.exp {
t.Errorf("\nexpected:\n%s\ngot:\n%s", test.exp, s)
}
})
}
}
func TestContextExpand(t *testing.T) {
root := &Command{
Name: "myApp",
}
configDir, err := userConfigDir()
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
cacheDir, err := userCacheDir()
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
tests := []struct {
v string
exp string
err error
}{
{``, ``, nil},
{`a`, `a`, nil},
{`$ABC`, `$ABC`, nil},
{`$HOME`, os.Getenv(`HOME`), nil},
{`$USER`, os.Getenv(`USER`), nil},
{`$APPNAME`, root.Name, nil},
{`$CONFIG`, configDir, nil},
{`$APPCONFIG`, filepath.Join(configDir, root.Name), nil},
{`$CACHE`, cacheDir, nil},
{`$APPCACHE`, filepath.Join(cacheDir, root.Name), nil},
{`$NUMCPU`, strconv.Itoa(runtime.NumCPU()), nil},
{`$NUMCPU2`, strconv.Itoa(runtime.NumCPU() + 2), nil},
{`$NUMCPU2X`, strconv.Itoa(runtime.NumCPU() * 2), nil},
{`$ARCH`, runtime.GOARCH, nil},
{`$OS`, runtime.GOOS, nil},
{`$ENV{HOME}`, os.Getenv(`HOME`), nil},
{`$ENV{NOT_DEFINED}`, ``, nil},
{`$MY_OVERRIDE{foo}`, `bar`, nil},
{`$HOME/$USER/$APPNAME`, os.Getenv("HOME") + "/" + os.Getenv("USER") + "/" + root.Name, nil},
{`$HOME$USER$APPNAME`, os.Getenv("HOME") + os.Getenv("USER") + root.Name, nil},
{`//$OS//`, "//" + runtime.GOOS + "//", nil},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Logf("test: %q", test.v)
ctx := &Context{
Root: root,
Loader: DefaultLoader,
Override: func(typ, key string) (string, bool) {
if typ == "MY_OVERRIDE" {
switch strings.ToLower(key) {
case "foo":
return "bar", true
}
}
return "", false
},
}
v, err := ctx.Expand(test.v)
switch {
case err != nil && !errors.Is(err, test.err):
t.Fatalf("expected error %v, got: %v", test.err, err)
case err == nil && test.err != nil:
t.Fatalf("expected error %v, got nil", test.err)
}
t.Logf("v: %v (%T)", v, v)
s, ok := v.(string)
if !ok {
t.Fatalf("expected string, got: %T", v)
}
if s != test.exp {
t.Errorf("expected %q, got: %q", test.exp, s)
}
})
}
}
func TestLdist(t *testing.T) {
tests := []struct {
a string
b string
exp int
}{
{"", "", 0},
{"", "a", 1},
{"ab", "aa", 1},
{"ab", "aaa", 2},
{"ab", "ba", 2},
{"a very long string that is meant to exceed", "another very long string that is meant to exceed", 6},
{"bar", "br", 1},
{"bbb", "a", 3},
{"book", "back", 2},
{"br", "bar", 1},
{"distance", "difference", 5},
{"fod", "Food", 1},
{"foo", "", 3},
{"foo", "bar", 3},
{"foo", "Food", 1},
{"Hafþór Júlíus Björnsson", "Hafþor Julius Bjornsson", 4},
{"", "hello", 5},
{"hello", "hello", 0},
{"kitten", "sitting", 3},
{"levenshtein", "frankenstein", 6},
{"mississippi", "swiss miss", 8},
{"resume and cafe", "resumé and café", 2},
{"resume and cafe", "resumes and cafes", 2},
{"resumé and café", "resumés and cafés", 2},
{"rosettacode", "raisethysword", 8},
{"saturday", "sunday", 3},
{"sitten", "sittin", 1},
{"sittin", "sitting", 1},
{"sleep", "fleeting", 5},
{"stop", "tops", 2},
{"test", "t", 3},
{"།་གམ་འས་པ་་མ།", "།་གམའས་པ་་མ", 2},
{"👀😀", "😀", 1},
}
for _, test := range tests {
t.Run(test.a+"::"+test.b, func(t *testing.T) {
a, b := []rune(strings.ToLower(test.a)), []rune(strings.ToLower(test.b))
if i := Ldist(a, b); i != test.exp {
t.Errorf("expected %d, got: %d", test.exp, i)
}
if i := Ldist(b, a); i != test.exp {
t.Errorf("expected %d, got: %d", test.exp, i)
}
})
}
}
func testContext(t *testing.T, code *int, args ...string) *Context {
t.Helper()
cmd := testCommand(t)
cmd.Exec = nil
c := &Context{
Exit: func(c int) {
*code = c
},
Panic: func(v any) {
t.Fatalf("context panic: %v", v)
},
Root: cmd,
Stderr: new(bytes.Buffer),
Args: args,
Vars: make(Vars),
}
c.Handler = DefaultErrorHandler(c)
if err := c.Parse(); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
return c
}