Files
wehub-resource-sync ead81af521
Deploy to GitHub Pages / deploy (push) Failing after 0s
CI / go (push) Has been cancelled
CI / build (darwin, macos-14) (push) Has been cancelled
CI / build (windows, windows-2025) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:13 +08:00

57 lines
1.6 KiB
Go

package fuzzy
import "testing"
func TestMatch(t *testing.T) {
tests := []struct {
name string
query string
target string
want bool
}{
{"empty query matches", "", "anything", true},
{"exact substring", "love", "Love Story", true},
{"case insensitive", "LOVE", "love story", true},
{"non-contiguous subsequence", "lst", "Love Story", true},
{"missing char", "lovex", "Love Story", false},
{"out of order", "ba", "ab", false},
{"empty target non-empty query", "x", "", false},
{"unicode subsequence", "café", "Le Café", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, ok := Match(tt.query, tt.target); ok != tt.want {
t.Fatalf("Match(%q, %q) ok = %v, want %v", tt.query, tt.target, ok, tt.want)
}
})
}
}
func TestMatchRanking(t *testing.T) {
// Each case asserts that `query` scores strictly higher against `high`
// than against `low` (both must match).
tests := []struct {
name string
query string
high, low string
}{
{"prefix beats interior", "lov", "Love", "Beloved"},
{"word boundary beats mid-word", "st", "Love Story", "august"},
{"consecutive beats scattered", "abc", "abcdef", "axbxcx"},
{"start beats later", "fo", "Foo Bar", "Bar Foo"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hi, okHi := Match(tt.query, tt.high)
lo, okLo := Match(tt.query, tt.low)
if !okHi || !okLo {
t.Fatalf("both should match: high ok=%v, low ok=%v", okHi, okLo)
}
if hi <= lo {
t.Fatalf("Match(%q,%q)=%d should beat Match(%q,%q)=%d",
tt.query, tt.high, hi, tt.query, tt.low, lo)
}
})
}
}