cache: make the title filter case insensitive

This commit is contained in:
Michael Muré 2019-03-03 15:23:20 +01:00
parent 0e5550a27b
commit 408654514e
No known key found for this signature in database
GPG Key ID: A4457C029293126F
5 changed files with 48 additions and 3 deletions

9
cache/filter.go vendored
View File

@ -55,10 +55,13 @@ func LabelFilter(label string) Filter {
}
}
// TitleFilter return a Filter that match if the title contains the given pattern
func TitleFilter(title string) Filter {
// TitleFilter return a Filter that match if the title contains the given query
func TitleFilter(query string) Filter {
return func(repo *RepoCache, excerpt *BugExcerpt) bool {
return strings.Contains(excerpt.Title, title)
return strings.Contains(
strings.ToLower(excerpt.Title),
strings.ToLower(query),
)
}
}

34
cache/filter_test.go vendored Normal file
View File

@ -0,0 +1,34 @@
package cache
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTitleFilter(t *testing.T) {
tests := []struct {
name string
title string
query string
match bool
}{
{name: "complete match", title: "hello world", query: "hello world", match: true},
{name: "partial match", title: "hello world", query: "hello", match: true},
{name: "no match", title: "hello world", query: "foo", match: false},
{name: "cased title", title: "Hello World", query: "hello", match: true},
{name: "cased query", title: "hello world", query: "Hello", match: true},
// Those following tests should work eventually but are left for a future iteration.
// {name: "cased accents", title: "ÑOÑO", query: "ñoño", match: true},
// {name: "natural language matching", title: "Århus", query: "Aarhus", match: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filter := TitleFilter(tt.query)
excerpt := &BugExcerpt{Title: tt.title}
assert.Equal(t, tt.match, filter(nil, excerpt))
})
}
}

View File

@ -34,6 +34,10 @@ You can pass an additional query to filter and order the list. This query can be
\fB\-l\fP, \fB\-\-label\fP=[]
Filter by label
.PP
\fB\-t\fP, \fB\-\-title\fP=[]
Filter by title
.PP
\fB\-n\fP, \fB\-\-no\fP=[]
Filter by absence of something. Valid values are [label]

View File

@ -29,6 +29,7 @@ git bug ls --status closed --by creation
-s, --status strings Filter by status. Valid values are [open,closed]
-a, --author strings Filter by author
-l, --label strings Filter by label
-t, --title strings Filter by title
-n, --no strings Filter by absence of something. Valid values are [label]
-b, --by string Sort the results by a characteristic. Valid values are [id,creation,edit] (default "creation")
-d, --direction string Select the sorting direction. Valid values are [asc,desc] (default "asc")

View File

@ -535,6 +535,9 @@ _git-bug_ls()
flags+=("--label=")
two_word_flags+=("-l")
local_nonpersistent_flags+=("--label=")
flags+=("--title=")
two_word_flags+=("-t")
local_nonpersistent_flags+=("--title=")
flags+=("--no=")
two_word_flags+=("-n")
local_nonpersistent_flags+=("--no=")