Skip to content

Commit

Permalink
fix(matchers): name matching case-insensitive (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinX authored Nov 18, 2024
1 parent 27ed056 commit 3e19805
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Take server state into account in server completions. For example, do not offer started servers as completions for `server start` command.
- Allow using UUID prefix as an argument. For example, if there is only one network available that has an UUID starting with `0316`, details of that network can be listed with `upctl network show 0316` command.
- Match title and name arguments case-insensitively if the given parameter does not resolve with an exact match.

## [3.11.1] - 2024-08-12

Expand Down
3 changes: 3 additions & 0 deletions internal/resolver/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func MatchArgWithWhitespace(arg, value string) MatchType {
if completion.RemoveWordBreaks(value) == arg || value == arg {
return MatchTypeExact
}
if strings.EqualFold(completion.RemoveWordBreaks(value), arg) || strings.EqualFold(value, arg) {
return MatchTypeCaseInsensitive
}
return MatchTypeNone
}

Expand Down
45 changes: 45 additions & 0 deletions internal/resolver/matchers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package resolver

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMatchers(t *testing.T) {
cases := []struct {
name string
execFn func(string, string) MatchType
arg string
value string
expected MatchType
}{
{
name: "Exact match",
execFn: MatchArgWithWhitespace,
arg: "McDuck",
value: "McDuck",
expected: MatchTypeExact,
},
{
name: "Case-insensitive match",
execFn: MatchArgWithWhitespace,
arg: "mcduck",
value: "McDuck",
expected: MatchTypeCaseInsensitive,
},
{
name: "No match",
execFn: MatchArgWithWhitespace,
arg: "scrooge",
value: "McDuck",
expected: MatchTypeNone,
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.execFn(tt.arg, tt.value))
})
}
}

0 comments on commit 3e19805

Please sign in to comment.