Initialized a proper go module for search, began adding tests and fixed a minor bug!

This commit is contained in:
Julien Dessaux 2021-09-21 19:18:56 +02:00
parent 39e58554a1
commit e504ad9fce
4 changed files with 67 additions and 0 deletions

11
search/go.mod Normal file
View file

@ -0,0 +1,11 @@
module git.adyxax.org/adyxax/www/search
go 1.17
require github.com/stretchr/testify v1.7.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

12
search/go.sum Normal file
View file

@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -61,6 +61,7 @@ func normalizeWords(words []string) (result []string) {
continue continue
} }
result = append(result, word) result = append(result, word)
lastword = word
} }
return return
} }

43
search/search_test.go Normal file
View file

@ -0,0 +1,43 @@
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestNormalizeWords(t *testing.T) {
testCases := []struct {
name string
input []string
expected []string
}{
{"simple", []string{"one", "two", "three"}, []string{"one", "three", "two"}},
{"duplicates", []string{"one", "one", "two", "one", "three", "two"}, []string{"one", "three", "two"}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
valid := normalizeWords(tc.input)
require.Equal(t, tc.expected, valid)
})
}
}
func TestScoreIndex(t *testing.T) {
testCases := []struct {
name string
input []string
index []string
expected int
}{
{"simple", []string{"one", "two", "three"}, []string{"one", "three", "two"}, 3},
{"duplicates", []string{"one", "one"}, []string{"one", "three", "two"}, 2},
{"none", []string{"one", "two"}, []string{"three", "four", "five"}, 0},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
valid := scoreIndex(tc.input, tc.index)
require.Equal(t, tc.expected, valid)
})
}
}