Skip to content

Commit 4a1287d

Browse files
committed
Add instance IsTest (ContT () IO t)
1 parent 88f435b commit 4a1287d

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

core/Test/Tasty/Core.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
-- | Core types and definitions
22
{-# LANGUAGE DeriveGeneric #-}
33
{-# LANGUAGE ExistentialQuantification #-}
4+
{-# LANGUAGE FlexibleInstances #-}
45
{-# LANGUAGE LambdaCase #-}
56
{-# LANGUAGE RankNTypes #-}
7+
{-# LANGUAGE TypeApplications #-}
68
module Test.Tasty.Core
79
( FailureReason(..)
810
, Outcome(..)
@@ -31,11 +33,15 @@ module Test.Tasty.Core
3133
, foldTestTree
3234
, foldTestTree0
3335
, treeOptions
36+
, testFailed
3437
) where
3538

3639
import Control.Exception
40+
import Control.Monad.Trans.Cont (ContT(..))
41+
import Data.Coerce (coerce)
3742
import qualified Data.Map as Map
3843
import Data.Bifunctor (Bifunctor(second, bimap))
44+
import Data.IORef (newIORef, readIORef, atomicModifyIORef')
3945
import Data.List (mapAccumR)
4046
import Data.Monoid (Any (getAny, Any))
4147
import Data.Sequence ((|>))
@@ -217,6 +223,36 @@ class Typeable t => IsTest t where
217223
-- | The list of options that affect execution of tests of this type
218224
testOptions :: Tagged t [OptionDescription]
219225

226+
instance IsTest t => IsTest (ContT () IO t) where
227+
testOptions = coerce (testOptions @t)
228+
run opts (ContT k) yieldProgress = do
229+
resRef <- newIORef Nothing
230+
let runInIORef :: t -> IO ()
231+
runInIORef t = do
232+
res <- run opts t yieldProgress
233+
let err = testFailed "Continuation was called multiple times"
234+
atomicModifyIORef' resRef $ \prev ->
235+
(Just $ maybe res (const err) prev, ())
236+
k runInIORef
237+
maybeRes <- readIORef resRef
238+
pure $ case maybeRes of
239+
Nothing -> testFailed "Continuation was not called"
240+
Just r -> r
241+
242+
-- | 'Result' of a failed test.
243+
--
244+
-- @since 0.8
245+
testFailed
246+
:: String -- ^ description
247+
-> Result
248+
testFailed desc = Result
249+
{ resultOutcome = Failure TestFailed
250+
, resultDescription = desc
251+
, resultShortDescription = "FAIL"
252+
, resultTime = 0
253+
, resultDetailsPrinter = noResultDetails
254+
}
255+
220256
-- | The name of a test or a group of tests.
221257
--
222258
-- @since 0.1

core/Test/Tasty/Providers.hs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ testPassed desc = Result
3737
, resultDetailsPrinter = noResultDetails
3838
}
3939

40-
-- | 'Result' of a failed test.
41-
--
42-
-- @since 0.8
43-
testFailed
44-
:: String -- ^ description
45-
-> Result
46-
testFailed desc = Result
47-
{ resultOutcome = Failure TestFailed
48-
, resultDescription = desc
49-
, resultShortDescription = "FAIL"
50-
, resultTime = 0
51-
, resultDetailsPrinter = noResultDetails
52-
}
53-
5440
-- | 'Result' of a failed test with custom details printer
5541
--
5642
-- @since 1.3.1

0 commit comments

Comments
 (0)