-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathox_test.go
135 lines (128 loc) · 2.99 KB
/
ox_test.go
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
package ox
import (
"bytes"
"context"
"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 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
}