forked from ploeh/dependency-rejection-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaitreDTests.hs
61 lines (50 loc) · 2.18 KB
/
MaitreDTests.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
module MaitreDTests
( tryAcceptBehavesCorrectlyWhenItCanAccept
, tryAcceptBehavesCorrectlyWhenItCanNotAccept
) where
import Control.Monad (liftM2)
import Data.Maybe (isNothing)
import Data.Time (LocalTime (..), ZonedTime (..), midnight,
utc)
import Data.Time.Calendar (fromGregorian, gregorianMonthLength)
import MaitreD
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
genZonedTime :: Monad m => Gen m ZonedTime
genZonedTime = do
y <- Gen.int (Range.linear 1 9999)
m <- Gen.int (Range.linear 1 12)
d <- Gen.int (Range.linear 1 (gregorianMonthLength (toInteger y) m))
return $ ZonedTime (LocalTime (fromGregorian (toInteger y) m d) midnight) utc
genReservation :: Monad m => Gen m Reservation
genReservation = do
bookingDate <- genZonedTime
positiveQty <- Gen.int (Range.linear 1 99)
trueOrFalse <- Gen.bool
return Reservation
{ date = bookingDate
, quantity = positiveQty
, isAccepted = trueOrFalse }
sumBy :: Num a => (b -> a) -> [b] -> a
sumBy x xs = sum $ map x xs
tryAcceptBehavesCorrectlyWhenItCanAccept :: Property
tryAcceptBehavesCorrectlyWhenItCanAccept =
property $ do
reservation <- forAll genReservation
reservations <- forAll $ Gen.list (Range.linear 0 100) genReservation
excessCapacity <- forAll $ Gen.filter (>= 0) $ Gen.int (Range.linear 1 99)
let capacity = excessCapacity
+ sumBy quantity reservations
+ quantity reservation
actual = tryAccept capacity reservations reservation
Just (reservation { isAccepted = True }) === actual
tryAcceptBehavesCorrectlyWhenItCanNotAccept :: Property
tryAcceptBehavesCorrectlyWhenItCanNotAccept =
property $ do
reservation <- forAll genReservation
reservations <- forAll $ Gen.list (Range.linear 0 100) genReservation
lackingCapacity <- forAll $ Gen.filter (> 0) $ Gen.int (Range.linear 1 99)
let capacity = sumBy quantity reservations - lackingCapacity
actual = tryAccept capacity reservations reservation
Nothing === actual