cache: make the title filter case insensitive

Michael Muré created

Change summary

cache/filter.go              |  9 ++++++---
cache/filter_test.go         | 34 ++++++++++++++++++++++++++++++++++
doc/man/git-bug-ls.1         |  4 ++++
doc/md/git-bug_ls.md         |  1 +
misc/bash_completion/git-bug |  3 +++
5 files changed, 48 insertions(+), 3 deletions(-)

Detailed changes

cache/filter.go 🔗

@@ -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),
+		)
 	}
 }
 

cache/filter_test.go 🔗

@@ -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))
+		})
+	}
+}

doc/man/git-bug-ls.1 🔗

@@ -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]

doc/md/git-bug_ls.md 🔗

@@ -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")

misc/bash_completion/git-bug 🔗

@@ -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=")