|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 3 | +{-# LANGUAGE TemplateHaskell #-} |
| 4 | + |
| 5 | +module Test.Example.Shrink where |
| 6 | + |
| 7 | +import qualified Control.Concurrent as CC |
| 8 | +import Control.Monad (when) |
| 9 | +import Control.Monad.IO.Class (MonadIO(..)) |
| 10 | +import Data.IORef (IORef) |
| 11 | +import qualified Data.IORef as IORef |
| 12 | +#if MIN_VERSION_base(4,11,0) |
| 13 | +import qualified GHC.Clock as Clock |
| 14 | +#endif |
| 15 | + |
| 16 | +import Hedgehog |
| 17 | +import qualified Hedgehog.Gen as Gen |
| 18 | +import qualified Hedgehog.Range as Range |
| 19 | +import qualified Hedgehog.Internal.Config as Config |
| 20 | +import qualified Hedgehog.Internal.Property as Property |
| 21 | +import qualified Hedgehog.Internal.Runner as Runner |
| 22 | +import Hedgehog.Internal.Report (FailureReport(..), FailedAnnotation (..)) |
| 23 | +import Hedgehog.Internal.Report (Report(..), Result(..)) |
| 24 | + |
| 25 | +-- No limit fully shrinks (5) |
| 26 | +prop_ShrinkNoLimit :: Property |
| 27 | +prop_ShrinkNoLimit = |
| 28 | + withTests 1 . property $ do |
| 29 | + (report, gens) <- checkModProp id |
| 30 | + [50, 0, 25, 13, 7, 4, 6, 5] === gens |
| 31 | + case reportStatus report of |
| 32 | + Failed f -> 5 === failureShrinks f |
| 33 | + _ -> failure |
| 34 | + |
| 35 | +-- Shrinks 3 times |
| 36 | +prop_ShrinkLimit :: Property |
| 37 | +prop_ShrinkLimit = |
| 38 | + withTests 1 . property $ do |
| 39 | + (report, gens) <- checkModProp (withShrinks 3) |
| 40 | + [50, 0, 25, 13, 7] === gens |
| 41 | + case reportStatus report of |
| 42 | + Failed f -> 3 === failureShrinks f |
| 43 | + _ -> failure |
| 44 | + |
| 45 | +-- Timeout of 0 i.e. does not shrink at all |
| 46 | +prop_ShrinkTimeoutMicrosZero :: Property |
| 47 | +prop_ShrinkTimeoutMicrosZero = |
| 48 | + withTests 1 . property $ do |
| 49 | + (report, gens) <- checkModProp (withShrinkTimeoutMicros 0) |
| 50 | + [50] === gens |
| 51 | + case reportStatus report of |
| 52 | + Failed f -> 0 === failureShrinks f |
| 53 | + _ -> failure |
| 54 | + |
| 55 | +-- Timeout of 1,000,000 microseconds = 1 s. Verifies that we get a |
| 56 | +-- "partial" shrink. |
| 57 | +-- |
| 58 | +-- There is tension in the shrinkTime. On the one hand, we want it long enough |
| 59 | +-- so that we generate the four values [50, 0, 25, 13] before it gets stuck |
| 60 | +-- on 13. We don't want it too long, though, because that makes the test |
| 61 | +-- slower. |
| 62 | +-- |
| 63 | +-- Experience shows that values under 1 second would cause an occasional CI |
| 64 | +-- failure (the machine would not generate all four values before the timeout). |
| 65 | +-- A timeout of 1 second, on the other hand, passed CI with 10,000 tests |
| 66 | +-- (and took 7 hours!). Thus we use the 1 second timeout as it seems robust |
| 67 | +-- enough to not cause CI failures, and we cap the tests at 1 to keep the |
| 68 | +-- running time fast. |
| 69 | +prop_ShrinkTimeoutMicros :: Property |
| 70 | +prop_ShrinkTimeoutMicros = |
| 71 | + withTests 1 . property $ do |
| 72 | + -- Test generates [ 50 , 0 , 25 , 13 , 7 , 4 , 6 , 5 ] |
| 73 | + -- The 1 s timeout combined with the 10 s delay on 13 means |
| 74 | + -- shrinking will get stuck on 13, hence: |
| 75 | + -- - only generate [50 , 0 , 25 , 13] |
| 76 | + -- - final shrink value is 13 |
| 77 | + (report, gens) <- checkModPropGen delay (withShrinkTimeoutMicros shrinkTime) |
| 78 | + [50 , 0 , 25 , 13] === gens |
| 79 | + case reportStatus report of |
| 80 | + Failed f -> do |
| 81 | + 1 === failureShrinks f |
| 82 | + case failureAnnotations f of |
| 83 | + [ann] -> "25" === failedValue ann |
| 84 | + _ -> failure |
| 85 | + _ -> failure |
| 86 | + where |
| 87 | + delay x = when (x == 13) (liftIO $ CC.threadDelay delayTime) |
| 88 | + shrinkTime = 1000000 -- 1 sec |
| 89 | + -- Does not matter what this is, as long as it is longer than shrinkTime |
| 90 | + delayTime = 10000000 -- 10 sec |
| 91 | + |
| 92 | +-- Timeout of 2 seconds. Verifies that withShrinkTimeoutMicros indeed cancels |
| 93 | +-- shrinking within the time limit we want. |
| 94 | +prop_ShrinkTimeoutMicrosClock :: Property |
| 95 | +#if MIN_VERSION_base(4,11,0) |
| 96 | +prop_ShrinkTimeoutMicrosClock = |
| 97 | + withTests 1 . property $ do |
| 98 | + startTime <- liftIO $ Clock.getMonotonicTime |
| 99 | + annotateShow startTime |
| 100 | + _ <- checkModPropGen delay30s (withShrinkTimeoutMicros 2000000) |
| 101 | + endTime <- liftIO $ Clock.getMonotonicTime |
| 102 | + annotateShow endTime |
| 103 | + let timeElapsed = endTime - startTime |
| 104 | + annotateShow timeElapsed |
| 105 | + -- should be around 2 |
| 106 | + diff timeElapsed (>=) 1.5 |
| 107 | + diff timeElapsed (<=) 2.5 |
| 108 | + where |
| 109 | + delay30s x = when (x == 13) (liftIO $ CC.threadDelay 30000000) |
| 110 | +#else |
| 111 | +-- Needed because auto test discovery via $$(discover) picks up |
| 112 | +-- prop_ShrinkTimeoutMicrosClock name before cpp is evaluated. |
| 113 | +prop_ShrinkTimeoutMicrosClock = property (pure ()) |
| 114 | +#endif |
| 115 | + |
| 116 | +-- Given a property modifier, returns the property's report and generated |
| 117 | +-- values. |
| 118 | +checkModProp :: |
| 119 | + ( MonadIO m |
| 120 | + , MonadTest m |
| 121 | + ) |
| 122 | + => -- property modifier |
| 123 | + (Property -> Property) |
| 124 | + -> m (Report Result, [Int]) |
| 125 | +checkModProp = checkModPropGen (const (pure ())) |
| 126 | + |
| 127 | +-- checkModProp with function to run on the generated values |
| 128 | +checkModPropGen :: |
| 129 | + ( MonadIO m |
| 130 | + , MonadTest m |
| 131 | + ) |
| 132 | + => -- function to run on generated values |
| 133 | + (Int -> PropertyT IO ()) |
| 134 | + -- property modifier |
| 135 | + -> (Property -> Property) |
| 136 | + -> m (Report Result, [Int]) |
| 137 | +checkModPropGen onGen md = do |
| 138 | + gensRef <- liftIO $ IORef.newIORef [] |
| 139 | + report <- checkProp $ modProp onGen gensRef md |
| 140 | + gens <- liftIO $ reverse <$> IORef.readIORef gensRef |
| 141 | + annotateShow report |
| 142 | + annotateShow gens |
| 143 | + pure (report, gens) |
| 144 | + |
| 145 | +modProp :: |
| 146 | + -- function to run on generated values |
| 147 | + (Int -> PropertyT IO ()) |
| 148 | + -- reference to hold generated values |
| 149 | + -> IORef [Int] |
| 150 | + -- property modifier |
| 151 | + -> (Property -> Property) |
| 152 | + -> Property |
| 153 | +modProp onGen gensRef md = withTests 1 . md . property $ do |
| 154 | + -- [ 50 , 0 , 25 , 13 , 7 , 4 , 6 , 5 ] |
| 155 | + x :: Int <- forAll $ Gen.integral (Range.linearFrom 0 50 100) |
| 156 | + liftIO $ IORef.modifyIORef' gensRef (x :) |
| 157 | + onGen x |
| 158 | + diff x (<) 5 |
| 159 | + |
| 160 | +checkProp :: MonadIO m => Property -> m (Report Result) |
| 161 | +checkProp prop = do |
| 162 | + seed <- Config.resolveSeed Nothing |
| 163 | + liftIO $ Runner.checkReport |
| 164 | + (Property.propertyConfig prop) |
| 165 | + 0 |
| 166 | + seed |
| 167 | + (Property.propertyTest prop) |
| 168 | + (const $ pure ()) |
| 169 | + |
| 170 | +tests :: IO Bool |
| 171 | +tests = checkParallel $$(discover) |
0 commit comments