feat: some more tests
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22
parser/testdata/recipe_no_description/input.md
vendored
Normal file
22
parser/testdata/recipe_no_description/input.md
vendored
Normal 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.
|
||||||
|
|
24
parser/testdata/recipe_no_description/output.json
vendored
Normal file
24
parser/testdata/recipe_no_description/output.json
vendored
Normal 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."
|
||||||
|
]
|
||||||
|
}
|
@@ -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]
|
||||||
|
Reference in New Issue
Block a user