all repos

clerk @ a91ce69

missing tooling for ledger/hledger

clerk/internal/lsp/fuzzy/fuzzy_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
lsp: completion, 2 months ago
1
package fuzzy
2
3
import (
4
	"math"
5
	"testing"
6
)
7
8
func TestFuzzyScore(t *testing.T) {
9
	tests := []struct {
10
		name    string
11
		pattern string
12
		text    string
13
		want    float64 // -1 means: expect 0 (no match)
14
	}{
15
		{"empty pattern matches everything", "", "expenses:food", 1},
16
		{"exact boundary match", "food", "expenses:food", 1},
17
		{"long exact match also perfect", "expenses", "expenses:food", 1},
18
		{"acronym across segments", "expf", "expenses:food", 1},
19
		{"colon need not be typed", "expensesfood", "expenses:food", 1},
20
		{"all-caps pattern still perfect", "FOOD", "expenses:food", 1},
21
		{"gapped mid-word match", "od", "expenses:food", 0.444},
22
		{"boundary beats mid-word", "xf", "expenses:food", 0.778},
23
		{"exact prefix", "ex", "expenses", 1},
24
		{"gap penalty", "esf", "expenses:food", 0.923},
25
		{"no match", "xyz", "abc", 0},
26
		{"cjk perfect match", "食物", "支出:食物", 1},
27
		{"ukrainian perfect match", "продукти", "витрати:продукти", 1},
28
		{"cjk gap", "食物", "支出费物", 0},
29
	}
30
	for _, tt := range tests {
31
		t.Run(tt.name, func(t *testing.T) {
32
			got := Score(tt.pattern, tt.text)
33
			if tt.want == 0 {
34
				if got != 0 {
35
					t.Errorf("Score(%q, %q) = %v, want 0", tt.pattern, tt.text, got)
36
				}
37
				return
38
			}
39
			if math.Abs(got-tt.want) > 1e-3 {
40
				t.Errorf("Score(%q, %q) = %v, want %v", tt.pattern, tt.text, got, tt.want)
41
			}
42
		})
43
	}
44
}
45
46
func TestFuzzyScoreDeterministic(t *testing.T) {
47
	for _, text := range []string{"expenses:food", "支出:食物", "витрати:продукти"} {
48
		for _, pattern := range []string{"", "ex", "expf", "o", "食物", "п"} {
49
			if a, b := Score(pattern, text), Score(pattern, text); a != b {
50
				t.Errorf("Score(%q, %q) not deterministic: %v != %v", pattern, text, a, b)
51
			}
52
		}
53
	}
54
}