1
+ {-# LANGUAGE RankNTypes #-}
2
+
1
3
module Test.Control.Concurrent.Class.MonadMVar.Strict.Checked where
2
4
3
5
import Control.Concurrent.Class.MonadMVar.Strict.Checked
6
+ import Control.Monad.IOSim
7
+ import Test.QuickCheck.Gen.Unsafe (Capture (.. ), capture )
4
8
import Test.QuickCheck.Monadic
5
9
import Test.Tasty
6
10
import Test.Tasty.QuickCheck
7
11
8
12
tests :: TestTree
9
13
tests = testGroup " Test.Control.Concurrent.Class.MonadMVar.Strict" [
10
14
testGroup " Checked" [
11
- testProperty " prop_invariantShouldFail" prop_invariantShouldFail
12
- , testProperty " prop_invariantShouldNotFail" prop_invariantShouldNotFail
15
+ testGroup " IO" [
16
+ testProperty " prop_invariantShouldFail" $
17
+ once $ expectFailure $ monadicIO prop_invariantShouldFail
18
+ , testProperty " prop_invariantShouldNotFail" $
19
+ once $ monadicIO prop_invariantShouldNotFail
20
+ ]
21
+ , testGroup " IOSim" [
22
+ testProperty " prop_invariantShouldFail" $
23
+ once $ expectFailure $ monadicSim prop_invariantShouldFail
24
+ , testProperty " prop_invariantShouldNotFail" $
25
+ once $ monadicSim prop_invariantShouldNotFail
26
+ ]
13
27
]
14
28
]
15
29
@@ -19,12 +33,24 @@ invPositiveInt x
19
33
| x >= 0 = Nothing
20
34
| otherwise = Just $ " x<0 for x=" <> show x
21
35
22
- prop_invariantShouldFail :: Property
23
- prop_invariantShouldFail = once $ expectFailure $ monadicIO $ run $ do
36
+ prop_invariantShouldNotFail :: MonadMVar m => PropertyM m ()
37
+ prop_invariantShouldNotFail = run $ do
24
38
v <- newMVarWithInvariant invPositiveInt 0
25
- modifyMVar_ v (\ x -> pure $ x - 1 )
39
+ modifyMVar_ v (\ x -> pure $ x + 1 )
26
40
27
- prop_invariantShouldNotFail :: Property
28
- prop_invariantShouldNotFail = monadicIO $ run $ do
41
+ prop_invariantShouldFail :: MonadMVar m => PropertyM m ()
42
+ prop_invariantShouldFail = run $ do
29
43
v <- newMVarWithInvariant invPositiveInt 0
30
- modifyMVar_ v (\ x -> pure $ x + 1 )
44
+ modifyMVar_ v (\ x -> pure $ x - 1 )
45
+
46
+ {- ------------------------------------------------------------------------------
47
+ Property runners (copied from "Ouroboros.Network.Testing.QuickCheck")
48
+ -------------------------------------------------------------------------------}
49
+
50
+ runSimGen :: (forall s . Gen (IOSim s a )) -> Gen a
51
+ runSimGen f = do
52
+ Capture eval <- capture
53
+ return $ runSimOrThrow (eval f)
54
+
55
+ monadicSim :: Testable a => (forall s . PropertyM (IOSim s ) a ) -> Property
56
+ monadicSim m = property (runSimGen (monadic' m))
0 commit comments