feat: refactor some shit

This commit is contained in:
Max Richter
2025-08-17 00:46:45 +02:00
parent 43644c4f40
commit cc8f967f07
20 changed files with 459 additions and 209 deletions

View File

@@ -1,37 +1,27 @@
package parser_test
import (
"os"
"path/filepath"
"testing"
"git.max-richter.dev/max/marka/parser"
"git.max-richter.dev/max/marka/registry"
)
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")
recipeMd := readTestDataFile(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: "# 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: 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},
{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 {
@@ -45,16 +35,20 @@ func TestFuzzyFindAll(t *testing.T) {
}
func TestFuzzyBlockMatch(t *testing.T) {
recipeMd := readFile(t, "baguette.md")
schemaMd := readFile(t, "recipe.schema.md")
blocks := parser.ExtractBlocks(schemaMd)
recipeMd := readTestDataFile(t, "baguette.md")
schemaMd, err := registry.GetTemplate("recipe")
if err != nil {
t.Errorf("Failed to load template: %s", err.Error())
t.FailNow()
}
blocks, _ := parser.ExtractBlocks(schemaMd)
matches := parser.MatchBlocksFuzzy(recipeMd, blocks, 0.3)
expected := []struct {
value string
}{
{
value: "author: Max Richter",
value: "author.name: Max Richter",
},
{
value: "Baguette",
@@ -66,7 +60,7 @@ func TestFuzzyBlockMatch(t *testing.T) {
value: "- Flour\n- Water\n- Salt",
},
{
value: "1. Mix Flour Water and Salt\n2. Bake the bread",
value: "1. Mix Flour Water and Salt\n2. Bake the bread\n",
},
}