Skip to content

Commit

Permalink
fix: find by name with [
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Feb 22, 2024
1 parent abbf3bd commit ddf07ba
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions pkg/search/find_test.go
Original file line number Diff line number Diff line change
@@ -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: "[braket]",
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)
}
})
}
}
2 changes: 1 addition & 1 deletion strhlp/strhlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ", ".*")
Expand Down

0 comments on commit ddf07ba

Please sign in to comment.