feat: refactor some shit
This commit is contained in:
@@ -1,50 +1,54 @@
|
||||
package parser
|
||||
package parser_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.max-richter.dev/max/marka/parser"
|
||||
"git.max-richter.dev/max/marka/parser/blocks"
|
||||
"git.max-richter.dev/max/marka/registry"
|
||||
)
|
||||
|
||||
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)
|
||||
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 BlockType
|
||||
Type blocks.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}"},
|
||||
{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(blocks) != len(expected) {
|
||||
t.Fatalf("expected %d blocks, got %d", len(expected), len(blocks))
|
||||
if len(templateBlocks) != len(expected) {
|
||||
t.Fatalf("expected %d blocks, got %d", len(expected), len(templateBlocks))
|
||||
}
|
||||
|
||||
for i, b := range blocks {
|
||||
for i, b := range templateBlocks {
|
||||
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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user