-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatch_test.go
More file actions
90 lines (77 loc) · 2.21 KB
/
match_test.go
File metadata and controls
90 lines (77 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package str
import "testing"
func TestFuzzyMatch(t *testing.T) {
hay := []string{"hello", "help", "world", "helm", "hero"}
t.Run("normal match", func(t *testing.T) {
matches := FuzzyMatch("helo", hay)
if len(matches) == 0 {
t.Fatal("expected matches")
}
if matches[0].Value != "hello" {
t.Errorf("best match got %q, want %q", matches[0].Value, "hello")
}
})
t.Run("exact match score 1.0", func(t *testing.T) {
matches := FuzzyMatch("hello", hay)
if len(matches) == 0 {
t.Fatal("expected matches")
}
if matches[0].Score != 1.0 {
t.Errorf("exact match score got %f, want 1.0", matches[0].Score)
}
})
t.Run("case insensitive", func(t *testing.T) {
matches := FuzzyMatch("HELLO", hay)
if len(matches) == 0 {
t.Fatal("expected matches")
}
if matches[0].Value != "hello" {
t.Errorf("got %q, want %q", matches[0].Value, "hello")
}
})
t.Run("empty needle", func(t *testing.T) {
if FuzzyMatch("", hay) != nil {
t.Error("expected nil for empty needle")
}
})
t.Run("empty haystack", func(t *testing.T) {
if FuzzyMatch("hello", nil) != nil {
t.Error("expected nil for empty haystack")
}
})
t.Run("no matches above threshold", func(t *testing.T) {
matches := FuzzyMatch("zzzzz", hay)
if matches != nil {
t.Errorf("expected nil, got %d matches", len(matches))
}
})
t.Run("sorted by score descending", func(t *testing.T) {
matches := FuzzyMatch("hel", hay)
for i := 1; i < len(matches); i++ {
if matches[i].Score > matches[i-1].Score {
t.Errorf("not sorted: %f > %f", matches[i].Score, matches[i-1].Score)
}
}
})
t.Run("index preserved", func(t *testing.T) {
matches := FuzzyMatch("hello", hay)
if matches[0].Index != 0 {
t.Errorf("got index %d, want 0", matches[0].Index)
}
})
}
func TestFuzzyMatchAll(t *testing.T) {
hay := []string{"hello", "xyz", "help"}
t.Run("returns low-scoring entries", func(t *testing.T) {
all := FuzzyMatchAll("hello", hay)
filtered := FuzzyMatch("hello", hay)
if len(all) < len(filtered) {
t.Errorf("FuzzyMatchAll returned fewer results (%d) than FuzzyMatch (%d)", len(all), len(filtered))
}
})
t.Run("empty needle", func(t *testing.T) {
if FuzzyMatchAll("", hay) != nil {
t.Error("expected nil")
}
})
}