002bde2233
The new option `app.Suggest` enables command and flag suggestions via the jaro-winkler distance algorithm. Flags are scoped to their appropriate commands whereas command suggestions are scoped to the current command level. Signed-off-by: Sascha Grunert <sgrunert@suse.com>
123 lines
2.1 KiB
Go
123 lines
2.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestSuggestFlag(t *testing.T) {
|
|
// Given
|
|
app := testApp()
|
|
|
|
for _, testCase := range []struct {
|
|
provided, expected string
|
|
}{
|
|
{"", ""},
|
|
{"a", "--another-flag"},
|
|
{"hlp", "--help"},
|
|
{"k", ""},
|
|
{"s", "-s"},
|
|
} {
|
|
// When
|
|
res := app.suggestFlag(app.Flags, testCase.provided)
|
|
|
|
// Then
|
|
expect(t, res, testCase.expected)
|
|
}
|
|
}
|
|
|
|
func TestSuggestFlagHideHelp(t *testing.T) {
|
|
// Given
|
|
app := testApp()
|
|
app.HideHelp = true
|
|
|
|
// When
|
|
res := app.suggestFlag(app.Flags, "hlp")
|
|
|
|
// Then
|
|
expect(t, res, "--fl")
|
|
}
|
|
|
|
func TestSuggestFlagFromError(t *testing.T) {
|
|
// Given
|
|
app := testApp()
|
|
|
|
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
|
|
expect(t, res, fmt.Sprintf(didYouMeanTemplate+"\n\n", testCase.expected))
|
|
}
|
|
}
|
|
|
|
func TestSuggestFlagFromErrorWrongError(t *testing.T) {
|
|
// Given
|
|
app := testApp()
|
|
|
|
// When
|
|
_, err := app.suggestFlagFromError(errors.New("invalid"), "")
|
|
|
|
// Then
|
|
expect(t, true, err != nil)
|
|
}
|
|
|
|
func TestSuggestFlagFromErrorWrongCommand(t *testing.T) {
|
|
// Given
|
|
app := testApp()
|
|
|
|
// When
|
|
_, err := app.suggestFlagFromError(
|
|
errors.New(providedButNotDefinedErrMsg+"flag"),
|
|
"invalid",
|
|
)
|
|
|
|
// Then
|
|
expect(t, true, err != nil)
|
|
}
|
|
|
|
func TestSuggestFlagFromErrorNoSuggestion(t *testing.T) {
|
|
// Given
|
|
app := testApp()
|
|
|
|
// When
|
|
_, err := app.suggestFlagFromError(
|
|
errors.New(providedButNotDefinedErrMsg+""),
|
|
"",
|
|
)
|
|
|
|
// Then
|
|
expect(t, true, err != nil)
|
|
}
|
|
|
|
func TestSuggestCommand(t *testing.T) {
|
|
// Given
|
|
app := testApp()
|
|
|
|
for _, testCase := range []struct {
|
|
provided, expected string
|
|
}{
|
|
{"", ""},
|
|
{"conf", "config"},
|
|
{"i", "i"},
|
|
{"information", "info"},
|
|
{"not-existing", "info"},
|
|
} {
|
|
// When
|
|
res := suggestCommand(app.Commands, testCase.provided)
|
|
|
|
// Then
|
|
expect(t, res, fmt.Sprintf(didYouMeanTemplate, testCase.expected))
|
|
}
|
|
}
|