Skip to content

Commit 5e58b38

Browse files
committed
Improve internal types
takeSmallest should return FailureReport, not Result, as the latter throws away information. This improves clarity and later extensions.
1 parent 94e3987 commit 5e58b38

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

hedgehog/src/Hedgehog/Internal/Runner.hs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,35 @@ takeSmallest ::
123123
-> ShrinkPath
124124
-> ShrinkLimit
125125
-> ShrinkRetries
126-
-> (Progress -> m ())
127-
-> NodeT m (Maybe (Either Failure (), Journal))
128-
-> m Result
126+
-> (FailureReport -> m ())
127+
-> Failure
128+
-> Journal
129+
-> [TreeT m (Maybe (Either Failure (), Journal))]
130+
-> m FailureReport
129131
takeSmallest shrinks0 (ShrinkPath shrinkPath0) slimit retries updateUI =
130132
let
131-
loop shrinks revShrinkPath = \case
132-
NodeT Nothing _ ->
133-
pure GaveUp
134-
135-
NodeT (Just (x, (Journal logs))) xs ->
136-
case x of
137-
Left (Failure loc err mdiff) -> do
138-
let
139-
shrinkPath =
140-
ShrinkPath $ reverse revShrinkPath
141-
failure =
142-
mkFailure shrinks shrinkPath Nothing loc err mdiff (reverse logs)
133+
loop shrinks revShrinkPath (Failure loc err mdiff) (Journal logs) xs = do
134+
let
135+
shrinkPath =
136+
ShrinkPath $ reverse revShrinkPath
137+
failure =
138+
mkFailure shrinks shrinkPath Nothing loc err mdiff (reverse logs)
143139

144-
updateUI $ Shrinking failure
140+
updateUI failure
145141

146-
if shrinks >= fromIntegral slimit then
147-
-- if we've hit the shrink limit, don't shrink any further
148-
pure $ Failed failure
149-
else
150-
findM (zip [0..] xs) (Failed failure) $ \(n, m) -> do
151-
o <- runTreeN retries m
152-
if isFailure o then
153-
Just <$> loop (shrinks + 1) (n : revShrinkPath) o
154-
else
155-
return Nothing
142+
if shrinks >= fromIntegral slimit then
143+
-- if we've hit the shrink limit, don't shrink any further
144+
pure failure
145+
else
146+
findM (zip [0..] xs) (failure) $ \(n, m) -> do
147+
o <- runTreeN retries m
148+
case o of
149+
NodeT (Just (Left smallerFailure, smallerLogs)) children ->
150+
Just <$>
151+
loop (shrinks + 1) (n : revShrinkPath) smallerFailure smallerLogs children
152+
_ ->
153+
return Nothing
156154

157-
Right () ->
158-
return OK
159155
in
160156
loop shrinks0 (reverse shrinkPath0)
161157

@@ -348,25 +344,27 @@ checkReport cfg size0 seed0 test0 updateUI = do
348344
Report (tests + 1) discards coverage0 seed0
349345
mkReport <$> skipToShrink shrinkPath (updateUI . mkReport) node
350346
_ -> do
351-
node@(NodeT x _) <-
347+
NodeT x trees <-
352348
runTreeT . evalGenT size s0 . runTestT $ unPropertyT test
353349
case x of
354350
Nothing ->
355351
loop tests (discards + 1) (size + 1) s1 coverage0
356352

357-
Just (Left _, _) ->
353+
Just (Left failure, journal) ->
358354
let
359355
mkReport =
360356
Report (tests + 1) discards coverage0 seed0
361357
in
362-
fmap mkReport $
358+
fmap (mkReport . Failed) $
363359
takeSmallest
364360
0
365361
(ShrinkPath [])
366362
(propertyShrinkLimit cfg)
367363
(propertyShrinkRetries cfg)
368-
(updateUI . mkReport)
369-
node
364+
(updateUI . mkReport . Shrinking)
365+
failure
366+
journal
367+
trees
370368

371369
Just (Right (), journal) ->
372370
let

0 commit comments

Comments
 (0)