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) } } }