diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d2e684..9e4666d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- using name for id options with `[` in the name makes the cli panic + ## [v0.48.1] - 2024-02-16 ### Fixed diff --git a/pkg/search/find_test.go b/pkg/search/find_test.go new file mode 100644 index 00000000..46dd2acc --- /dev/null +++ b/pkg/search/find_test.go @@ -0,0 +1,87 @@ +package search + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSearchOnList(t *testing.T) { + entities := []named{ + namedStruct{ + ID: "1", + Name: "entity one", + }, + namedStruct{ + ID: "2", + Name: "entity two", + }, + namedStruct{ + ID: "3", + Name: "entity three", + }, + namedStruct{ + ID: "4", + Name: "more complex name", + }, + namedStruct{ + ID: "id", + Name: "by id", + }, + namedStruct{ + ID: "bra", + Name: "with [bracket]", + }, + } + + tts := []struct { + name string + search string + entities []named + result string + }{ + { + name: "one term", + search: "two", + entities: entities, + result: "2", + }, + { + name: "two terms", + search: "complex name", + entities: entities, + result: "4", + }, + { + name: "sections of the name", + search: "mo nam", + entities: entities, + result: "4", + }, + { + name: "with brackets", + search: "[bracket]", + entities: entities, + result: "bra", + }, + { + name: "using id", + search: "by id", + entities: entities, + result: "id", + }, + } + + for i := range tts { + tt := tts[i] + t.Run(tt.name, func(t *testing.T) { + id, err := findByName(tt.search, "element", func() ([]named, error) { + return tt.entities, nil + }) + + if assert.NoError(t, err) { + assert.Equal(t, tt.result, id) + } + }) + } +} diff --git a/strhlp/strhlp.go b/strhlp/strhlp.go index fce68d2c..b7d02d02 100644 --- a/strhlp/strhlp.go +++ b/strhlp/strhlp.go @@ -106,7 +106,7 @@ func PadSpace(s string, size int) string { // string will be taken as .* on a regex func IsSimilar(filter string) func(string) bool { // skipcq: GO-C4007 - filter = regexp.MustCompile(`[\]\^\\\,\.\(\)\-]+`). + filter = regexp.MustCompile(`[\[\]\^\\\,\.\(\)\-]+`). ReplaceAllString(Normalize(filter), " ") filter = regexp.MustCompile(`\s+`).ReplaceAllString(filter, " ") filter = strings.ReplaceAll(filter, " ", ".*")