Files
marka/parser/blocks_test.go
2025-08-17 00:46:45 +02:00

56 lines
2.1 KiB
Go

package parser_test
import (
"testing"
"git.max-richter.dev/max/marka/parser"
"git.max-richter.dev/max/marka/parser/blocks"
"git.max-richter.dev/max/marka/registry"
)
func TestExtractBlocks(t *testing.T) {
src, err := registry.GetTemplate("recipe")
if err != nil {
t.Errorf("Failed to extract blocks: %s", err.Error())
t.FailNow()
}
templateBlocks, err := parser.ExtractBlocks(src)
if err != nil {
t.Errorf("Failed to extract blocks: %s", err.Error())
t.FailNow()
}
expected := []struct {
Type blocks.BlockType
Content string
}{
{blocks.MatchingBlock, "---\n"},
{blocks.DataBlock, "{\n path: .\n codec: yaml\n fields:\n - path: name\n codec: text\n required: true\n - path: image\n codec: text\n required: true\n - path: author.@type\n codec: const\n value: Person\n - path: author.name\n codec: text\n - path: datePublished\n codec: text\n - path: description\n codec: text\n - path: prepTime\n codec: text\n - path: cookTime\n codec: text\n - path: recipeYield\n codec: text\n}"},
{blocks.MatchingBlock, "\n---\n\n# "},
{blocks.DataBlock, "{ name | text,required }"},
{blocks.MatchingBlock, "\n\n"},
{blocks.DataBlock, "{ description | text }"},
{blocks.MatchingBlock, "\n\n## Ingredients\n"},
{blocks.DataBlock, "{\n path: recipeIngredient\n codec: list\n required: true\n item:\n template: \"- { . }\"\n}"},
{blocks.MatchingBlock, "\n\n## Steps\n"},
{blocks.DataBlock, "{\n path: recipeInstructions\n codec: list\n required: true\n item:\n template: \"{ @index }. { . }\"\n}"},
}
if len(templateBlocks) != len(expected) {
t.Fatalf("expected %d blocks, got %d", len(expected), len(templateBlocks))
}
for i, b := range templateBlocks {
exp := expected[i]
if b.Type != exp.Type {
t.Errorf("Block#%d Type '%s' did not match expected type '%s'", i, b.Type, exp.Type)
}
content := b.GetContent()
if content != exp.Content {
t.Errorf("Block#%d Content '%s' did not match expected Content: '%s'", i, content, exp.Content)
}
}
}