-
Notifications
You must be signed in to change notification settings - Fork 9
/
Main.hs
139 lines (120 loc) · 4.25 KB
/
Main.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
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
------------------------------------------------------------------------------
import Control.Lens hiding (elements)
import Data.Foldable
import Data.Time
import GHC.Generics
import System.Random
import Test.Hspec
import Data.Text (unpack)
------------------------------------------------------------------------------
import Fake.Cover
import Fake.Combinators
import Fake.Provider.Lang
import Fake.Provider.Person.EN_US
import Fake.Types
------------------------------------------------------------------------------
instance Cover Int where
cover = Coverage [fakeInt 0 100]
instance Cover Char where
cover = Coverage [fakeLetter]
data ThreePhonetic = Alpha | Bravo | Charlie
deriving (Eq,Ord,Show,Generic)
instance Cover ThreePhonetic where
cover = gcover
data Four = MOne Int
| MTwo Char
| MThree Int
| MFour Char
deriving (Eq,Ord,Show,Generic)
makePrisms ''Four
instance Cover Four where
cover = gcover
-- Use gcover for the majority of the generation, then tweak one field to
-- be a different distribution.
&>>= _MFour %%~ (\_ -> fakeEnumFromTo 'A' 'F')
birthdayCoverage :: Coverage Day
birthdayCoverage = fromGregorian
<$> Coverage [fakeEnumFromTo 1950 2017]
<*> Coverage [fakeInt 1 12]
<*> Coverage [fakeInt 1 31]
ssnCoverage :: Coverage String
ssnCoverage = Coverage [elements ["123-45-6789", "000-00-0000"]]
data Person = Person
{ personFirstName :: String
, personLastName :: String
, personBirthdate :: Day
, personSSN :: Maybe String
} deriving (Eq,Ord,Show,Generic)
------------------------------------------------------------------------------
-- | Must be able to define a Cover instnance for Person without needing to
-- define instances for Day and String.
--instance Cover Person where
-- cover = gcover
instance Cover Person where
cover = Person
<$> fmap unpack (Coverage [unSingleWord <$> firstName])
<*> fmap unpack (Coverage [unSingleWord <$> lastName])
<*> birthdayCoverage
<*> asum [ pure Nothing, Just <$> ssnCoverage ]
testFake :: FGen a -> a
testFake (MkFGen f) = f $ mkStdGen 5
tc :: Coverage a -> [a]
tc = testFake . sequence . unCoverage
------------------------------------------------------------------------------
main :: IO ()
main = hspec $ do
describe "Fake.Cover" $ do
#if MIN_VERSION_random(1,2,0)
it "Maybe Int" $
tc gcover `shouldBe` [Nothing, Just (37 :: Int)]
it "Either Int Char" $
tc gcover `shouldBe` [Left (8 :: Int), Right 'f']
it "(Maybe Int, ThreePhonetic)" $
tc gcover `shouldBe`
[(Nothing,Alpha),(Just (26 :: Int),Bravo),(Nothing,Charlie)]
it "(Either ThreePhonetic Four)" $
tc gcover `shouldBe`
[ Left Alpha
, Left Bravo
, Left Charlie
, Right (MOne 39)
, Right (MTwo 'u')
, Right (MThree 17)
, Right (MFour 'C')
]
-- Since Person contains one Maybe field, cover should generate two values
it "Person" $
tc cover `shouldBe`
[ Person "Jaylen" "Massey" (fromGregorian 1967 1 21) Nothing
, Person "Timothy" "Garcia" (fromGregorian 2007 4 18) (Just "000-00-0000")
]
#else
it "Maybe Int" $
tc gcover `shouldBe` [Nothing, Just (94 :: Int)]
it "Either Int Char" $
tc gcover `shouldBe` [Left (53 :: Int), Right 't']
it "(Maybe Int, ThreePhonetic)" $
tc gcover `shouldBe`
[(Nothing,Alpha),(Just (84 :: Int),Bravo),(Nothing,Charlie)]
it "(Either ThreePhonetic Four)" $
tc gcover `shouldBe`
[ Left Alpha
, Left Bravo
, Left Charlie
, Right (MOne 32)
, Right (MTwo 'j')
, Right (MThree 17)
, Right (MFour 'B')
]
-- Since Person contains one Maybe field, cover should generate two values
it "Person" $
tc cover `shouldBe`
[ Person "Opal" "Clark" (fromGregorian 1958 10 12) Nothing
, Person "Katherine" "Oneill" (fromGregorian 1966 07 21) (Just "123-45-6789")
]
#endif