Files
marka/template/compile_test.go
Max Richter c687eff53d big tings
2025-08-17 15:16:17 +02:00

101 lines
2.2 KiB
Go

package template_test
import (
"testing"
"git.max-richter.dev/max/marka/registry"
"git.max-richter.dev/max/marka/template"
)
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 := template.CompileTemplate(src)
if err != nil {
t.Errorf("Failed to extract blocks: %s", err.Error())
t.FailNow()
}
expected := []template.Block{
{
Type: template.MatchingBlock,
},
{
Type: template.DataBlock,
Codec: "yaml",
Path: ".",
Fields: []template.BlockField{
{
Path: "@type",
},
{
Path: "image",
},
{
Path: "author.@type",
},
{
Path: "author.name",
},
{
Path: "datePublished",
},
{
Path: "prepTime",
},
{
Path: "cookTime",
},
{
Path: "recipeYield",
},
},
},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "name", Codec: "text"},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "description", Codec: "text"},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "recipeIngredient", Codec: "list", ListTemplate: "- { . }"},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "recipeInstructions", Codec: "list", ListTemplate: "{ @index }. { . }"},
}
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)
}
if b.Path != exp.Path {
t.Errorf("Block#%d Path '%s' did not match expected path '%s'", i, b.Path, exp.Path)
}
if b.Codec != exp.Codec {
t.Errorf("Block#%d Codec '%s' did not match expected codec '%s'", i, b.Codec, exp.Codec)
}
}
}
func TestExtractInlineTemplate(t *testing.T) {
testCases := []string{
"- { . }",
"- { amount } { type }",
}
for _, tc := range testCases {
_, err := template.CompileTemplate(tc)
if err != nil {
t.Errorf("Failed to extract blocks: %s", err.Error())
t.FailNow()
}
}
}