Skip to content

Commit bc3e336

Browse files
committed
test: reduce suggestFlag single-rune Unicode coverage to one case
Replace the two new tests and the flagStringForTest helper with a case in TestSuggestFlag. prefixFor with a single-rune Unicode name is tested by TestCommand_SingleRuneUnicodeFlag, and the suggestFlagFromError path is covered by TestSuggestFlagFromError. Signed-off-by: Eng Zer Jun <[email protected]>
1 parent c824838 commit bc3e336

1 file changed

Lines changed: 2 additions & 55 deletions

File tree

‎suggestions_test.go‎

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"errors"
55
"fmt"
6-
"strings"
76
"testing"
87

98
"github.com/stretchr/testify/assert"
@@ -39,6 +38,7 @@ func TestJaroWinkler(t *testing.T) {
3938
func TestSuggestFlag(t *testing.T) {
4039
// Given
4140
app := buildExtendedTestCommand()
41+
app.Flags = append(app.Flags, &BoolFlag{Name: "é"})
4242

4343
for _, testCase := range []struct {
4444
provided, expected string
@@ -48,6 +48,7 @@ func TestSuggestFlag(t *testing.T) {
4848
{"hlp", "--help"},
4949
{"k", ""},
5050
{"s", "-s"},
51+
{"éé", "-é"},
5152
} {
5253
// When
5354
res := suggestFlag(app.Flags, testCase.provided, false)
@@ -57,60 +58,6 @@ func TestSuggestFlag(t *testing.T) {
5758
}
5859
}
5960

60-
// TestSuggestFlagMultibyteRunePrefix ensures the "-"/"--" prefix chosen for a
61-
// suggested flag name is derived from the rune count, exactly like the prefix
62-
// used when the very same flag is rendered in help output by prefixFor. A
63-
// single (non-ASCII) rune flag such as "é" is a short flag and must be
64-
// suggested as "-é", not "--é".
65-
func TestSuggestFlagMultibyteRunePrefix(t *testing.T) {
66-
for _, testCase := range []struct {
67-
name, provided, expected string
68-
}{
69-
// Single ASCII rune: short flag.
70-
{"single-rune-ascii", "ss", "-s"},
71-
// Single multi-byte rune: still a short flag.
72-
{"single-rune-multibyte", "éé", "-é"},
73-
} {
74-
t.Run(testCase.name, func(t *testing.T) {
75-
fl := &BoolFlag{Name: "é"}
76-
if testCase.name == "single-rune-ascii" {
77-
fl = &BoolFlag{Name: "s"}
78-
}
79-
80-
// The suggestion must agree with how the flag is advertised in
81-
// help output, which is driven by prefixFor.
82-
assert.Equal(t, "-"+fl.Names()[0], flagStringForTest(fl))
83-
84-
assert.Equal(t, testCase.expected, suggestFlag([]Flag{fl}, testCase.provided, true))
85-
})
86-
}
87-
}
88-
89-
// TestSuggestFlagMultibyteRuneFromError covers the user-visible output: the
90-
// "Did you mean ...?" hint must name the short flag exactly as help output
91-
// does.
92-
func TestSuggestFlagMultibyteRuneFromError(t *testing.T) {
93-
cmd := &Command{Flags: []Flag{&BoolFlag{Name: "é"}}}
94-
95-
res, err := cmd.suggestFlagFromError(
96-
errors.New(providedButNotDefinedErrMsg+"éé"),
97-
"",
98-
)
99-
assert.NoError(t, err)
100-
assert.Equal(t, fmt.Sprintf(SuggestDidYouMeanTemplate+"\n\n", "-é"), res)
101-
assert.Equal(t, "Did you mean \"-é\"?\n\n", res)
102-
}
103-
104-
// flagStringForTest returns the leading flag-name portion of a flag's help
105-
// rendering, e.g. "-é" or "--verbose".
106-
func flagStringForTest(fl Flag) string {
107-
s := stringifyFlag(fl)
108-
if i := strings.IndexByte(s, '\t'); i >= 0 {
109-
return strings.TrimSpace(s[:i])
110-
}
111-
return strings.TrimSpace(s)
112-
}
113-
11461
func TestSuggestFlagHideHelp(t *testing.T) {
11562
// Given
11663
app := buildExtendedTestCommand()

0 commit comments

Comments
 (0)