aboutsummaryrefslogtreecommitdiff
path: root/search/search_test.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-09-21 19:18:56 +0200
committerJulien Dessaux2021-09-21 19:18:56 +0200
commite504ad9fce707d14a9315490237efcd16baff87f (patch)
tree48e961841b6ab42bdb51a0c6ed40aa3a29592b1f /search/search_test.go
parentAdded treasure island book article (diff)
downloadwww-e504ad9fce707d14a9315490237efcd16baff87f.tar.gz
www-e504ad9fce707d14a9315490237efcd16baff87f.tar.bz2
www-e504ad9fce707d14a9315490237efcd16baff87f.zip
Initialized a proper go module for search, began adding tests and fixed a minor bug!
Diffstat (limited to '')
-rw-r--r--search/search_test.go43
1 files changed, 43 insertions, 0 deletions
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)
+ })
+ }
+}