feat: some more tests

This commit is contained in:
Max Richter
2025-08-17 15:20:16 +02:00
parent c687eff53d
commit 6db87db325
4 changed files with 80 additions and 0 deletions

View File

@@ -38,3 +38,32 @@ func TestParseRecipe_Golden(t *testing.T) {
t.Fatalf("JSON mismatch (-want +got):\n%s", diff) t.Fatalf("JSON mismatch (-want +got):\n%s", diff)
} }
} }
func TestParseRecipe_NoDescription(t *testing.T) {
td := filepath.Join("testdata", "recipe_no_description")
input := filepath.Join(td, "input.md")
output := filepath.Join(td, "output.json")
inputContent, err := os.ReadFile(input)
if err != nil {
t.Fatalf("read input.md: %v", err)
}
got, err := parser.ParseFile(string(inputContent))
if err != nil {
t.Fatalf("ParseFile: %v", err)
}
var want map[string]any
b, err := os.ReadFile(output)
if err != nil {
t.Fatalf("read expected.json: %v", err)
}
if err := json.Unmarshal(b, &want); err != nil {
t.Fatalf("unmarshal expected.json: %v", err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("JSON mismatch (-want +got):\n%s", diff)
}
}

View File

@@ -0,0 +1,22 @@
---
@type: Recipe
image: https://example.com/salad.jpg
author.name: Alex Chef
prepTime: PT10M
cookTime: PT0M
recipeYield: 2 servings
---
# Simple Salad
## Ingredients
- 100 g lettuce
- 5 cherry tomatoes
- 1 tbsp olive oil
- Pinch of salt
## Steps
1. Wash and dry the lettuce.
2. Halve the cherry tomatoes.
3. Toss with olive oil and salt.

View File

@@ -0,0 +1,24 @@
{
"@context": "https://schema.org",
"@type": "Recipe",
"name": "Simple Salad",
"image": "https://example.com/salad.jpg",
"author": {
"@type": "Person",
"name": "Alex Chef"
},
"prepTime": "PT10M",
"cookTime": "PT0M",
"recipeYield": "2 servings",
"recipeIngredient": [
"100 g lettuce",
"5 cherry tomatoes",
"1 tbsp olive oil",
"Pinch of salt"
],
"recipeInstructions": [
"Wash and dry the lettuce.",
"Halve the cherry tomatoes.",
"Toss with olive oil and salt."
]
}

View File

@@ -12,6 +12,11 @@ import (
// - when obj is a map -> if value is a map[string]any, merge into obj; otherwise obj is unchanged. // - when obj is a map -> if value is a map[string]any, merge into obj; otherwise obj is unchanged.
// - otherwise -> returns obj unchanged. // - otherwise -> returns obj unchanged.
func SetPathValue(path string, value any, obj any) any { func SetPathValue(path string, value any, obj any) any {
// check if value is empty string and if so continue
if value == "" || value == nil {
return obj
}
// Split and drop empty segments (so ".", "..", "" become no keys) // Split and drop empty segments (so ".", "..", "" become no keys)
raw := strings.Split(path, ".") raw := strings.Split(path, ".")
keys := raw[:0] keys := raw[:0]