{-# LANGUAGE CPP, DeriveDataTypeable, FlexibleContexts, FlexibleInstances, TypeSynonymInstances #-}
#if MIN_VERSION_base(4,5,0)
{-# LANGUAGE ConstraintKinds #-}
#endif
module Test.Tasty.HUnit.Orig where
import qualified Control.Exception as E
import Control.Monad
import Data.Typeable (Typeable)
#if MIN_VERSION_base(4,5,0)
import Data.CallStack
#else
#define HasCallStack Eq ()
callStack :: [a]
callStack = []
data SrcLoc = SrcLoc
{ srcLocPackage :: String
, srcLocModule :: String
, srcLocFile :: String
, srcLocStartLine :: Int
, srcLocStartCol :: Int
, srcLocEndLine :: Int
, srcLocEndCol :: Int
} deriving (Eq, Show)
#endif
type Assertion = IO ()
assertFailure
:: HasCallStack
=> String
-> IO a
assertFailure :: String -> IO a
assertFailure String
msg = HUnitFailure -> IO a
forall e a. Exception e => e -> IO a
E.throwIO (Maybe SrcLoc -> String -> HUnitFailure
HUnitFailure Maybe SrcLoc
location String
msg)
where
location :: Maybe SrcLoc
location :: Maybe SrcLoc
location = case [(String, SrcLoc)] -> [(String, SrcLoc)]
forall a. [a] -> [a]
reverse [(String, SrcLoc)]
HasCallStack => [(String, SrcLoc)]
callStack of
(String
_, SrcLoc
loc) : [(String, SrcLoc)]
_ -> SrcLoc -> Maybe SrcLoc
forall a. a -> Maybe a
Just SrcLoc
loc
[] -> Maybe SrcLoc
forall a. Maybe a
Nothing
assertBool
:: HasCallStack
=> String
-> Bool
-> Assertion
assertBool :: String -> Bool -> Assertion
assertBool String
msg Bool
b = Bool -> Assertion -> Assertion
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
b (String -> Assertion
forall a. HasCallStack => String -> IO a
assertFailure String
msg)
assertEqual
:: (Eq a, Show a, HasCallStack)
=> String
-> a
-> a
-> Assertion
assertEqual :: String -> a -> a -> Assertion
assertEqual String
preface a
expected a
actual =
Bool -> Assertion -> Assertion
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (a
actual a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
expected) (String -> Assertion
forall a. HasCallStack => String -> IO a
assertFailure String
msg)
where msg :: String
msg = (if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
preface then String
"" else String
preface String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n") String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"expected: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
expected String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n but got: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
actual
infix 1 @?, @=?, @?=
(@=?)
:: (Eq a, Show a, HasCallStack)
=> a
-> a
-> Assertion
a
expected @=? :: a -> a -> Assertion
@=? a
actual = String -> a -> a -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" a
expected a
actual
(@?=)
:: (Eq a, Show a, HasCallStack)
=> a
-> a
-> Assertion
a
actual @?= :: a -> a -> Assertion
@?= a
expected = String -> a -> a -> Assertion
forall a.
(Eq a, Show a, HasCallStack) =>
String -> a -> a -> Assertion
assertEqual String
"" a
expected a
actual
(@?) :: (AssertionPredicable t, HasCallStack)
=> t
-> String
-> Assertion
t
predi @? :: t -> String -> Assertion
@? String
msg = t -> IO Bool
forall t. AssertionPredicable t => t -> IO Bool
assertionPredicate t
predi IO Bool -> (Bool -> Assertion) -> Assertion
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => String -> Bool -> Assertion
String -> Bool -> Assertion
assertBool String
msg
class AssertionPredicable t
where assertionPredicate :: t -> IO Bool
instance AssertionPredicable Bool
where assertionPredicate :: Bool -> IO Bool
assertionPredicate = Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return
instance (AssertionPredicable t) => AssertionPredicable (IO t)
where assertionPredicate :: IO t -> IO Bool
assertionPredicate = (IO t -> (t -> IO Bool) -> IO Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= t -> IO Bool
forall t. AssertionPredicable t => t -> IO Bool
assertionPredicate)
data HUnitFailure = HUnitFailure (Maybe SrcLoc) String
deriving (HUnitFailure -> HUnitFailure -> Bool
(HUnitFailure -> HUnitFailure -> Bool)
-> (HUnitFailure -> HUnitFailure -> Bool) -> Eq HUnitFailure
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HUnitFailure -> HUnitFailure -> Bool
$c/= :: HUnitFailure -> HUnitFailure -> Bool
== :: HUnitFailure -> HUnitFailure -> Bool
$c== :: HUnitFailure -> HUnitFailure -> Bool
Eq, Int -> HUnitFailure -> String -> String
[HUnitFailure] -> String -> String
HUnitFailure -> String
(Int -> HUnitFailure -> String -> String)
-> (HUnitFailure -> String)
-> ([HUnitFailure] -> String -> String)
-> Show HUnitFailure
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [HUnitFailure] -> String -> String
$cshowList :: [HUnitFailure] -> String -> String
show :: HUnitFailure -> String
$cshow :: HUnitFailure -> String
showsPrec :: Int -> HUnitFailure -> String -> String
$cshowsPrec :: Int -> HUnitFailure -> String -> String
Show, Typeable)
instance E.Exception HUnitFailure
prependLocation :: Maybe SrcLoc -> String -> String
prependLocation :: Maybe SrcLoc -> String -> String
prependLocation Maybe SrcLoc
mbloc String
s =
case Maybe SrcLoc
mbloc of
Maybe SrcLoc
Nothing -> String
s
Just SrcLoc
loc -> SrcLoc -> String
srcLocFile SrcLoc
loc String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (SrcLoc -> Int
srcLocStartLine SrcLoc
loc) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
{-# DEPRECATED assertString "Why not use assertBool instead?" #-}
{-# DEPRECATED Assertable, AssertionPredicate
"This class or type seems dubious. If you have a good use case for it, please create an issue for tasty. Otherwise, it may be removed in a future version." #-}
assertString
:: HasCallStack
=> String
-> Assertion
assertString :: String -> Assertion
assertString String
s = Bool -> Assertion -> Assertion
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
s) (String -> Assertion
forall a. HasCallStack => String -> IO a
assertFailure String
s)
class Assertable t
where assert :: t -> Assertion
instance Assertable ()
where assert :: () -> Assertion
assert = () -> Assertion
forall (m :: * -> *) a. Monad m => a -> m a
return
instance Assertable Bool
where assert :: Bool -> Assertion
assert = HasCallStack => String -> Bool -> Assertion
String -> Bool -> Assertion
assertBool String
""
instance (Assertable t) => Assertable (IO t)
where assert :: IO t -> Assertion
assert = (IO t -> (t -> Assertion) -> Assertion
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= t -> Assertion
forall t. Assertable t => t -> Assertion
assert)
instance Assertable String
where assert :: String -> Assertion
assert = HasCallStack => String -> Assertion
String -> Assertion
assertString
type AssertionPredicate = IO Bool