Skip to content

Commit dbe1f31

Browse files
committed
Allow using a custom initial state when generating actions
1 parent 19eda90 commit dbe1f31

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

quickcheck-dynamic/src/Test/QuickCheck/StateModel.hs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class QCDProp state p | p -> state where
387387
instance QCDProp state (QCDProperty state) where
388388
qcdProperty = id
389389

390-
instance Testable p => QCDProp state (Actions state -> p) where
390+
instance (StateModel state, Testable p) => QCDProp state (Actions state -> p) where
391391
qcdProperty p = QCDProperty (property . p) defaultOptions
392392

393393
modifyOptions :: QCDProperty state -> (Options state -> Options state) -> QCDProperty state
@@ -397,25 +397,23 @@ modifyOptions p f =
397397

398398
moreActions :: QCDProp state p => Rational -> p -> QCDProperty state
399399
moreActions r p =
400-
modifyOptions (qcdProperty p) $ \opts -> opts{actionLengthMultiplier = actionLengthMultiplier opts * r}
400+
modifyOptions (qcdProperty p) $ \opts -> opts{oActionLengthMultiplier = oActionLengthMultiplier opts * r}
401401

402-
-- NOTE: indexed on state for forwards compatibility, e.g. when we
403-
-- want to give an explicit initial state
404-
data Options state = Options {actionLengthMultiplier :: Rational}
402+
data Options state = Options {oActionLengthMultiplier :: Rational, oInitialAnnotatedState :: Annotated state}
405403

406-
defaultOptions :: Options state
407-
defaultOptions = Options{actionLengthMultiplier = 1}
404+
defaultOptions :: StateModel state => Options state
405+
defaultOptions = Options{oActionLengthMultiplier = 1, oInitialAnnotatedState = initialAnnotatedState}
408406

409407
-- | Generate arbitrary actions with the `GenActionsOptions`. More flexible than using the type-based
410408
-- modifiers.
411409
generateActionsWithOptions :: forall state. StateModel state => Options state -> Gen (Actions state)
412410
generateActionsWithOptions Options{..} = do
413-
(as, rejected) <- arbActions [] [] initialAnnotatedState 1
411+
(as, rejected) <- arbActions [] [] oInitialAnnotatedState 1
414412
return $ Actions_ rejected (Smart 0 as)
415413
where
416414
arbActions :: [Step state] -> [String] -> Annotated state -> Int -> Gen ([Step state], [String])
417415
arbActions steps rejected s step = sized $ \n -> do
418-
let w = round (actionLengthMultiplier * fromIntegral n) `div` 2 + 1
416+
let w = round (oActionLengthMultiplier * fromIntegral n) `div` 2 + 1
419417
continue <- frequency [(1, pure False), (w, pure True)]
420418
if continue
421419
then do

0 commit comments

Comments
 (0)