ffs, i should have committed wayyy earlier

This commit is contained in:
Max Richter
2025-08-16 20:38:40 +02:00
commit 43644c4f40
25 changed files with 865 additions and 0 deletions

51
parser/blocks_test.go Normal file
View File

@@ -0,0 +1,51 @@
package parser
import (
"os"
"path/filepath"
"strings"
"testing"
)
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 TestExtractBlocks(t *testing.T) {
src := readFile(t, "recipe.schema.md")
blocks := ExtractBlocks(src)
expected := []struct {
Type BlockType
Content string
}{
{BlockMatching, "---\\n"},
{BlockData, "{ . }"},
{BlockMatching, "\\n---\\n\\n# "},
{BlockData, "{ name | text,required }"},
{BlockMatching, "\\n\\n"},
{BlockData, "{ description | text,optional }"},
{BlockMatching, "\\n\\n## Ingredients\\n"},
{BlockData, "{\\n path: recipeIngredient\\n codec: list\\n required: true\\n item:\\n template: \"- { . }\"\\n}"},
{BlockMatching, "\\n\\n## Steps\\n"},
{BlockData, "{\\n path: recipeInstructions\\n codec: list\\n required: true\\n item:\\n template: \"{ @index }. { . }\"\\n}"},
}
if len(blocks) != len(expected) {
t.Fatalf("expected %d blocks, got %d", len(expected), len(blocks))
}
for i, b := range blocks {
exp := expected[i]
content := strings.ReplaceAll(b.GetContent(), "\n", "\\n")
if b.Type != exp.Type || content != exp.Content {
t.Errorf("Block %d: expected %v, got Type: %v, Start: %d, End: %d, Content: %s", i, exp, b.Type, b.Start, b.End, content)
}
}
}