Files
marka/parser/matcher/matcher_test.go
Max Richter c687eff53d big tings
2025-08-17 15:16:17 +02:00

88 lines
2.2 KiB
Go

package matcher_test
import (
"fmt"
"testing"
"git.max-richter.dev/max/marka/parser/matcher"
"git.max-richter.dev/max/marka/parser/utils"
"git.max-richter.dev/max/marka/registry"
"git.max-richter.dev/max/marka/template"
)
func TestFuzzyFindAll(t *testing.T) {
recipeMd := utils.ReadTestDataFile(t, "baguette.md")
tests := []struct {
Needle string
Start, End, StartIndex int
}{
{StartIndex: 0, Needle: "# Ingredients\n", Start: 77, End: 91},
{StartIndex: 0, Needle: "# Ingrdients\n", Start: 77, End: 91},
{StartIndex: 0, Needle: "# Inrdients\n", Start: 77, End: 91},
{StartIndex: 0, Needle: "---\n", Start: 0, End: 4},
{StartIndex: 4, Needle: "---\n", Start: 29, End: 33},
{StartIndex: 0, Needle: "# Steps\n", Start: 116, End: 124},
{StartIndex: 0, Needle: "# Stps\n", Start: 116, End: 124},
{StartIndex: 0, Needle: "# Step\n", Start: 116, End: 124},
}
for _, test := range tests {
start, end := matcher.FuzzyFind(recipeMd, test.StartIndex, test.Needle, 0.3) // allow 50% error
if start != test.Start || end != test.End {
t.Errorf("Start or end do not match: Needle=%q Start=%d/%d End=%d/%d", test.Needle, test.Start, start, test.End, end)
}
}
}
func TestFuzzyBlockMatch(t *testing.T) {
recipeMd := utils.ReadTestDataFile(t, "baguette.md")
schemaMd, err := registry.GetTemplate("Recipe")
if err != nil {
t.Errorf("Failed to load template: %s", err.Error())
t.FailNow()
}
blocks, err := template.CompileTemplate(schemaMd)
if err != nil {
t.Errorf("Failed to compile template: %s", err.Error())
t.FailNow()
}
for _, b := range blocks {
fmt.Printf("block: %#v\n", b)
}
matches := matcher.MatchBlocksFuzzy(recipeMd, blocks, 0.3)
expected := []struct {
value string
}{
{
value: "author.name: Max Richter",
},
{
value: "Baguette",
},
{
value: "My favourite baguette recipe",
},
{
value: "- Flour\n- Water\n- Salt",
},
{
value: "1. Mix Flour Water and Salt\n2. Bake the bread\n",
},
}
for i, m := range matches {
if i > len(expected)-1 {
t.Errorf("No expected result for match: %d -> %q", i, m.GetContent())
t.FailNow()
}
if expected[i].value != m.GetContent() {
t.Errorf("Match %d did not match expected: %q", i, m.GetContent())
}
}
}