Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(matchers): name matching case-insensitive #335

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})
}
}
Loading