-
Notifications
You must be signed in to change notification settings - Fork 20
/
Hedgehog.hs
225 lines (197 loc) · 8.15 KB
/
Hedgehog.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
-- | This package lets you test Hedgehog properties with tasty.
--
-- Typical usage would look like this:
--
-- @
-- testGroup "tasty-hedgehog tests" [
-- testPropertyNamed "reverse involutive" "prop_reverse_involutive" prop_reverse_involutive
-- , testPropertyNamed "sort idempotent" "prop_sort_idempotent" prop_sort_idempotent
-- ]
-- @
--
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Test.Tasty.Hedgehog (
testProperty
, testPropertyNamed
, fromGroup
-- * Options you can pass in via tasty
, HedgehogReplay(..)
, HedgehogShowReplay(..)
, HedgehogTestLimit(..)
, HedgehogDiscardLimit(..)
, HedgehogShrinkLimit(..)
, HedgehogShrinkRetries(..)
) where
import Data.Maybe (fromMaybe)
import Data.Typeable
import qualified Test.Tasty as T
import qualified Test.Tasty.Providers as T
import Test.Tasty.Options
import Hedgehog
import Hedgehog.Internal.Config (UseColor, detectColor)
import Hedgehog.Internal.Property
import Hedgehog.Internal.Runner as H
import Hedgehog.Internal.Report
import Hedgehog.Internal.Seed as Seed
data HP = HP PropertyName Property
deriving (Typeable)
-- | Create a 'T.TestTree' from a Hedgehog 'Property'.
{-# DEPRECATED testProperty "testProperty will cause Hedgehog to provide incorrect instructions for re-checking properties, use testPropertyNamed instead." #-}
testProperty :: T.TestName -> Property -> T.TestTree
testProperty name prop = T.singleTest name (HP (PropertyName name) prop)
-- | `testPropertyNamed` @testName propertyName property@ creates a
-- 'T.TestTree' from @property@ using @testName@ as the displayed
-- description for the property. The @propertyName@ is used by Hedgehog
-- when a failure occurs to provide instructions for how to re-run
-- the property and should normally be set to a string representation
-- of the @property@ argument.
--
-- @
-- testPropertyNamed
-- "reverse is involutive"
-- "prop_reverse_involutive"
-- prop_reverse_involutive
-- @
--
-- @since 1.2.0.0
testPropertyNamed :: T.TestName -> PropertyName -> Property -> T.TestTree
testPropertyNamed name propName prop = T.singleTest name (HP propName prop)
-- | Create a 'T.TestTree' from a Hedgehog 'Group'.
fromGroup :: Group -> T.TestTree
fromGroup group =
T.testGroup (unGroupName $ groupName group) $
map mkTestTree (groupProperties group)
where
mkTestTree :: (PropertyName, Property) -> T.TestTree
mkTestTree (propName, prop) = testProperty (unPropertyName propName) prop
-- | The replay token to use for replaying a previous test run
newtype HedgehogReplay = HedgehogReplay (Maybe (Size, Seed))
deriving (Typeable)
instance IsOption HedgehogReplay where
defaultValue = HedgehogReplay Nothing
parseValue v = HedgehogReplay . Just <$> replay
-- Reads a replay token in the form "{size} {seed}"
where replay = (,) <$> safeRead (unwords size) <*> safeRead (unwords seed)
(size, seed) = splitAt 2 $ words v
optionName = return "hedgehog-replay"
optionHelp = return "Replay token to use for replaying a previous test run"
-- | If a test case fails, show a replay token for replaying tests
newtype HedgehogShowReplay = HedgehogShowReplay Bool
deriving (Typeable)
instance IsOption HedgehogShowReplay where
defaultValue = HedgehogShowReplay True
parseValue = fmap HedgehogShowReplay . safeRead
optionName = return "hedgehog-show-replay"
optionHelp = return "Show a replay token for replaying tests"
-- | The number of successful test cases required before Hedgehog will pass a test
newtype HedgehogTestLimit = HedgehogTestLimit (Maybe TestLimit)
deriving (Eq, Ord, Show, Typeable)
instance IsOption HedgehogTestLimit where
defaultValue = HedgehogTestLimit Nothing
parseValue = fmap (HedgehogTestLimit . Just . TestLimit) . safeRead
optionName = return "hedgehog-tests"
optionHelp = return "Number of successful test cases required before Hedgehog will pass a test"
-- | The number of discarded cases allowed before Hedgehog will fail a test
newtype HedgehogDiscardLimit = HedgehogDiscardLimit (Maybe DiscardLimit)
deriving (Eq, Ord, Show, Typeable)
instance IsOption HedgehogDiscardLimit where
defaultValue = HedgehogDiscardLimit Nothing
parseValue = fmap (HedgehogDiscardLimit . Just . DiscardLimit) . safeRead
optionName = return "hedgehog-discards"
optionHelp = return "Number of discarded cases allowed before Hedgehog will fail a test"
-- | The number of shrinks allowed before Hedgehog will fail a test
newtype HedgehogShrinkLimit = HedgehogShrinkLimit (Maybe ShrinkLimit)
deriving (Eq, Ord, Show, Typeable)
instance IsOption HedgehogShrinkLimit where
defaultValue = HedgehogShrinkLimit Nothing
parseValue = fmap (HedgehogShrinkLimit . Just . ShrinkLimit) . safeRead
optionName = return "hedgehog-shrinks"
optionHelp = return "Number of shrinks allowed before Hedgehog will fail a test"
-- | The number of times to re-run a test during shrinking
newtype HedgehogShrinkRetries = HedgehogShrinkRetries (Maybe ShrinkRetries)
deriving (Eq, Ord, Show, Typeable)
instance IsOption HedgehogShrinkRetries where
defaultValue = HedgehogShrinkRetries Nothing
parseValue = fmap (HedgehogShrinkRetries . Just . ShrinkRetries) . safeRead
optionName = return "hedgehog-retries"
optionHelp = return "Number of times to re-run a test during shrinking"
propertyTestLimit :: PropertyConfig -> TestLimit
propertyTestLimit =
let
getTestLimit (EarlyTermination _ tests) = tests
getTestLimit (NoEarlyTermination _ tests) = tests
getTestLimit (NoConfidenceTermination tests) = tests
in
getTestLimit . propertyTerminationCriteria
reportToProgress :: PropertyConfig
-> Report Progress
-> T.Progress
reportToProgress config (Report testsDone _ _ status) =
let
TestLimit testLimit = propertyTestLimit config
ShrinkLimit shrinkLimit = propertyShrinkLimit config
ratio x y = 1.0 * fromIntegral x / fromIntegral y
in
-- TODO add details for tests run / discarded / shrunk
case status of
Running ->
T.Progress "Running" (ratio testsDone testLimit)
Shrinking fr ->
T.Progress "Shrinking" (ratio (failureShrinks fr) shrinkLimit)
reportOutput :: Bool
-> UseColor
-> PropertyName
-> Report Result
-> IO String
reportOutput showReplay useColor name report = do
s <- renderResult useColor (Just name) report
pure $ case reportStatus report of
Failed fr ->
let
size = failureSize fr
seed = failureSeed fr
replayStr =
if showReplay
then
"\nUse '--hedgehog-replay \"" ++
show size ++ " " ++ show seed ++
"\"' to reproduce."
else ""
in
s ++ replayStr ++ "\n"
_ -> s
instance T.IsTest HP where
testOptions =
return [ Option (Proxy :: Proxy HedgehogReplay)
, Option (Proxy :: Proxy HedgehogShowReplay)
, Option (Proxy :: Proxy HedgehogTestLimit)
, Option (Proxy :: Proxy HedgehogDiscardLimit)
, Option (Proxy :: Proxy HedgehogShrinkLimit)
, Option (Proxy :: Proxy HedgehogShrinkRetries)
]
run opts (HP name (Property pConfig pTest)) yieldProgress = do
useColor <- detectColor
let
HedgehogReplay replay = lookupOption opts
HedgehogShowReplay showReplay = lookupOption opts
HedgehogTestLimit mTests = lookupOption opts
HedgehogDiscardLimit mDiscards = lookupOption opts
HedgehogShrinkLimit mShrinks = lookupOption opts
HedgehogShrinkRetries mRetries = lookupOption opts
config =
PropertyConfig
(fromMaybe (propertyDiscardLimit pConfig) mDiscards)
(fromMaybe (propertyShrinkLimit pConfig) mShrinks)
(fromMaybe (propertyShrinkRetries pConfig) mRetries)
(NoConfidenceTermination $ fromMaybe (propertyTestLimit pConfig) mTests)
randSeed <- Seed.random
let
size = maybe 0 fst replay
seed = maybe randSeed snd replay
report <- checkReport config size seed pTest (yieldProgress . reportToProgress config)
let
resultFn = if reportStatus report == OK
then T.testPassed
else T.testFailed
out <- reportOutput showReplay useColor name report
return $ resultFn out