From e504ad9fce707d14a9315490237efcd16baff87f Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 21 Sep 2021 19:18:56 +0200 Subject: Initialized a proper go module for search, began adding tests and fixed a minor bug! --- search/go.mod | 11 +++++++++++ search/go.sum | 12 ++++++++++++ search/search.go | 1 + search/search_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 search/go.mod create mode 100644 search/go.sum create mode 100644 search/search_test.go diff --git a/search/go.mod b/search/go.mod new file mode 100644 index 0000000..46a0175 --- /dev/null +++ b/search/go.mod @@ -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 +) diff --git a/search/go.sum b/search/go.sum new file mode 100644 index 0000000..38a0a25 --- /dev/null +++ b/search/go.sum @@ -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= diff --git a/search/search.go b/search/search.go index 3643cf5..0d4dbdc 100644 --- a/search/search.go +++ b/search/search.go @@ -61,6 +61,7 @@ func normalizeWords(words []string) (result []string) { continue } result = append(result, word) + lastword = word } return } diff --git a/search/search_test.go b/search/search_test.go new file mode 100644 index 0000000..e7ce3bc --- /dev/null +++ b/search/search_test.go @@ -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) + }) + } +} -- cgit v1.2.3