-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpec.hs
349 lines (244 loc) · 18.6 KB
/
Spec.hs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
-- Copyright Isaac Van Doren 2022-present
import Test.Hspec ( hspec, describe, it, shouldBe )
import Text.Cook ( parseCook, Content(..), Result(..), Category(..), Item(..) )
main :: IO ()
main = hspec $ do
describe "TDD" $ do
describe "metadata" $ do
it "basic metadata" $ do
parseCook ">> source: https://isaacvando.com" `shouldBe` Right (Recipe [("source", "https://isaacvando.com")] [])
it "two metadata" $ do
parseCook ">> first: metadata\n>> second: metadata" `shouldBe` Right (Recipe [("first", "metadata"), ("second", "metadata")] [])
it "multi word key" $ do
parseCook ">> multi word: value" `shouldBe` Right (Recipe [("multi word", "value")] [])
it "simple extra spacing" $ do
parseCook ">> \t key: value \t\t\t\t \n" `shouldBe` Right (Recipe [("key", "value")] [])
it "multiple words with extra spaces" $ do
parseCook ">> key: this is the value " `shouldBe` Right (Recipe [("key", "this is the value")] [])
it "no spaces" $ do
parseCook ">>foo:bar" `shouldBe` Right (Recipe [("foo", "bar")] [])
describe "step" $ do
it "basic step" $ do
parseCook "this is a step" `shouldBe` Right (Recipe [] [[Text "this is a step"]])
it "step beginning with >" $ do
parseCook "> sneaky step" `shouldBe` Right (Recipe [] [[Text "> sneaky step"]])
it "two steps" $ do
parseCook "step one\nstep two" `shouldBe` Right (Recipe [] [[Text "step one"], [Text "step two"]])
it "space around step" $ do
parseCook "\n\nfoo\n\t" `shouldBe` Right (Recipe [] [[Text "foo"]])
describe "ingredient" $ do
it "basic ingredient" $ do
parseCook "add @apples" `shouldBe` Right (Recipe [] [[Text "add ", Ingredient "apples" "some" ""]])
it "ingredient with longer sentence" $ do
parseCook "add lots of delicious @apples" `shouldBe` Right (Recipe [] [[Text "add lots of delicious ", Ingredient "apples" "some" ""]])
it "multi word ingredient" $ do
parseCook "@honey crisp apples{}" `shouldBe` Right (Recipe [] [[Ingredient "honey crisp apples" "some" ""]])
it "extra spacing" $ do
parseCook "@ \thoney \t crisp\tapples {}" `shouldBe` Right (Recipe [] [[Ingredient "honey crisp apples" "some" ""]])
it "ingredient with quantity" $ do
parseCook "@honey crisp apples {a million}" `shouldBe` Right (Recipe [] [[Ingredient "honey crisp apples" "a million" ""]])
it "ingredient with quantity and unit" $ do
parseCook "@chives {10 % bushels}" `shouldBe` Right (Recipe [] [[Ingredient "chives" "10" "bushels"]])
it "ingredient with quantity and unit and no spaces" $ do
parseCook "@eggs{0.98%oz}" `shouldBe` Right (Recipe [] [[Ingredient "eggs" "0.98" "oz"]])
describe "cookware" $ do
it "basic cookware" $ do
parseCook "#pot" `shouldBe` Right (Recipe [] [[Cookware "pot" "1"]])
it "multiword cookware" $ do
parseCook "#stock pot{}" `shouldBe` Right (Recipe [] [[Cookware "stock pot" "1"]])
it "cookware with quantity" $ do
parseCook "# cast iron skillet {10}" `shouldBe` Right (Recipe [] [[Cookware "cast iron skillet" "10"]])
describe "timer" $ do
it "basic timer" $ do
parseCook "~{10 minutes}" `shouldBe` Right (Recipe [] [[Timer "" "10 minutes" ""]])
it "timer with label" $ do
parseCook "~stewed apples {10 minutes}" `shouldBe` Right (Recipe [] [[Timer "stewed apples" "10 minutes" ""]])
it "timer with amount and unit" $ do
parseCook "~grapefruit{2%hours}" `shouldBe` Right (Recipe [] [[Timer "grapefruit" "2" "hours"]])
it "timer without unit" $ do
parseCook "~apple" `shouldBe` Right (Recipe [] [[Timer "apple" "" ""]])
-- I'm not a big fan of this behavior as it loses information and could change the aesthetic qualities of a recipe
-- if fractions are desired but I am complying with the canonical tests.
describe "fractions" $ do
it "basic no spaces" $ do
parseCook "@parsley{1/2%lbs}" `shouldBe` Right (Recipe [] [[Ingredient "parsley" "0.5" "lbs"]])
it "repeating decimal" $ do
parseCook "@food{1/3}" `shouldBe` Right (Recipe [] [[Ingredient "food" "0.33" ""]])
it "bigger number" $ do
parseCook "@grapefruit{9999/5}" `shouldBe` Right (Recipe [] [[Ingredient "grapefruit" "1999.8" ""]])
it "leading zeros don't count" $ do
parseCook "#pot{08/3}" `shouldBe` Right (Recipe [] [[Cookware "pot" "08/3"]])
it "division by zero doesn't error" $ do
parseCook "~foo{10/0}" `shouldBe` Right (Recipe [] [[Timer "foo" "10/0" ""]])
describe "general step content" $ do
it "single ingredient followed by timer" $ do
parseCook "@food ~timer{10 minutes}" `shouldBe` Right (Recipe [] [[Ingredient "food" "some" "", Text " ", Timer "timer" "10 minutes" ""]])
it "single cookware followed by ingredient" $ do
parseCook "#pan @two foods{}" `shouldBe` Right (Recipe [] [[Cookware "pan" "1", Text " ", Ingredient "two foods" "some" ""]])
it "spaces for single word ingredient" $ do
parseCook "@ apple" `shouldBe` Right (Recipe [] [[Ingredient "apple" "some" ""]])
it "spaces for single word cookware" $ do
parseCook "# pot" `shouldBe` Right (Recipe [] [[Cookware "pot" "1"]])
it "spaces for single word timer" $ do
parseCook "~ banana" `shouldBe` Right (Recipe [] [[Timer "banana" "" ""]])
it "timer and cookware" $ do
parseCook "~apples and bananas in a #pot" `shouldBe` Right (Recipe [] [[Timer "apples" "" "", Text " and bananas in a ", Cookware "pot" "1"]])
describe "comments" $ do
it "single line comment" $ do
parseCook "-- this is a comment" `shouldBe` Right (Recipe [] [])
it "block comment" $ do
parseCook "[- single line block comment -]" `shouldBe` Right (Recipe [] [])
it "multi line block comment" $ do
parseCook "[- multi line \n block \n comment \n-]" `shouldBe` Right (Recipe [] [])
it "block comment containing a - and ]" $ do
parseCook "[- this ] is just - a comment ] with -]" `shouldBe` Right (Recipe [] [])
it "block comment containing - adjacent to end" $ do
parseCook "[-- comment --]" `shouldBe` Right (Recipe [] [])
it "step followed by inline comment" $ do
parseCook "i'm a step -- i'm a comment" `shouldBe` Right (Recipe [] [[Text "i'm a step "]])
it "step, inline comment, step" $ do
parseCook "i'm a step -- i'm a comment\nI'm another step" `shouldBe` Right (Recipe [] [[Text "i'm a step "], [Text "I'm another step"]])
it "ingredient followed by inline comment" $ do
parseCook "@cayenne pepper{} -- i'm a comment" `shouldBe` Right (Recipe [] [[Ingredient "cayenne pepper" "some" "", Text " "]])
it "step followed by block comment" $ do
parseCook "i'm a step [- i'm a comment -]" `shouldBe` Right (Recipe [] [[Text "i'm a step "]])
it "metadata followed by inline comment" $ do
parseCook ">> key: value -- i'm a comment" `shouldBe` Right (Recipe [("key","value")] [])
it "metadata followed by block comment" $ do
parseCook ">> key: value [-i'm a comment-]" `shouldBe` Right (Recipe [("key","value")] [])
describe "general" $ do
it "empty input" $ do
parseCook "" `shouldBe` Right (Recipe [] [])
it "step and metadata" $ do
parseCook "step one\n>> key: value" `shouldBe` Right (Recipe [("key","value")] [[Text "step one"]])
let fullRecipeText = unlines [
"-- this recipe is delicious!",
"[- spinach is optional but a good addition -]",
">> source: https://isaacvando.com",
">> time: 1 hr 20 min",
">> title: Grandma's Quiche",
"\n",
"Preheat the #oven to 325 degrees.",
"In a medium bowl, mix the @eggs{3}, @sour cream{1/2%cup}, @shredded cheese{1/3%cup}, @crushed french fried onions{1/3%cup}, and @chopped spinach{}.",
"Mix ingredients thoroughly and add to the @pie crust{1}.",
"\t\t\t\t\n\n\n",
"Bake for an ~{hour} or until the top is nicely brown and a toothpick comes out clean.",
"Serve with @fruit and enjoy!",
"\n\n",
"[- the",
"end -]"
]
let fullRecipe = Recipe [("source", "https://isaacvando.com"), ("time", "1 hr 20 min"), ("title", "Grandma's Quiche")]
[[Text "Preheat the ", Cookware "oven" "1", Text " to 325 degrees."]
, [Text "In a medium bowl, mix the ", Ingredient "eggs" "3" "", Text ", ", Ingredient "sour cream" "0.5" "cup",
Text ", ", Ingredient "shredded cheese" "0.33" "cup", Text ", ", Ingredient "crushed french fried onions" "0.33" "cup",
Text ", and ", Ingredient "chopped spinach" "some" "", Text "."]
, [Text "Mix ingredients thoroughly and add to the ", Ingredient "pie crust" "1" "", Text "."]
, [Text "Bake for an ", Timer "" "hour" "", Text " or until the top is nicely brown and a toothpick comes out clean."]
, [Text "Serve with ", Ingredient "fruit" "some" "", Text " and enjoy!"]]
it "full recipe" $ do
parseCook fullRecipeText `shouldBe` Right fullRecipe
describe "shopping list" $ do
it "basic" $ do
parseCook "[Aldi]\napples" `shouldBe` Right (Grouping [Category "Aldi" [Item "apples" ""]])
it "multiple stores" $ do
parseCook "[Costco]\nfoo\nbar\n\n[Walmart]\nbaz" `shouldBe` Right (Grouping [Category "Costco" [Item "foo" "", Item "bar" ""], Category "Walmart" [Item "baz" ""]])
it "markdown link" $ do
parseCook "[recipes]\n[here](https://isaacvando.com)" `shouldBe` Right (Grouping [Category "recipes" [Item "[here](https://isaacvando.com)" ""]])
it "synonym" $ do
parseCook "[fruit]\nstrawberry|berry of the straw" `shouldBe` Right (Grouping [Category "fruit" [Item "strawberry" "berry of the straw"]])
it "extra space" $ do
parseCook "[ Dinner food ] \n \t apples | NYC \n\n\n\n [lunch food \t] \n banana pie "
`shouldBe` Right (Grouping [Category "Dinner food" [Item "apples" "NYC"], Category "lunch food" [Item "banana pie" ""]])
it "no item" $ do
parseCook "[title]" `shouldBe` Right (Grouping [Category "title" []])
-- generated by GenerateCanonical.hs from canonical.yaml from https://github.com/cooklang/spec/blob/main/tests/canonical.yaml
describe "Canonical" $ do
it "BasicDirection" $ do
parseCook "Add a bit of chilli" `shouldBe` Right (Recipe [] [[Text "Add a bit of chilli"]])
it "Comments" $ do
parseCook "-- testing comments" `shouldBe` Right (Recipe [] [])
it "CommentsAfterIngredients" $ do
parseCook "@thyme{2%springs} -- testing comments\nand some text" `shouldBe` Right (Recipe [] [[Ingredient "thyme" "2" "springs",Text " "],[Text "and some text"]])
it "CommentsWithIngredients" $ do
parseCook "-- testing comments\n@thyme{2%springs}" `shouldBe` Right (Recipe [] [[Ingredient "thyme" "2" "springs"]])
it "DirectionsWithDegrees" $ do
parseCook "Heat oven up to 200°C" `shouldBe` Right (Recipe [] [[Text "Heat oven up to 200\176C"]])
it "DirectionsWithNumbers" $ do
parseCook "Heat 5L of water" `shouldBe` Right (Recipe [] [[Text "Heat 5L of water"]])
it "DirectionWithIngrident" $ do
parseCook "Add @chilli{3%items}, @ginger{10%g} and @milk{1%l}." `shouldBe` Right (Recipe [] [[Text "Add ",Ingredient "chilli" "3" "items",Text ", ",Ingredient "ginger" "10" "g",Text " and ",Ingredient "milk" "1" "l",Text "."]])
it "EquipmentMultipleWords" $ do
parseCook "Fry in #frying pan{}" `shouldBe` Right (Recipe [] [[Text "Fry in ",Cookware "frying pan" "1"]])
it "EquipmentMultipleWordsWithLeadingNumber" $ do
parseCook "Fry in #7-inch nonstick frying pan{ }" `shouldBe` Right (Recipe [] [[Text "Fry in ",Cookware "7-inch nonstick frying pan" "1"]])
it "EquipmentMultipleWordsWithSpaces" $ do
parseCook "Fry in #frying pan{ }" `shouldBe` Right (Recipe [] [[Text "Fry in ",Cookware "frying pan" "1"]])
it "EquipmentOneWord" $ do
parseCook "Simmer in #pan for some time" `shouldBe` Right (Recipe [] [[Text "Simmer in ",Cookware "pan" "1",Text " for some time"]])
it "EquipmentQuantity" $ do
parseCook "#frying pan{2}" `shouldBe` Right (Recipe [] [[Cookware "frying pan" "2"]])
it "EquipmentQuantityOneWord" $ do
parseCook "#frying pan{three}" `shouldBe` Right (Recipe [] [[Cookware "frying pan" "three"]])
it "EquipmentQuantityMultipleWords" $ do
parseCook "#frying pan{two small}" `shouldBe` Right (Recipe [] [[Cookware "frying pan" "two small"]])
it "Fractions" $ do
parseCook "@milk{1/2%cup}" `shouldBe` Right (Recipe [] [[Ingredient "milk" "0.5" "cup"]])
it "FractionsInDirections" $ do
parseCook "knife cut about every 1/2 inches" `shouldBe` Right (Recipe [] [[Text "knife cut about every 1/2 inches"]])
it "FractionsLike" $ do
parseCook "@milk{01/2%cup}" `shouldBe` Right (Recipe [] [[Ingredient "milk" "01/2" "cup"]])
it "FractionsWithSpaces" $ do
parseCook "@milk{1 / 2 %cup}" `shouldBe` Right (Recipe [] [[Ingredient "milk" "0.5" "cup"]])
it "IngredientMultipleWordsWithLeadingNumber" $ do
parseCook "Top with @1000 island dressing{ }" `shouldBe` Right (Recipe [] [[Text "Top with ",Ingredient "1000 island dressing" "some" ""]])
it "IngredientWithEmoji" $ do
parseCook "Add some @🧂" `shouldBe` Right (Recipe [] [[Text "Add some ",Ingredient "\129474" "some" ""]])
it "IngridentExplicitUnits" $ do
parseCook "@chilli{3%items}" `shouldBe` Right (Recipe [] [[Ingredient "chilli" "3" "items"]])
it "IngridentExplicitUnitsWithSpaces" $ do
parseCook "@chilli{ 3 % items }" `shouldBe` Right (Recipe [] [[Ingredient "chilli" "3" "items"]])
it "IngridentImplicitUnits" $ do
parseCook "@chilli{3}" `shouldBe` Right (Recipe [] [[Ingredient "chilli" "3" ""]])
it "IngridentNoUnits" $ do
parseCook "@chilli" `shouldBe` Right (Recipe [] [[Ingredient "chilli" "some" ""]])
it "IngridentNoUnitsNotOnlyString" $ do
parseCook "@5peppers" `shouldBe` Right (Recipe [] [[Ingredient "5peppers" "some" ""]])
it "IngridentWithNumbers" $ do
parseCook "@tipo 00 flour{250%g}" `shouldBe` Right (Recipe [] [[Ingredient "tipo 00 flour" "250" "g"]])
it "IngridentWithoutStopper" $ do
parseCook "@chilli cut into pieces" `shouldBe` Right (Recipe [] [[Ingredient "chilli" "some" "",Text " cut into pieces"]])
it "Metadata" $ do
parseCook ">> sourced: babooshka" `shouldBe` Right (Recipe [("sourced","babooshka")] [])
it "MetadataBreak" $ do
parseCook "hello >> sourced: babooshka" `shouldBe` Right (Recipe [] [[Text "hello >> sourced: babooshka"]])
it "MetadataMultiwordKey" $ do
parseCook ">> cooking time: 30 mins" `shouldBe` Right (Recipe [("cooking time","30 mins")] [])
it "MetadataMultiwordKeyWithSpaces" $ do
parseCook ">>cooking time :30 mins" `shouldBe` Right (Recipe [("cooking time","30 mins")] [])
it "MultiLineDirections" $ do
parseCook "Add a bit of chilli\n\n\nAdd a bit of hummus" `shouldBe` Right (Recipe [] [[Text "Add a bit of chilli"],[Text "Add a bit of hummus"]])
it "MultipleLines" $ do
parseCook ">> Prep Time: 15 minutes\n>> Cook Time: 30 minutes" `shouldBe` Right (Recipe [("Prep Time","15 minutes"),("Cook Time","30 minutes")] [])
it "MultiWordIngrident" $ do
parseCook "@hot chilli{3}" `shouldBe` Right (Recipe [] [[Ingredient "hot chilli" "3" ""]])
it "MultiWordIngridentNoAmount" $ do
parseCook "@hot chilli{}" `shouldBe` Right (Recipe [] [[Ingredient "hot chilli" "some" ""]])
it "MutipleIngridentsWithoutStopper" $ do
parseCook "@chilli cut into pieces and @garlic" `shouldBe` Right (Recipe [] [[Ingredient "chilli" "some" "",Text " cut into pieces and ",Ingredient "garlic" "some" ""]])
it "QuantityAsText" $ do
parseCook "@thyme{few%springs}" `shouldBe` Right (Recipe [] [[Ingredient "thyme" "few" "springs"]])
it "QuantityDigitalString" $ do
parseCook "@water{7 k }" `shouldBe` Right (Recipe [] [[Ingredient "water" "7 k" ""]])
it "Servings" $ do
parseCook ">> servings: 1|2|3" `shouldBe` Right (Recipe [("servings","1|2|3")] [])
it "SlashInText" $ do
parseCook "Preheat the oven to 200℃/Fan 180°C." `shouldBe` Right (Recipe [] [[Text "Preheat the oven to 200\8451/Fan 180\176C."]])
it "TimerDecimal" $ do
parseCook "Fry for ~{1.5%minutes}" `shouldBe` Right (Recipe [] [[Text "Fry for ",Timer "" "1.5" "minutes"]])
it "TimerFractional" $ do
parseCook "Fry for ~{1/2%hour}" `shouldBe` Right (Recipe [] [[Text "Fry for ",Timer "" "0.5" "hour"]])
it "TimerInteger" $ do
parseCook "Fry for ~{10%minutes}" `shouldBe` Right (Recipe [] [[Text "Fry for ",Timer "" "10" "minutes"]])
it "TimerWithName" $ do
parseCook "Fry for ~potato{42%minutes}" `shouldBe` Right (Recipe [] [[Text "Fry for ",Timer "potato" "42" "minutes"]])