@@ -171,10 +171,10 @@ instance StateModel s => Show (FailingAction s) where
171
171
show (ActionFail (ActionWithPolarity a pol)) = show pol ++ " : " ++ show a
172
172
173
173
data DynLogicTest s
174
- = BadPrecondition (TestSequence s ) (FailingAction s ) (Annotated s )
175
- | Looping (TestSequence s )
176
- | Stuck (TestSequence s ) (Annotated s )
177
- | DLScript (TestSequence s )
174
+ = BadPrecondition (Annotated s ) ( TestSequence s ) (FailingAction s ) (Annotated s )
175
+ | Looping (Annotated s ) ( TestSequence s )
176
+ | Stuck (Annotated s ) ( TestSequence s ) (Annotated s )
177
+ | DLScript (Annotated s ) ( TestSequence s )
178
178
179
179
data Witnesses r where
180
180
Do :: r -> Witnesses r
@@ -294,9 +294,10 @@ prettyWitnesses :: Witnesses () -> [String]
294
294
prettyWitnesses (Witness a w) = (" _ <- forAllQ $ exactlyQ $ " ++ show a) : prettyWitnesses w
295
295
prettyWitnesses Do {} = []
296
296
297
+ -- TODO: show initial state is
297
298
instance StateModel s => Show (DynLogicTest s ) where
298
- show (BadPrecondition ss bad s) =
299
- prettyTestSequence (usedVariables ss <> allVariables bad) ss
299
+ show (BadPrecondition is ss bad s) =
300
+ prettyTestSequence (usedVariables is ss <> allVariables bad) ss
300
301
++ " \n -- In state: "
301
302
++ show s
302
303
++ " \n "
@@ -309,12 +310,12 @@ instance StateModel s => Show (DynLogicTest s) where
309
310
f
310
311
| p == PosPolarity = " action"
311
312
| otherwise = " failingAction"
312
- show (Looping ss) = prettyTestSequence (usedVariables ss) ss ++ " \n pure ()\n -- Looping"
313
- show (Stuck ss s) = prettyTestSequence (usedVariables ss) ss ++ " \n pure ()\n -- Stuck in state " ++ show s
314
- show (DLScript ss) = prettyTestSequence (usedVariables ss) ss ++ " \n pure ()\n "
313
+ show (Looping is ss) = prettyTestSequence (usedVariables is ss) ss ++ " \n pure ()\n -- Looping"
314
+ show (Stuck is ss s) = prettyTestSequence (usedVariables is ss) ss ++ " \n pure ()\n -- Stuck in state " ++ show s
315
+ show (DLScript is ss) = prettyTestSequence (usedVariables is ss) ss ++ " \n pure ()\n "
315
316
316
- usedVariables :: forall s . StateModel s => TestSequence s -> VarContext
317
- usedVariables = go initialAnnotatedState
317
+ usedVariables :: forall s . StateModel s => Annotated s -> TestSequence s -> VarContext
318
+ usedVariables s = go s
318
319
where
319
320
go :: Annotated s -> TestSequence s -> VarContext
320
321
go aState TestSeqStop = allVariables (underlyingState aState)
@@ -342,10 +343,11 @@ restrictedPolar (ActionWithPolarity a _) = restricted a
342
343
-- `Actions` sequence into a proper `Property` that can be run by QuickCheck.
343
344
forAllScripts
344
345
:: (DynLogicModel s , Testable a )
345
- => DynFormula s
346
+ => s
347
+ -> DynFormula s
346
348
-> (Actions s -> a )
347
349
-> Property
348
- forAllScripts = forAllMappedScripts id id
350
+ forAllScripts s = forAllMappedScripts s id id
349
351
350
352
-- | `Property` function suitable for formulae without choice.
351
353
forAllUniqueScripts
@@ -365,16 +367,17 @@ forAllUniqueScripts s f k =
365
367
-- | Creates a `Property` from `DynFormula` with some specialised isomorphism for shrinking purpose.
366
368
forAllMappedScripts
367
369
:: (DynLogicModel s , Testable a )
368
- => (rep -> DynLogicTest s )
370
+ => s
371
+ -> (rep -> DynLogicTest s )
369
372
-> (DynLogicTest s -> rep )
370
373
-> DynFormula s
371
374
-> (Actions s -> a )
372
375
-> Property
373
- forAllMappedScripts to from f k =
376
+ forAllMappedScripts s to from f k =
374
377
QC. withSize $ \ n ->
375
378
let d = unDynFormula f n
376
379
in forAllShrinkBlind
377
- (Smart 0 <$> sized ((from <$> ) . generateDLTest d))
380
+ (Smart 0 <$> sized ((from <$> ) . generateDLTest ( Metadata mempty s) d))
378
381
(shrinkSmart ((from <$> ) . shrinkDLTest d . to))
379
382
$ \ (Smart _ script) ->
380
383
withDLScript d k (to script)
@@ -405,17 +408,24 @@ validDLTest test prop =
405
408
Stuck {} -> property Discard
406
409
_other -> counterexample (show test) False
407
410
408
- generateDLTest :: DynLogicModel s => DynLogic s -> Int -> Gen (DynLogicTest s )
409
- generateDLTest d size = generate chooseNextStep d 0 (initialStateFor d) size
411
+ generateDLTest :: DynLogicModel s => Annotated s -> DynLogic s -> Int -> Gen (DynLogicTest s )
412
+ generateDLTest s d size = do
413
+ generate chooseNextStep d 0 s size
410
414
411
415
onDLTestSeq :: (TestSequence s -> TestSequence s ) -> DynLogicTest s -> DynLogicTest s
412
- onDLTestSeq f (BadPrecondition ss bad s) = BadPrecondition (f ss) bad s
413
- onDLTestSeq f (Looping ss) = Looping (f ss)
414
- onDLTestSeq f (Stuck ss s) = Stuck (f ss) s
415
- onDLTestSeq f (DLScript ss) = DLScript (f ss)
416
+ onDLTestSeq f (BadPrecondition is ss bad s) = BadPrecondition is (f ss) bad s
417
+ onDLTestSeq f (Looping is ss) = Looping is (f ss)
418
+ onDLTestSeq f (Stuck is ss s) = Stuck is (f ss) s
419
+ onDLTestSeq f (DLScript is ss) = DLScript is (f ss)
420
+
421
+ setDLTestState :: Annotated s -> DynLogicTest s -> DynLogicTest s
422
+ setDLTestState is (BadPrecondition _ ss bad s) = BadPrecondition is ss bad s
423
+ setDLTestState is (Looping _ ss) = Looping is ss
424
+ setDLTestState is (Stuck _ ss s) = Stuck is ss s
425
+ setDLTestState is (DLScript _ ss) = DLScript is ss
416
426
417
- consDLTest :: TestStep s -> DynLogicTest s -> DynLogicTest s
418
- consDLTest step = onDLTestSeq (step :> )
427
+ consDLTest :: Annotated s -> TestStep s -> DynLogicTest s -> DynLogicTest s
428
+ consDLTest is step = setDLTestState is . onDLTestSeq (step :> )
419
429
420
430
consDLTestW :: Witnesses () -> DynLogicTest s -> DynLogicTest s
421
431
consDLTestW w = onDLTestSeq (addW w)
@@ -433,15 +443,15 @@ generate
433
443
-> m (DynLogicTest s )
434
444
generate chooseNextStepFun d n s size =
435
445
if n > sizeLimit size
436
- then return $ Looping TestSeqStop
446
+ then return $ Looping s TestSeqStop
437
447
else do
438
448
let preferred = if n > size then stopping d else noStopping d
439
- useStep (BadAction (Witnesses ws bad)) _ = return $ BadPrecondition (TestSeqStopW ws) bad s
440
- useStep StoppingStep _ = return $ DLScript TestSeqStop
449
+ useStep (BadAction (Witnesses ws bad)) _ = return $ BadPrecondition s (TestSeqStopW ws) bad s
450
+ useStep StoppingStep _ = return $ DLScript s TestSeqStop
441
451
useStep (Stepping step d') _ =
442
452
case discardWitnesses step of
443
453
var := act ->
444
- consDLTest step
454
+ consDLTest s step
445
455
<$> generate
446
456
chooseNextStepFun
447
457
d'
@@ -451,15 +461,12 @@ generate chooseNextStepFun d n s size =
451
461
useStep NoStep alt = alt
452
462
foldr
453
463
(\ step k -> do try <- chooseNextStepFun s n step; useStep try k)
454
- (return $ Stuck TestSeqStop s)
464
+ (return $ Stuck s TestSeqStop s)
455
465
[preferred, noAny preferred, d, noAny d]
456
466
457
467
sizeLimit :: Int -> Int
458
468
sizeLimit size = 2 * size + 20
459
469
460
- initialStateFor :: StateModel s => DynLogic s -> Annotated s
461
- initialStateFor _ = initialAnnotatedState
462
-
463
470
stopping :: DynLogic s -> DynLogic s
464
471
stopping EmptySpec = EmptySpec
465
472
stopping Stop = Stop
@@ -589,22 +596,28 @@ keepTryingUntil n g p = do
589
596
if p x then return $ Just x else scale (+ 1 ) $ keepTryingUntil (n - 1 ) g p
590
597
591
598
shrinkDLTest :: DynLogicModel s => DynLogic s -> DynLogicTest s -> [DynLogicTest s ]
592
- shrinkDLTest _ (Looping _) = []
599
+ shrinkDLTest _ (Looping _ _ ) = []
593
600
shrinkDLTest d tc =
594
- [ test | as' <- shrinkScript d (getScript tc), let pruned = pruneDLTest d as'
595
- test = makeTestFromPruned d pruned,
596
- -- Don't shrink a non-executable test case to an executable one.
601
+ [ test
602
+ | as' <-
603
+ shrinkScript
604
+ (underlyingState $ getInitialState tc)
605
+ d
606
+ (getScript tc)
607
+ , let pruned = pruneDLTest (getInitialState tc) d as'
608
+ test = makeTestFromPruned (getInitialState tc) d pruned
609
+ , -- Don't shrink a non-executable test case to an executable one.
597
610
case (tc, test) of
598
- (DLScript _, _) -> True
599
- (_, DLScript _) -> False
611
+ (DLScript _ _ , _) -> True
612
+ (_, DLScript _ _ ) -> False
600
613
_ -> True
601
614
]
602
615
603
616
nextStateStep :: StateModel s => Step s -> Annotated s -> Annotated s
604
617
nextStateStep (var := act) s = computeNextState s act var
605
618
606
- shrinkScript :: forall s . DynLogicModel s => DynLogic s -> TestSequence s -> [TestSequence s ]
607
- shrinkScript = shrink' initialAnnotatedState
619
+ shrinkScript :: forall s . DynLogicModel s => s -> DynLogic s -> TestSequence s -> [TestSequence s ]
620
+ shrinkScript is = shrink' ( Metadata mempty is)
608
621
where
609
622
shrink' :: Annotated s -> DynLogic s -> TestSequence s -> [TestSequence s ]
610
623
shrink' s d ss = structural s d ss ++ nonstructural s d ss
@@ -648,8 +661,8 @@ shrinkWitness AfterAny{} _ = []
648
661
649
662
-- The result of pruning a list of actions is a prefix of a list of actions that
650
663
-- could have been generated by the dynamic logic.
651
- pruneDLTest :: forall s . DynLogicModel s => DynLogic s -> TestSequence s -> TestSequence s
652
- pruneDLTest dl = prune [dl] initialAnnotatedState
664
+ pruneDLTest :: forall s . DynLogicModel s => Annotated s -> DynLogic s -> TestSequence s -> TestSequence s
665
+ pruneDLTest is dl = prune [dl] is
653
666
where
654
667
prune [] _ _ = TestSeqStop
655
668
prune _ _ TestSeqStop = TestSeqStop
@@ -710,27 +723,29 @@ demonicAlt ds = foldr1 (Alt Demonic) ds
710
723
711
724
propPruningGeneratedScriptIsNoop :: DynLogicModel s => DynLogic s -> Property
712
725
propPruningGeneratedScriptIsNoop d =
713
- forAll (sized $ \ n -> choose (1 , max 1 n) >>= generateDLTest d) $ \ test ->
714
- let script = case test of
715
- BadPrecondition s _ _ -> s
716
- Looping s -> s
717
- Stuck s _ -> s
718
- DLScript s -> s
719
- in script == pruneDLTest d script
726
+ forAll initialState $ \ s ->
727
+ forAll (sized $ \ n -> choose (1 , max 1 n) >>= generateDLTest (Metadata mempty s) d) $ \ test ->
728
+ getScript test == pruneDLTest (getInitialState test) d (getScript test)
729
+
730
+ getInitialState :: DynLogicTest s -> Annotated s
731
+ getInitialState (BadPrecondition is _ _ _) = is
732
+ getInitialState (Looping is _) = is
733
+ getInitialState (Stuck is _ _) = is
734
+ getInitialState (DLScript is _) = is
720
735
721
736
getScript :: DynLogicTest s -> TestSequence s
722
- getScript (BadPrecondition s _ _) = s
723
- getScript (Looping s) = s
724
- getScript (Stuck s _) = s
725
- getScript (DLScript s) = s
737
+ getScript (BadPrecondition _ s _ _) = s
738
+ getScript (Looping _ s) = s
739
+ getScript (Stuck _ s _) = s
740
+ getScript (DLScript _ s) = s
726
741
727
- makeTestFromPruned :: forall s . DynLogicModel s => DynLogic s -> TestSequence s -> DynLogicTest s
728
- makeTestFromPruned dl = make dl initialAnnotatedState
742
+ makeTestFromPruned :: forall s . DynLogicModel s => Annotated s -> DynLogic s -> TestSequence s -> DynLogicTest s
743
+ makeTestFromPruned st dl = make dl st
729
744
where
730
745
make d s TestSeqStop
731
- | b : _ <- badActions @ s d s = BadPrecondition TestSeqStop b s
732
- | stuck d s = Stuck TestSeqStop s
733
- | otherwise = DLScript TestSeqStop
746
+ | b : _ <- badActions @ s d s = BadPrecondition s TestSeqStop b s
747
+ | stuck d s = Stuck s TestSeqStop s
748
+ | otherwise = DLScript s TestSeqStop
734
749
make d s (TestSeqWitness a ss) =
735
750
onDLTestSeq (TestSeqWitness a) $
736
751
make
@@ -747,13 +762,7 @@ makeTestFromPruned dl = make dl initialAnnotatedState
747
762
-- | If failed, return the prefix up to the failure. Also prunes the test in case the model has
748
763
-- changed.
749
764
unfailDLTest :: DynLogicModel s => DynLogic s -> DynLogicTest s -> DynLogicTest s
750
- unfailDLTest d test = makeTestFromPruned d $ pruneDLTest d steps
751
- where
752
- steps = case test of
753
- BadPrecondition as _ _ -> as
754
- Stuck as _ -> as
755
- DLScript as -> as
756
- Looping as -> as
765
+ unfailDLTest d test = makeTestFromPruned (getInitialState test) d $ pruneDLTest (getInitialState test) d (getScript test)
757
766
758
767
stuck :: DynLogicModel s => DynLogic s -> Annotated s -> Bool
759
768
stuck EmptySpec _ = True
@@ -778,8 +787,8 @@ stuck (ForAll _ _) _ = False
778
787
stuck (Monitor _ d) s = stuck d s
779
788
780
789
scriptFromDL :: DynLogicTest s -> Actions s
781
- scriptFromDL (DLScript s) = Actions $ sequenceSteps s
782
- scriptFromDL _ = Actions []
790
+ scriptFromDL (DLScript is s) = Actions (underlyingState is) $ sequenceSteps s
791
+ scriptFromDL test = Actions (underlyingState $ getInitialState test) []
783
792
784
793
sequenceSteps :: TestSequence s -> [Step s ]
785
794
sequenceSteps (TestSeq ss) =
@@ -818,8 +827,8 @@ badActions (ForAll _ _) _ = []
818
827
badActions (Monitor _ d) s = badActions d s
819
828
820
829
applyMonitoring :: DynLogicModel s => DynLogic s -> DynLogicTest s -> Property -> Property
821
- applyMonitoring d (DLScript s) p =
822
- case findMonitoring d initialAnnotatedState s of
830
+ applyMonitoring d (DLScript is s) p =
831
+ case findMonitoring d is s of
823
832
Just f -> f p
824
833
Nothing -> p
825
834
applyMonitoring _ Stuck {} p = p
0 commit comments