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,43 +1,42 @@
package parser_test
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"fmt"
"testing"
"git.max-richter.dev/max/marka/parser"
"git.max-richter.dev/max/marka/registry"
)
func TestParseRecipe_Golden(t *testing.T) {
td := filepath.Join("testdata", "recipe_salad")
input := filepath.Join(td, "input.md")
output := filepath.Join(td, "output.json")
func TestParseBaguette(t *testing.T) {
recipeMd := readTestDataFile(t, "baguette.md")
inputContent, err := os.ReadFile(input)
template, err := registry.GetTemplate("recipe")
if err != nil {
t.Fatalf("read input.md: %v", err)
t.Fatalf("Err: %s", err)
}
got, err := parser.ParseFile(string(inputContent))
blocks, err := parser.ExtractBlocks(template)
if err != nil {
t.Fatalf("ParseFile: %v", err)
t.Fatalf("Err: %s", err)
}
var want map[string]any
b, err := os.ReadFile(output)
if err != nil {
t.Fatalf("read expected.json: %v", err)
}
if err := json.Unmarshal(b, &want); err != nil {
t.Fatalf("unmarshal expected.json: %v", err)
matches := parser.MatchBlocksFuzzy(recipeMd, blocks, 0.3)
parsed := parser.Parse(matches)
expected := map[string]any{
"name": "Baguette",
"description": "My favourite baguette recipe",
"recipeIngredient": []string{"Flour", "Water", "Salt"},
// "recipeInstructions": []string{
// "Mix Flour Water and Salt",
// "Bake the bread",
// },
}
// Deep structural compare
if !reflect.DeepEqual(want, got) {
gb, _ := json.MarshalIndent(got, "", " ")
wb, _ := json.MarshalIndent(want, "", " ")
t.Fatalf("parsed JSON mismatch\n--- got ---\n%s\n--- want ---\n%s", string(gb), string(wb))
for k, v := range expected {
if fmt.Sprintf("%v", parsed[k]) != fmt.Sprintf("%v", v) {
t.Errorf("Expected %v but got %v", v, parsed[k])
}
}
}