{-# LANGUAGE CPP #-}
module Test.LeanCheck.Error
( holds
, fails
, exists
, counterExample
, counterExamples
, witness
, witnesses
, results
, fromError
, errorToNothing
, errorToFalse
, errorToTrue
, errorToLeft
, anyErrorToNothing
, anyErrorToLeft
, (?==?)
, (!==!)
, module Test.LeanCheck
)
where
#if __GLASGOW_HASKELL__ <= 704
import Prelude hiding (catch)
#endif
import Test.LeanCheck hiding
( holds
, fails
, exists
, counterExample
, counterExamples
, witness
, witnesses
, results
)
import qualified Test.LeanCheck as C
( holds
, fails
, results
)
import Control.Monad (liftM)
import System.IO.Unsafe (unsafePerformIO)
import Data.Maybe (listToMaybe, fromMaybe)
import Data.Function (on)
import Control.Exception ( evaluate
, catch
#if __GLASGOW_HASKELL__
, SomeException
, ArithException
, ArrayException
, ErrorCall
, PatternMatchFail
, catches
, Handler (Handler)
#endif
)
etom :: Either b a -> Maybe a
etom :: forall b a. Either b a -> Maybe a
etom (Right a
x) = forall a. a -> Maybe a
Just a
x
etom (Left b
_) = forall a. Maybe a
Nothing
errorToNothing :: a -> Maybe a
errorToNothing :: forall a. a -> Maybe a
errorToNothing = forall b a. Either b a -> Maybe a
etom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Either String a
errorToLeft
anyErrorToNothing :: a -> Maybe a
anyErrorToNothing :: forall a. a -> Maybe a
anyErrorToNothing = forall b a. Either b a -> Maybe a
etom forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Either String a
anyErrorToLeft
errorToLeft :: a -> Either String a
errorToLeft :: forall a. a -> Either String a
errorToLeft a
x = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
#if __GLASGOW_HASKELL__
(forall a b. b -> Either a b
Right forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a. a -> IO a
evaluate a
x) forall a. IO a -> [Handler a] -> IO a
`catches`
[ forall a e. Exception e => (e -> IO a) -> Handler a
Handler forall a b. (a -> b) -> a -> b
$ \ArithException
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show1st (ArithException
e :: ArithException)
, forall a e. Exception e => (e -> IO a) -> Handler a
Handler forall a b. (a -> b) -> a -> b
$ \ArrayException
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show1st (ArrayException
e :: ArrayException)
, forall a e. Exception e => (e -> IO a) -> Handler a
Handler forall a b. (a -> b) -> a -> b
$ \ErrorCall
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show1st (ErrorCall
e :: ErrorCall)
, forall a e. Exception e => (e -> IO a) -> Handler a
Handler forall a b. (a -> b) -> a -> b
$ \PatternMatchFail
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show1st (PatternMatchFail
e :: PatternMatchFail)
]
#else
(Right `liftM` evaluate x) `catch` (return . Left . show1st)
#endif
anyErrorToLeft :: a -> Either String a
anyErrorToLeft :: forall a. a -> Either String a
anyErrorToLeft a
x = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
#if __GLASGOW_HASKELL__
(forall a b. b -> Either a b
Right forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall a. a -> IO a
evaluate a
x)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` (\SomeException
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show1st (SomeException
e :: SomeException))
#else
(Right `liftM` evaluate x) `catch` (return . Left . show1st)
#endif
show1st :: Show a => a -> String
show1st :: forall a. Show a => a -> String
show1st = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
errorToFalse :: Bool -> Bool
errorToFalse :: Bool -> Bool
errorToFalse = forall a. a -> a -> a
fromError Bool
False
errorToTrue :: Bool -> Bool
errorToTrue :: Bool -> Bool
errorToTrue = forall a. a -> a -> a
fromError Bool
True
fromError :: a -> a -> a
fromError :: forall a. a -> a -> a
fromError a
x = forall a. a -> Maybe a -> a
fromMaybe a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
errorToNothing
(?==?) :: Eq a => a -> a -> Bool
?==? :: forall a. Eq a => a -> a -> Bool
(?==?) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. a -> Maybe a
errorToNothing
infix 4 ?==?
(!==!) :: Eq a => a -> a -> Bool
!==! :: forall a. Eq a => a -> a -> Bool
(!==!) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a. a -> Either String a
errorToLeft
infix 4 !==!
holds :: Testable a => Int -> a -> Bool
holds :: forall a. Testable a => Int -> a -> Bool
holds Int
n = Bool -> Bool
errorToFalse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => Int -> a -> Bool
C.holds Int
n
fails :: Testable a => Int -> a -> Bool
fails :: forall a. Testable a => Int -> a -> Bool
fails Int
n = Bool -> Bool
errorToTrue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => Int -> a -> Bool
C.fails Int
n
exists :: Testable a => Int -> a -> Bool
exists :: forall a. Testable a => Int -> a -> Bool
exists Int
n = forall (t :: * -> *). Foldable t => t Bool -> Bool
or forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => a -> [([String], Bool)]
results
counterExample :: Testable a => Int -> a -> Maybe [String]
counterExample :: forall a. Testable a => Int -> a -> Maybe [String]
counterExample Int
n = forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => Int -> a -> [[String]]
counterExamples Int
n
witness :: Testable a => Int -> a -> Maybe [String]
witness :: forall a. Testable a => Int -> a -> Maybe [String]
witness Int
n = forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => Int -> a -> [[String]]
witnesses Int
n
counterExamples :: Testable a => Int -> a -> [[String]]
counterExamples :: forall a. Testable a => Int -> a -> [[String]]
counterExamples Int
n = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => a -> [([String], Bool)]
results
witnesses :: Testable a => Int -> a -> [[String]]
witnesses :: forall a. Testable a => Int -> a -> [[String]]
witnesses Int
n = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => a -> [([String], Bool)]
results
results :: Testable a => a -> [([String],Bool)]
results :: forall a. Testable a => a -> [([String], Bool)]
results = forall a b. (a -> b) -> [a] -> [b]
map (forall {t} {b} {a}. (t -> b) -> (a, t) -> (a, b)
mapSnd Bool -> Bool
errorToFalse) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Testable a => a -> [([String], Bool)]
C.results
where
mapSnd :: (t -> b) -> (a, t) -> (a, b)
mapSnd t -> b
f (a
x,t
y) = (a
x,t -> b
f t
y)