forked from kowainik/learn4haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kowainik#6] Add tests for Chapter 4 (kowainik#25)
* [kowainik#6] Add tests for Chapter 4 Resolves kowainik#6 * Update test/Test/Chapter4.hs Co-authored-by: Dmitrii Kovanikov <[email protected]> Co-authored-by: Dmitrii Kovanikov <[email protected]>
- Loading branch information
Showing
4 changed files
with
74 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,66 @@ | ||
{-# OPTIONS_GHC -Wno-type-defaults #-} | ||
|
||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Test.Chapter4 | ||
( chapter4 | ||
) where | ||
|
||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
import Chapter3 | ||
import Chapter4 | ||
|
||
|
||
chapter4 :: Spec | ||
chapter4 = describe "Chapter4" $ do | ||
describe "Chapter4Normal" $ it "" $ True `shouldBe` True | ||
describe "Chapter4Advanced" $ it "" $ True `shouldBe` True | ||
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` Trap False | ||
it "reward <*> reward" $ | ||
Reward odd <*> Reward 42 `shouldBe` Trap False | ||
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 "andM" $ 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" |