Skip to content

Commit

Permalink
fix(matchers): name matching case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinX committed Oct 4, 2024
1 parent 18eb8a4 commit 5eee973
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/resolver/matchers.go
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)
}
43 changes: 43 additions & 0 deletions internal/resolver/matchers_test.go
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)
}
})
}
}

0 comments on commit 5eee973

Please sign in to comment.