git.iko.soy/ikoHSE/sc-task-2
on master help sign in
443 lines 17 kB history raw
1module Tests
2 ( tests,
3 )
4where
6import Data.Foldable
7import Data.Function
8import Data.List (sort, groupBy)
9import Task
10import Test.Tasty
11import Test.Tasty.HUnit
13tests :: TestTree
14tests = testGroup "Tests"
15 [ testGroup "List Monoids"
16 [ testCase "FirstMonoid" $ do
17 let
18 getFirst :: [a] -> Maybe a
19 getFirst = destructFirstMonoid . fold . fmap constructFirstMonoid
20 getFirst [1 :: Int, 2, 3] @=? Just 1
21 getFirst [] @=? (Nothing :: Maybe ())
22 getFirst [True, False] @=? Just True
23 getFirst [True, True, False] @=? Just True
24 getFirst [1 .. 1000] @=? Just (1 :: Int)
25 getFirst [100 .. 1000] @=? Just (100 :: Int)
26 getFirst [6, 2, 7, 398, 1984, 383, -1020] @=? Just (6 :: Int)
27 , testCase "LastMonoid" $ do
28 let
29 getLast :: [a] -> Maybe a
30 getLast = destructLastMonoid . fold . fmap constructLastMonoid
31 getLast [1 :: Int, 2, 3] @=? Just 3
32 getLast [] @=? (Nothing :: Maybe ())
33 getLast [1 .. 1000] @=? Just (1000 :: Int)
34 getLast [100 .. 10000] @=? Just (10000 :: Int)
35 getLast [6, 2, 7, 398, 1984, 383, -1020, 498] @=? Just (498 :: Int)
36 getLast [True, False] @=? Just False
37 getLast [True, True, False] @=? Just False
38 getLast [True, True, False, True] @=? Just True
39 ]
40 , testGroup "Todo List" $
41 let createTasks :: [(String, Priority)] -> MyTaskManager
42 createTasks = foldl (\t (a, b) -> createTask a b t) emptyTaskManager
43 getPrioritySets :: MyTaskManager -> [[(Completion, String)]]
44 getPrioritySets =
45 fmap (sort . fmap (\(a, _, b) -> (a, b)))
46 . groupBy ((==) `on` (\(_, p, _) -> p))
47 . getPriorityList
48 in
49 [ testCase "emptyTaskManager" $
50 getPriorityList (emptyTaskManager :: MyTaskManager) @=? []
51 , testCase "createTask" $ do
52 getPrioritySets
53 ( createTasks
54 [ ("a", Low),
55 ("aaa", Medium),
56 ("AAAAAAAAAA", High)
57 ]
58 )
59 @=? [ [(NotCompleted, "AAAAAAAAAA")],
60 [(NotCompleted, "aaa")],
61 [(NotCompleted, "a")]
62 ]
63 getPrioritySets
64 ( createTasks
65 [ ("a", Low),
66 ("AAAAAAAAAA", High)
67 ]
68 )
69 @=? [ [(NotCompleted, "AAAAAAAAAA")],
70 [(NotCompleted, "a")]
71 ]
72 getPrioritySets
73 ( createTasks
74 [ ("a", Low),
75 ("AAAAAAAAAA", High),
76 ("aaa", Medium),
77 ("oh HaiMark", Medium)
78 ]
79 )
80 @=? [ [(NotCompleted, "AAAAAAAAAA")],
81 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
82 [(NotCompleted, "a")]
83 ]
84 getPrioritySets
85 ( createTasks
86 [ ("a", Low),
87 ("AAAAAAAAAA", High),
88 ("aaa", Medium),
89 ("oh HaiMark", Medium),
90 ("OH NO", High)
91 ]
92 )
93 @=? [ [(NotCompleted, "AAAAAAAAAA"), (NotCompleted, "OH NO")],
94 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
95 [(NotCompleted, "a")]
96 ]
97 , testCase "toggleTaskCompletion" $ do
98 getPrioritySets
99 ( toggleTaskCompletion "AAAAAAAAAA" . createTasks $
100 [ ("a", Low),
101 ("AAAAAAAAAA", High),
102 ("aaa", Medium),
103 ("oh HaiMark", Medium),
104 ("OH NO", High)
105 ]
106 )
107 @=? [ [(Completed, "AAAAAAAAAA"), (NotCompleted, "OH NO")],
108 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
109 [(NotCompleted, "a")]
110 ]
111 getPrioritySets
112 ( toggleTaskCompletion "AAAAAAAAAA" . toggleTaskCompletion "AAAAAAAAAA" . createTasks $
113 [ ("a", Low),
114 ("AAAAAAAAAA", High),
115 ("aaa", Medium),
116 ("oh HaiMark", Medium),
117 ("OH NO", High)
118 ]
119 )
120 @=? [ [(NotCompleted, "AAAAAAAAAA"), (NotCompleted, "OH NO")],
121 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
122 [(NotCompleted, "a")]
123 ]
124 getPrioritySets
125 ( toggleTaskCompletion "a"
126 . toggleTaskCompletion "not here"
127 . toggleTaskCompletion "AAAAAAAAAA"
128 . createTasks
129 $ [ ("a", Low),
130 ("AAAAAAAAAA", High),
131 ("aaa", Medium),
132 ("oh HaiMark", Medium),
133 ("OH NO", High)
134 ]
135 )
136 @=? [ [(Completed, "AAAAAAAAAA"), (NotCompleted, "OH NO")],
137 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
138 [(Completed, "a")]
139 ]
140 , testCase "removeTask" $ do
141 getPrioritySets
142 ( removeTask "AAAAAAAAAA"
143 . createTasks
144 $ [ ("a", Low),
145 ("AAAAAAAAAA", High),
146 ("aaa", Medium),
147 ("oh HaiMark", Medium),
148 ("OH NO", High)
149 ]
150 )
151 @=? [ [(NotCompleted, "OH NO")],
152 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
153 [(NotCompleted, "a")]
154 ]
155 getPrioritySets
156 ( removeTask "AAAAAAAAAA" . removeTask "AAAAAAAAAA"
157 . createTasks
158 $ [ ("a", Low),
159 ("AAAAAAAAAA", High),
160 ("aaa", Medium),
161 ("oh HaiMark", Medium),
162 ("OH NO", High)
163 ]
164 )
165 @=? [ [(NotCompleted, "OH NO")],
166 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
167 [(NotCompleted, "a")]
168 ]
169 getPrioritySets
170 ( removeTask "aaa" . removeTask "oh HaiMark"
171 . removeTask "AAAAAAAAAA"
172 . removeTask "AAAAAAAAAA"
173 . createTasks
174 $ [ ("a", Low),
175 ("AAAAAAAAAA", High),
176 ("aaa", Medium),
177 ("oh HaiMark", Medium),
178 ("OH NO", High)
179 ]
180 )
181 @=? [ [(NotCompleted, "OH NO")],
182 [(NotCompleted, "a")]
183 ]
184 , testCase "modifyPriority" $ do
185 getPrioritySets
186 ( modifyPriority "aaa" High . createTasks $
187 [ ("a", Low),
188 ("AAAAAAAAAA", High),
189 ("aaa", Medium),
190 ("oh HaiMark", Medium),
191 ("OH NO", High)
192 ]
193 )
194 @=? [ [(NotCompleted, "AAAAAAAAAA"), (NotCompleted, "OH NO"), (NotCompleted, "aaa")],
195 [(NotCompleted, "oh HaiMark")],
196 [(NotCompleted, "a")]
197 ]
198 getPrioritySets
199 ( modifyPriority "aaa" High
200 . toggleTaskCompletion "a"
201 . modifyPriority "a" High
202 . createTasks
203 $ [ ("a", Low),
204 ("AAAAAAAAAA", High),
205 ("aaa", Medium),
206 ("oh HaiMark", Medium),
207 ("OH NO", High)
208 ]
209 )
210 @=? [ [ (Completed, "a"),
211 (NotCompleted, "AAAAAAAAAA"),
212 (NotCompleted, "OH NO"),
213 (NotCompleted, "aaa")
214 ],
215 [(NotCompleted, "oh HaiMark")]
216 ]
217 getPrioritySets
218 ( modifyPriority "aaa" High
219 . modifyPriority "a" High
220 . modifyPriority "a" High
221 . modifyPriority "oh HaiMark" High
222 . createTasks
223 $ [ ("a", Low),
224 ("AAAAAAAAAA", High),
225 ("aaa", Medium),
226 ("oh HaiMark", Medium),
227 ("OH NO", High)
228 ]
229 )
230 @=? [ [ (NotCompleted, "AAAAAAAAAA"),
231 (NotCompleted, "OH NO"),
232 (NotCompleted, "a"),
233 (NotCompleted, "aaa"),
234 (NotCompleted, "oh HaiMark")
235 ]
236 ]
237 getPrioritySets
238 ( modifyPriority "aaa" High
239 . modifyPriority "a" High
240 . modifyPriority "ntestCase here sihji" High
241 . modifyPriority "oh HaiMark" Low
242 . modifyPriority "a" High
243 . modifyPriority "oh HaiMark" High
244 . createTasks
245 $ [ ("a", Low),
246 ("AAAAAAAAAA", High),
247 ("aaa", Medium),
248 ("oh HaiMark", Medium),
249 ("OH NO", High)
250 ]
251 )
252 @=? [ [ (NotCompleted, "AAAAAAAAAA"),
253 (NotCompleted, "OH NO"),
254 (NotCompleted, "a"),
255 (NotCompleted, "aaa")
256 ],
257 [(NotCompleted, "oh HaiMark")]
258 ]
259 , testCase "renameTask" $ do
260 getPrioritySets
261 ( renameTask "AAAAAAAAAA" "henlo"
262 . createTasks
263 $ [ ("a", Low),
264 ("AAAAAAAAAA", High),
265 ("aaa", Medium),
266 ("oh HaiMark", Medium),
267 ("OH NO", High)
268 ]
269 )
270 @=? [ [(NotCompleted, "OH NO"), (NotCompleted, "henlo")],
271 [(NotCompleted, "aaa"), (NotCompleted, "oh HaiMark")],
272 [(NotCompleted, "a")]
273 ]
274 getPrioritySets
275 ( renameTask "AAAAAAAAAA" "henlo"
276 . toggleTaskCompletion "a"
277 . renameTask "oh HaiMark" "iz task"
278 . createTasks
279 $ [ ("a", Low),
280 ("AAAAAAAAAA", High),
281 ("aaa", Medium),
282 ("oh HaiMark", Medium),
283 ("OH NO", High)
284 ]
285 )
286 @=? [ [(NotCompleted, "OH NO"), (NotCompleted, "henlo")],
287 [(NotCompleted, "aaa"), (NotCompleted, "iz task")],
288 [(Completed, "a")]
289 ]
290 getPrioritySets
291 ( renameTask "AAAAAAAAAA" "henlo"
292 . toggleTaskCompletion "iz task"
293 . renameTask "oh HaiMark" "iz task"
294 . renameTask "iz not here" "ajajaj"
295 . createTasks
296 $ [ ("a", Low),
297 ("AAAAAAAAAA", High),
298 ("aaa", Medium),
299 ("oh HaiMark", Medium),
300 ("OH NO", High)
301 ]
302 )
303 @=? [ [(NotCompleted, "OH NO"), (NotCompleted, "henlo")],
304 [(Completed, "iz task"), (NotCompleted, "aaa")],
305 [(NotCompleted, "a")]
306 ]
307 ]
308 , testGroup "Coffee"
309 [ testCase "HasPrice Coffee" $ do
310 price (Coffee 2 [OatMilk, Cinnamon]) @=? 2 + price OatMilk + price Cinnamon
311 price (Coffee 2 [Cinnamon, Cinnamon]) @=? 2 + price Cinnamon + price Cinnamon
312 price (Coffee 2 []) @=? 2
313 price (Coffee 2 [Cinnamon, Cream, Cinnamon])
314 @=? 2
315 + price Cinnamon
316 + price Cinnamon
317 + price Cream
318 , testCase "chargeCoffee" $ do
319 chargeCoffee (Coffee 2 [OatMilk, Cinnamon]) (DebitCard 199 69)
320 @=? Just (DebitCard 112 69)
321 chargeCoffee (Coffee 2 [OatMilk, Cinnamon]) (DebitCard 87 69)
322 @=? Just (DebitCard 0 69)
323 chargeCoffee (Coffee 2 [OatMilk, Cinnamon]) (DebitCard 86 69)
324 @=? Nothing
325 chargeCoffee (Coffee 2 [OatMilk, Cinnamon]) (DebitCard 0 69)
326 @=? Nothing
327 chargeCoffee (Coffee 2 [OatMilk, Cinnamon]) (DebitCard (-2189) 69)
328 @=? Nothing
329 chargeCoffee (Coffee 2 [OatMilk, Cinnamon, WhiteSugar]) (DebitCard 199 69)
330 @=? Just (DebitCard 97 69)
331 chargeCoffee (Coffee 2 [WhiteSugar, OatMilk, Cinnamon, WhiteSugar]) (DebitCard 199 69)
332 @=? Just (DebitCard 82 69)
333 , let coffee = Coffee 2 [OatMilk, Cinnamon]
334 in testCase "payForCoffee" $ do
335 payForCoffee (Customer [DebitCard 199 69] 0) coffee
336 @=? Just (Card (DebitCard 112 69))
337 payForCoffee (Customer [DebitCard 86 12, DebitCard 199 69] 2) coffee
338 @=? Just (Card (DebitCard 112 69))
339 payForCoffee (Customer [DebitCard (-12) 8, DebitCard 86 12, DebitCard 199 69] 0) coffee
340 @=? Just (Card (DebitCard 112 69))
341 payForCoffee (Customer [DebitCard 87 8, DebitCard 111 12, DebitCard 199 69] 0) coffee
342 @=? Just (Card (DebitCard 0 8))
343 payForCoffee (Customer [DebitCard 86 8, DebitCard 86 12, DebitCard 82 69] 0) coffee
344 @=? Nothing
345 payForCoffee (Customer [DebitCard 86 8, DebitCard 86 12, DebitCard 82 69] 87) coffee
346 @=? Just (Cash 0)
347 payForCoffee (Customer [DebitCard 86 8, DebitCard 86 12, DebitCard 81 69] 86) coffee
348 @=? Nothing
349 , testCase "applyPayment" $ do
350 applyPayment (Customer [DebitCard 199 69] 0) (Card (DebitCard 112 69))
351 @=? Customer [DebitCard 112 69] 0
352 applyPayment (Customer [DebitCard 111 12, DebitCard 199 69] 2) (Card (DebitCard 112 69))
353 @=? Customer [DebitCard 111 12, DebitCard 112 69] 2
354 applyPayment (Customer [DebitCard (-12) 8, DebitCard 111 12, DebitCard 199 69] 0) (Card (DebitCard 112 69))
355 @=? Customer [DebitCard (-12) 8, DebitCard 111 12, DebitCard 112 69] 0
356 applyPayment (Customer [DebitCard 112 8, DebitCard 111 12, DebitCard 199 69] 0) (Card (DebitCard 0 8))
357 @=? Customer [DebitCard 0 8, DebitCard 111 12, DebitCard 199 69] 0
358 applyPayment (Customer [DebitCard 111 8, DebitCard 111 12, DebitCard 110 69] 112) (Cash 0)
359 @=? Customer [DebitCard 111 8, DebitCard 111 12, DebitCard 110 69] 0
360 applyPayment (Customer [DebitCard 111 8, DebitCard 111 12, DebitCard 110 69] 112) (Card (DebitCard 0 13))
361 @=? Customer [DebitCard 111 8, DebitCard 111 12, DebitCard 110 69] 112
362 , let coffee = Coffee 2 [OatMilk, Cinnamon]
363 in testCase "buyCoffee" $ do
364 buyCoffee (Customer [DebitCard 199 69] 0) coffee
365 @=? Just (Customer [DebitCard 112 69] 0)
366 buyCoffee (Customer [DebitCard 86 12, DebitCard 199 69] 2) coffee
367 @=? Just (Customer [DebitCard 86 12, DebitCard 112 69] 2)
368 buyCoffee (Customer [DebitCard (-12) 8, DebitCard 86 12, DebitCard 199 69] 0) coffee
369 @=? Just (Customer [DebitCard (-12) 8, DebitCard 86 12, DebitCard 112 69] 0)
370 buyCoffee (Customer [DebitCard 87 8, DebitCard 86 12, DebitCard 199 69] 0) coffee
371 @=? Just (Customer [DebitCard 0 8, DebitCard 86 12, DebitCard 199 69] 0)
372 buyCoffee (Customer [DebitCard 86 8, DebitCard 86 12, DebitCard 84 69] 0) coffee
373 @=? Nothing
374 buyCoffee (Customer [DebitCard 86 8, DebitCard 86 12, DebitCard 82 69] 87) coffee
375 @=? Just (Customer [DebitCard 86 8, DebitCard 86 12, DebitCard 82 69] 0)
376 buyCoffee (Customer [DebitCard 86 8, DebitCard 86 12, DebitCard 81 69] 86) coffee
377 @=? Nothing
378 , testCase "saveTheDiabetic" $ do
379 saveTheDiabetic
380 [ Coffee 0 [WhiteSugar, BrownSugar, Cinnamon],
381 Coffee 0 [WhiteSugar, BrownSugar, Cinnamon],
382 Coffee 0 [WhiteSugar, BrownSugar, Cinnamon]
383 ]
384 @=? ( 150,
385 [ Coffee 0 [Cinnamon],
386 Coffee 0 [Cinnamon],
387 Coffee 0 [Cinnamon]
388 ]
389 )
390 saveTheDiabetic
391 [ Coffee 0 [WhiteSugar, BrownSugar, Cinnamon, BrownSugar, SoyMilk],
392 Coffee 0 [WhiteSugar, BrownSugar, WhiteSugar, WhiteSugar, Cinnamon],
393 Coffee 0 [WhiteSugar, BrownSugar, Cinnamon]
394 ]
395 @=? ( 215,
396 [ Coffee 0 [Cinnamon, SoyMilk],
397 Coffee 0 [Cinnamon],
398 Coffee 0 [Cinnamon]
399 ]
400 )
401 saveTheDiabetic
402 [ Coffee 0 [Cinnamon, SoyMilk],
403 Coffee 0 [Cinnamon],
404 Coffee 0 [Cinnamon],
405 Coffee 69 []
406 ]
407 @=? ( 0,
408 [ Coffee 0 [Cinnamon, SoyMilk],
409 Coffee 0 [Cinnamon],
410 Coffee 0 [Cinnamon],
411 Coffee 69 []
412 ]
413 )
414 saveTheDiabetic
415 []
416 @=? ( 0,
417 []
418 )
419 , testCase "calculateSugarDanger" $ do
420 calculateSugarDanger
421 [ Coffee 0 [WhiteSugar, BrownSugar, Cinnamon],
422 Coffee 0 [WhiteSugar, BrownSugar, Cinnamon],
423 Coffee 0 [WhiteSugar, BrownSugar, Cinnamon]
424 ]
425 @=? 9
426 calculateSugarDanger
427 [ Coffee 0 [WhiteSugar, BrownSugar, Cinnamon, BrownSugar, SoyMilk],
428 Coffee 0 [WhiteSugar, BrownSugar, WhiteSugar, WhiteSugar, Cinnamon],
429 Coffee 0 [WhiteSugar, BrownSugar, Cinnamon],
430 Coffee 0 []
431 ]
432 @=? 14
433 calculateSugarDanger
434 [ Coffee 0 [Cinnamon, SoyMilk],
435 Coffee 0 [Cinnamon],
436 Coffee 0 [Cinnamon]
437 ]
438 @=? 0
439 calculateSugarDanger
440 []
441 @=? 0
442 ]
443 ]