Skip to content

Commit

Permalink
Testing for exact matches
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Mar 23, 2020
1 parent f7181ad commit a04eb0c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions finder/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ func (t *Finder) Refresh(list []string) {
t.lock.Unlock()
}

// Exact returns true if the input is an exact match.
func (t *Finder) Exact(input string) bool {
t.lock.RLock()
_, ok := t.referenceMap[input]
t.lock.RUnlock()

return ok
}

// Find returns the best alternative a score and if it was an exact match or not.
// Since algorithms can define their own upper-bound, there is no "best" value.
func (t *Finder) Find(input string) (string, float64, bool) {
Expand Down
27 changes: 27 additions & 0 deletions finder/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,30 @@ func TestFinder_Refresh(t *testing.T) {
})
}
}

func TestFinder_Exact(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{want: true, name: "exact match", input: "a"},

{name: "empty input", input: ""},
{name: "not exact input", input: "c"},
}

sug, err := New([]string{"a", "b", "z"}, WithAlgorithm(exampleAlgorithm))
if err != nil {
t.Errorf("Didn't expect construction to fail %v", err)
return
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sug.Exact(tt.input); got != tt.want {
t.Errorf("Exact() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/NYTimes/gziphandler v1.0.1
github.com/alextanhongpin/stringdist v0.0.0-20181112144725-57e5fb016be2
github.com/c4milo/github-release v1.1.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/mitchellh/gox v1.0.1 // indirect
github.com/rs/cors v1.6.0
github.com/sirupsen/logrus v1.1.1
github.com/xrash/smetrics v0.0.0-20170218160415-a3153f7040e9
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ github.com/NYTimes/gziphandler v1.0.1 h1:iLrQrdwjDd52kHDA5op2UBJFjmOb9g+7scBan4R
github.com/NYTimes/gziphandler v1.0.1/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/alextanhongpin/stringdist v0.0.0-20181112144725-57e5fb016be2 h1:t2GHeJnzMuA5LXpzhuO4DPSyL79AFzi4K0xv7pAEFjQ=
github.com/alextanhongpin/stringdist v0.0.0-20181112144725-57e5fb016be2/go.mod h1:6Bb1jYZG8v0tBZjFmU6/48W60Gt/aN6rRs2dXDBliq0=
github.com/c4milo/github-release v1.1.0 h1:7wvL5+AGg3qqQAkOi5YXYCHEOkk6Hy3lEV1UKZrUkYo=
github.com/c4milo/github-release v1.1.0/go.mod h1:2W1180qLf1Mb5g/j/ENrfvj909OdHJOSpUForghRPsk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/go-version v1.0.0 h1:21MVWPKDphxa7ineQQTrCU5brh7OuVVAzGOCnnCPtE8=
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4=
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
Expand Down

0 comments on commit a04eb0c

Please sign in to comment.