/
Chapter4.hs
66 lines (57 loc) Β· 2.66 KB
/
Chapter4.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
{-# OPTIONS_GHC -Wno-type-defaults #-}
{-# LANGUAGE TypeApplications #-}
module Test.Chapter4
( chapter4
) where
import Test.Hspec (Spec, describe, it, shouldBe)
import Chapter4
chapter4 :: Spec
chapter4 = describe "Chapter4" $ do
chapter4normal
chapter4advanced
chapter4normal :: Spec
chapter4normal = describe "Chapter4Normal" $ do
describe "Task2: Functor for Secret" $ do
let trap = Trap "it's a trap"
it "doen't affect trap" $
fmap @(Secret String) @Bool not trap `shouldBe` trap
it "change reward, same type" $
fmap @(Secret String) @Bool not (Reward False) `shouldBe` Reward True
it "change reward, other type" $
fmap @(Secret String) @Int even (Reward 5) `shouldBe` Reward False
it "change reward, other type" $
fmap @(Secret String) @Int even (Reward 4) `shouldBe` Reward True
describe "Task4: Applicative for Secret" $ do
let trap :: Secret String Int
trap = Trap "it's a trap"
it "pure int" $
pure @(Secret String) "x" `shouldBe` Reward "x"
it "pure bool" $
pure @(Secret String) False `shouldBe` Reward False
it "trap <*> reward" $
Trap "it's a trap" <*> Reward 42 `shouldBe` trap
it "trap <*> trap" $
Trap "it's a trap" <*> Trap "42" `shouldBe` trap
it "reward <*> trap" $
Reward not <*> Trap 42 `shouldBe` Trap 42
it "reward <*> reward - same type" $
Reward not <*> Reward True `shouldBe` (Reward False :: Secret String Bool)
it "reward <*> reward" $
Reward odd <*> Reward 42 `shouldBe` (Reward False :: Secret String Bool)
describe "Task6: Monad for Secret" $ do
it "Trap" $ (Trap "aaar" >>= halfSecret) `shouldBe` Trap "aaar"
it "Reward even" $ (Reward 42 >>= halfSecret) `shouldBe` Reward 21
it "Reward odd" $ (Reward 11 >>= halfSecret) `shouldBe` Trap "it's a trap"
chapter4advanced :: Spec
chapter4advanced = describe "Chapter4Advanced" $
describe "Task 8*: Before the Final Boss" $ do
it "Nothing - Nothing" $ andM Nothing Nothing `shouldBe` Nothing
it "Nothing - Just" $ andM Nothing (Just True) `shouldBe` Nothing
it "Just True - Nothing" $ andM (Just True) Nothing `shouldBe` Nothing
it "Just False - Nothing" $ andM (Just False) Nothing `shouldBe` Just False
it "Just - Just : False" $ andM (Just True) (Just False) `shouldBe` Just False
it "Just - Just : True" $ andM (Just True) (Just True) `shouldBe` Just True
halfSecret :: Int -> Secret String Int
halfSecret n
| even n = Reward (div n 2)
| otherwise = Trap "it's a trap"