84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package parser_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.max-richter.dev/max/marka/parser"
|
|
)
|
|
|
|
func readFile(t *testing.T, fileName string) string {
|
|
path := filepath.Join("testdata", fileName)
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("failed to read test data file: %v", err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func TestFuzzyFindAll(t *testing.T) {
|
|
recipeMd := readFile(t, "baguette.md")
|
|
|
|
tests := []struct {
|
|
Needle string
|
|
Start, End, StartIndex int
|
|
}{
|
|
{StartIndex: 0, Needle: "# Ingredients\n", Start: 72, End: 86},
|
|
{StartIndex: 0, Needle: "# Ingrdients\n", Start: 72, End: 86},
|
|
{StartIndex: 0, Needle: "# Inrdients\n", Start: 72, End: 86},
|
|
{StartIndex: 0, Needle: "---\n", Start: 0, End: 4},
|
|
{StartIndex: 4, Needle: "---\n", Start: 24, End: 28},
|
|
{StartIndex: 0, Needle: "# Steps\n", Start: 111, End: 119},
|
|
{StartIndex: 0, Needle: "# Stps\n", Start: 111, End: 119},
|
|
{StartIndex: 0, Needle: "# Step\n", Start: 111, End: 119},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
start, end := parser.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 := readFile(t, "baguette.md")
|
|
schemaMd := readFile(t, "recipe.schema.md")
|
|
blocks := parser.ExtractBlocks(schemaMd)
|
|
matches := parser.MatchBlocksFuzzy(recipeMd, blocks, 0.3)
|
|
|
|
expected := []struct {
|
|
value string
|
|
}{
|
|
{
|
|
value: "author: 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",
|
|
},
|
|
}
|
|
|
|
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())
|
|
}
|
|
}
|
|
|
|
}
|