-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
suggestions_test.go
152 lines (127 loc) · 2.75 KB
/
suggestions_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package cli
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJaroWinkler(t *testing.T) {
// Given
for _, testCase := range []struct {
a, b string
expected float64
}{
{"", "", 1},
{"a", "", 0},
{"", "a", 0},
{"a", "a", 1},
{"a", "b", 0},
{"aa", "aa", 1},
{"aa", "bb", 0},
{"aaa", "aaa", 1},
{"aa", "ab", 0.6666666666666666},
{"aa", "ba", 0.6666666666666666},
{"ba", "aa", 0.6666666666666666},
{"ab", "aa", 0.6666666666666666},
} {
// When
res := jaroWinkler(testCase.a, testCase.b)
// Then
assert.Equal(t, testCase.expected, res)
}
}
func TestSuggestFlag(t *testing.T) {
// Given
app := buildExtendedTestCommand()
for _, testCase := range []struct {
provided, expected string
}{
{"", ""},
{"a", "--another-flag"},
{"hlp", "--help"},
{"k", ""},
{"s", "-s"},
} {
// When
res := suggestFlag(app.Flags, testCase.provided, false)
// Then
assert.Equal(t, testCase.expected, res)
}
}
func TestSuggestFlagHideHelp(t *testing.T) {
// Given
app := buildExtendedTestCommand()
// When
res := suggestFlag(app.Flags, "hlp", true)
// Then
assert.Equal(t, "--fl", res)
}
func TestSuggestFlagFromError(t *testing.T) {
// Given
app := buildExtendedTestCommand()
for _, testCase := range []struct {
command, provided, expected string
}{
{"", "hel", "--help"},
{"", "soccer", "--socket"},
{"config", "anot", "--another-flag"},
} {
// When
res, _ := app.suggestFlagFromError(
errors.New(providedButNotDefinedErrMsg+testCase.provided),
testCase.command,
)
// Then
assert.Equal(t, fmt.Sprintf(SuggestDidYouMeanTemplate+"\n\n", testCase.expected), res)
}
}
func TestSuggestFlagFromErrorWrongError(t *testing.T) {
// Given
app := buildExtendedTestCommand()
// When
_, err := app.suggestFlagFromError(errors.New("invalid"), "")
// Then
assert.Error(t, err)
}
func TestSuggestFlagFromErrorWrongCommand(t *testing.T) {
// Given
app := buildExtendedTestCommand()
// When
_, err := app.suggestFlagFromError(
errors.New(providedButNotDefinedErrMsg+"flag"),
"invalid",
)
// Then
assert.Error(t, err)
}
func TestSuggestFlagFromErrorNoSuggestion(t *testing.T) {
// Given
app := buildExtendedTestCommand()
// When
_, err := app.suggestFlagFromError(
errors.New(providedButNotDefinedErrMsg+""),
"",
)
// Then
assert.Error(t, err)
}
func TestSuggestCommand(t *testing.T) {
// Given
app := buildExtendedTestCommand()
for _, testCase := range []struct {
provided, expected string
}{
{"", ""},
{"conf", "config"},
{"i", "i"},
{"information", "info"},
{"inf", "info"},
{"con", "config"},
{"not-existing", "info"},
} {
// When
res := suggestCommand(app.Commands, testCase.provided)
// Then
assert.Equal(t, testCase.expected, res)
}
}