-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcooklang_test.go
More file actions
118 lines (99 loc) · 2.44 KB
/
cooklang_test.go
File metadata and controls
118 lines (99 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cooklang
import (
"testing"
"github.com/hilli/cooklang/parser"
)
var (
input = `---
title: Test Recipe
date: 2023-10-01
cuisine: Italian
difficulty: Easy
prep_time: 10 minutes
total_time: 30 minutes
author: John Doe
description: A simple test recipe.
images:
- image1.jpg
- image2.jpeg
servings: 2
tags: [ test, cooking]
---
Mince @garlic{2%cloves} and sauté in @olive oil{2%tbsp}.
Cook the @pasta{500%g} in a #pot until al dente - Approximately ~{8%minutes}.
`
)
func Test_Reparser(t *testing.T) {
t.Log(input)
p := parser.New()
pRecipe, err := p.ParseString(input)
if err != nil {
t.Fatalf("Failed to parse recipe: %v", err)
}
t.Logf("Parsed Recipe: %#v", pRecipe)
recipe := ToCooklangRecipe(pRecipe)
if recipe == nil {
t.Fatal("Failed to convert parsed recipe to Recipe")
}
}
func TestGetCookware(t *testing.T) {
recipe := `---
title: Test Recipe
servings: 4
---
Cook the @pasta{500%g} in a #pot{} until al dente.
Transfer to a #serving bowl{} and add @cheese{100%g}.
Bake in an #oven{} at 180°C for ~{30%minutes}.
`
parsed, err := ParseString(recipe)
if err != nil {
t.Fatalf("Failed to parse recipe: %v", err)
}
cookware := parsed.GetCookware()
if len(cookware) != 3 {
t.Errorf("Expected 3 cookware items, got %d", len(cookware))
}
// Check that we got the expected cookware
expectedNames := []string{"pot", "serving bowl", "oven"}
for i, expected := range expectedNames {
if cookware[i].Name != expected {
t.Errorf("Expected cookware[%d].Name to be %q, got %q", i, expected, cookware[i].Name)
}
}
}
func TestGetCookwareWithQuantities(t *testing.T) {
recipe := `---
title: Test Recipe
---
Mix ingredients in #bowl{2}.
`
parsed, err := ParseString(recipe)
if err != nil {
t.Fatalf("Failed to parse recipe: %v", err)
}
cookware := parsed.GetCookware()
if len(cookware) != 1 {
t.Fatalf("Expected 1 cookware item, got %d", len(cookware))
}
if cookware[0].Name != "bowl" {
t.Errorf("Expected cookware name to be 'bowl', got %q", cookware[0].Name)
}
if cookware[0].Quantity != 2 {
t.Errorf("Expected cookware quantity to be 2, got %d", cookware[0].Quantity)
}
}
func TestGetCookwareEmpty(t *testing.T) {
recipe := `---
title: Test Recipe
---
Mix @flour{200%g} with @water{100%ml}.
`
parsed, err := ParseString(recipe)
if err != nil {
t.Fatalf("Failed to parse recipe: %v", err)
}
cookware := parsed.GetCookware()
if len(cookware) != 0 {
t.Errorf("Expected 0 cookware items, got %d", len(cookware))
}
}