-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(matchers): name matching case-insensitive
- Loading branch information
Showing
2 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package resolver | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion" | ||
) | ||
|
||
// MatchStringWithWhitespace checks if arg that may include whitespace matches given value. This checks both quoted args and auto-completed args handled with completion.RemoveWordBreaks. | ||
func MatchArgWithWhitespace(arg string, value string) bool { | ||
return completion.RemoveWordBreaks(value) == arg || value == arg | ||
return completion.RemoveWordBreaks(value) == arg || value == arg || strings.EqualFold(arg, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package resolver | ||
|
||
import "testing" | ||
|
||
func TestMatchers(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
execFn func(string, string) bool | ||
arg string | ||
value string | ||
expected bool | ||
}{ | ||
{ | ||
name: "Matcher no case", | ||
execFn: MatchArgWithWhitespace, | ||
arg: "test", | ||
value: "test", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Matcher with case", | ||
execFn: MatchArgWithWhitespace, | ||
arg: "TeSt", | ||
value: "test", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Matcher invalid", | ||
execFn: MatchArgWithWhitespace, | ||
arg: "test", | ||
value: "invalid", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if result := tt.execFn(tt.arg, tt.value); result != tt.expected { | ||
t.Errorf("Matcher() failed %v, wanted %v", result, tt.expected) | ||
} | ||
}) | ||
} | ||
} |