xonecas.io
5119
Lh

lazy-history / files

lazy-history / internal / search / search_test.go

1.2 KB
1package search
2
3import (
4	"reflect"
5	"testing"
6)
7
8// Test the search function.
9func TestSearch(t *testing.T) {
10	tests := []struct {
11		slice     []string
12		substring string
13		want      []SearchResult
14	}{
15		{
16			slice:     []string{"apple", "banana", "cherry", "date"},
17			substring: "ban",
18			want:      []SearchResult{{"banana", 3}, {"date", 3}},
19		},
20		{
21			slice:     []string{"apple", "banana", "cherry", "date"},
22			substring: "banana",
23			want:      []SearchResult{{"banana", 0}},
24		},
25	}
26
27	for _, test := range tests {
28		if got := search(test.slice, test.substring); !reflect.DeepEqual(got, test.want) {
29			t.Errorf("search(%v, %q) = %v; want %v", test.slice, test.substring, got, test.want)
30		}
31	}
32}
33
34// Test the levenshtein distance function.
35func TestLevenshteinDistance(t *testing.T) {
36	tests := []struct {
37		s, t string
38		want int
39	}{
40		{"ban", "apple", 5},
41		{"ban", "banana", 3},
42		{"ban", "cherry", 6},
43		{"ban", "date", 3},
44		{"banana", "banana", 0},
45		{"banana", "apple", 5},
46		{"banana", "cherry", 6},
47		{"banana", "date", 5},
48	}
49
50	for _, test := range tests {
51		if got := levenshteinDistance(test.s, test.t); got != test.want {
52			t.Errorf("levenshteinDistance(%q, %q) = %d; want %d", test.s, test.t, got, test.want)
53		}
54	}
55}