-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add withShrinkTime to allow configuring shrink behavior in terms of time.
- Loading branch information
Showing
6 changed files
with
157 additions
and
7 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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Test.Hedgehog.Shrink where | ||
|
||
import Control.Monad.IO.Class (MonadIO(..)) | ||
|
||
import Hedgehog | ||
import qualified Hedgehog.Gen as Gen | ||
import qualified Hedgehog.Range as Range | ||
import qualified Hedgehog.Internal.Config as Config | ||
import qualified Hedgehog.Internal.Property as Property | ||
import qualified Hedgehog.Internal.Runner as Runner | ||
import Hedgehog.Internal.Report (Report(..), Result(..), FailureReport(..)) | ||
|
||
modProp :: (Property -> Property) -> Property | ||
modProp md = withTests 1 . md . property $ do | ||
x :: Int <- forAll $ Gen.integral (Range.linearFrom 0 500000 1000000) | ||
diff x (<) 5 | ||
|
||
checkProp :: MonadIO m => Property -> m (Report Result) | ||
checkProp prop = do | ||
seed <- Config.resolveSeed Nothing | ||
liftIO $ Runner.checkReport | ||
(Property.propertyConfig prop) | ||
0 | ||
seed | ||
(Property.propertyTest prop) | ||
(const $ pure ()) | ||
|
||
-- No limit fully shrinks (18) | ||
prop_ShrinkNoLimit :: Property | ||
prop_ShrinkNoLimit = | ||
property $ do | ||
report <- checkProp $ modProp id | ||
case reportStatus report of | ||
Failed f -> failureShrinks f === 18 | ||
_ -> failure | ||
|
||
-- Shrinks 3 times | ||
prop_ShrinkLimit :: Property | ||
prop_ShrinkLimit = | ||
property $ do | ||
report <- checkProp $ modProp (withShrinks 3) | ||
case reportStatus report of | ||
Failed f -> failureShrinks f === 3 | ||
_ -> failure | ||
|
||
-- Time limit of 0 i.e. does not shrink at all | ||
prop_ShrinkTimeLimitZero :: Property | ||
prop_ShrinkTimeLimitZero = | ||
property $ do | ||
report <- checkProp $ modProp (withShrinkTime 0) | ||
case reportStatus report of | ||
GaveUp -> pure () | ||
_ -> failure | ||
|
||
-- Time limit of 1 microsecond. Non-deterministic, can shrink s times w/ | ||
-- 0 <= s <= 18. | ||
prop_ShrinkTimeLimit :: Property | ||
prop_ShrinkTimeLimit = | ||
property $ do | ||
report <- checkProp $ modProp (withShrinkTime 1) | ||
case reportStatus report of | ||
Failed f -> do | ||
let shrinks = failureShrinks f | ||
diff shrinks (>=) 0 | ||
diff shrinks (<=) 18 | ||
GaveUp -> pure () | ||
_ -> failure | ||
|
||
tests :: IO Bool | ||
tests = checkParallel $$(discover) |
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