module Test.Chell
(
defaultMain
, Suite
, suite
, suiteName
, suiteTests
, SuiteOrTest
, skipIf
, skipWhen
, Assertions
, assertions
, IsAssertion
, Assertion
, assertionPassed
, assertionFailed
, assert
, expect
, die
, trace
, note
, afterTest
, requireLeft
, requireRight
, equal
, notEqual
, equalWithin
, just
, nothing
, left
, right
, throws
, throwsEq
, greater
, greaterEqual
, lesser
, lesserEqual
, sameItems
, equalItems
, IsText
, equalLines
, equalLinesWith
, Test
, test
, testName
, runTest
, TestResult (..)
, Failure
, failure
, failureLocation
, failureMessage
, Location
, location
, locationFile
, locationModule
, locationLine
, TestOptions
, defaultTestOptions
, testOptionSeed
, testOptionTimeout
) where
import qualified Control.Applicative
import qualified Control.Exception
import Control.Exception (Exception)
import Control.Monad (ap, liftM)
import Control.Monad.IO.Class (MonadIO, liftIO)
import qualified Data.Algorithm.Patience as Patience
import qualified Data.ByteString.Char8
import qualified Data.ByteString.Lazy.Char8
import Data.Foldable (Foldable, foldMap)
import Data.List (foldl', intercalate, sort)
import Data.Maybe (isJust, isNothing)
import Data.IORef (IORef, newIORef, readIORef, modifyIORef)
import qualified Data.Text
import Data.Text (Text)
import qualified Data.Text.Lazy
import qualified Language.Haskell.TH as TH
import Test.Chell.Main (defaultMain)
import Test.Chell.Types
data Assertion
= AssertionPassed
| AssertionFailed String
deriving (Eq, Show)
assertionPassed :: Assertion
assertionPassed = AssertionPassed
assertionFailed :: String -> Assertion
assertionFailed = AssertionFailed
class IsAssertion a where
runAssertion :: a -> IO Assertion
instance IsAssertion Assertion where
runAssertion = return
instance IsAssertion Bool where
runAssertion x = return $ if x
then assertionPassed
else assertionFailed "boolean assertion failed"
instance IsAssertion a => IsAssertion (IO a) where
runAssertion x = x >>= runAssertion
type TestState = (IORef [(String, String)], IORef [IO ()], [Failure])
newtype Assertions a = Assertions { unAssertions :: TestState -> IO (Maybe a, TestState) }
instance Functor Assertions where
fmap = liftM
instance Control.Applicative.Applicative Assertions where
pure = return
(<*>) = ap
instance Monad Assertions where
return x = Assertions (\s -> return (Just x, s))
m >>= f = Assertions (\s -> do
(maybe_a, s') <- unAssertions m s
case maybe_a of
Nothing -> return (Nothing, s')
Just a -> unAssertions (f a) s')
instance MonadIO Assertions where
liftIO io = Assertions (\s -> do
x <- io
return (Just x, s))
assertions :: String -> Assertions a -> Test
assertions name testm = test name $ \opts -> do
noteRef <- newIORef []
afterTestRef <- newIORef []
let getNotes = fmap reverse (readIORef noteRef)
let getResult = do
res <- unAssertions testm (noteRef, afterTestRef, [])
case res of
(_, (_, _, [])) -> do
notes <- getNotes
return (TestPassed notes)
(_, (_, _, fs)) -> do
notes <- getNotes
return (TestFailed notes (reverse fs))
Control.Exception.finally
(handleJankyIO opts getResult getNotes)
(runAfterTest afterTestRef)
runAfterTest :: IORef [IO ()] -> IO ()
runAfterTest ref = readIORef ref >>= loop where
loop [] = return ()
loop (io:ios) = Control.Exception.finally (loop ios) io
addFailure :: Maybe TH.Loc -> String -> Assertions ()
addFailure maybe_loc msg = Assertions $ \(notes, afterTestRef, fs) -> do
let loc = do
th_loc <- maybe_loc
return $ location
{ locationFile = TH.loc_filename th_loc
, locationModule = TH.loc_module th_loc
, locationLine = Just (toInteger (fst (TH.loc_start th_loc)))
}
let f = failure
{ failureLocation = loc
, failureMessage = msg
}
return (Just (), (notes, afterTestRef, f : fs))
die :: TH.Q TH.Exp
die = do
loc <- TH.location
let qloc = liftLoc loc
[| \msg -> dieAt $qloc ("die: " ++ msg) |]
dieAt :: TH.Loc -> String -> Assertions a
dieAt loc msg = do
addFailure (Just loc) msg
Assertions (\s -> return (Nothing, s))
trace :: TH.Q TH.Exp
trace = do
loc <- TH.location
let qloc = liftLoc loc
[| traceAt $qloc |]
traceAt :: TH.Loc -> String -> Assertions ()
traceAt loc msg = liftIO $ do
let file = TH.loc_filename loc
let line = fst (TH.loc_start loc)
putStr ("[" ++ file ++ ":" ++ show line ++ "] ")
putStrLn msg
note :: String -> String -> Assertions ()
note key value = Assertions (\(notes, afterTestRef, fs) -> do
modifyIORef notes ((key, value) :)
return (Just (), (notes, afterTestRef, fs)))
afterTest :: IO () -> Assertions ()
afterTest io = Assertions (\(notes, ref, fs) -> do
modifyIORef ref (io :)
return (Just (), (notes, ref, fs)))
requireLeft :: TH.Q TH.Exp
requireLeft = do
loc <- TH.location
let qloc = liftLoc loc
[| requireLeftAt $qloc |]
requireLeftAt :: Show b => TH.Loc -> Either a b -> Assertions a
requireLeftAt loc val = case val of
Left a -> return a
Right b -> do
let dummy = Right b `asTypeOf` Left ()
dieAt loc ("requireLeft: received " ++ showsPrec 11 dummy "")
requireRight :: TH.Q TH.Exp
requireRight = do
loc <- TH.location
let qloc = liftLoc loc
[| requireRightAt $qloc |]
requireRightAt :: Show a => TH.Loc -> Either a b -> Assertions b
requireRightAt loc val = case val of
Left a -> do
let dummy = Left a `asTypeOf` Right ()
dieAt loc ("requireRight: received " ++ showsPrec 11 dummy "")
Right b -> return b
liftLoc :: TH.Loc -> TH.Q TH.Exp
liftLoc loc = [| TH.Loc filename package module_ start end |] where
filename = TH.loc_filename loc
package = TH.loc_package loc
module_ = TH.loc_module loc
start = TH.loc_start loc
end = TH.loc_end loc
assertAt :: IsAssertion assertion => TH.Loc -> Bool -> assertion -> Assertions ()
assertAt loc fatal assertion = do
result <- liftIO (runAssertion assertion)
case result of
AssertionPassed -> return ()
AssertionFailed err -> if fatal
then dieAt loc err
else addFailure (Just loc) err
assert :: TH.Q TH.Exp
assert = do
loc <- TH.location
let qloc = liftLoc loc
[| assertAt $qloc True |]
expect :: TH.Q TH.Exp
expect = do
loc <- TH.location
let qloc = liftLoc loc
[| assertAt $qloc False |]
assertBool :: Bool -> String -> Assertion
assertBool True _ = assertionPassed
assertBool False err = AssertionFailed err
equal :: (Show a, Eq a) => a -> a -> Assertion
equal x y = assertBool (x == y) ("equal: " ++ show x ++ " is not equal to " ++ show y)
notEqual :: (Eq a, Show a) => a -> a -> Assertion
notEqual x y = assertBool (x /= y) ("notEqual: " ++ show x ++ " is equal to " ++ show y)
equalWithin :: (Real a, Show a) => a -> a
-> a
-> Assertion
equalWithin x y delta = assertBool
((x delta <= y) && (x + delta >= y))
("equalWithin: " ++ show x ++ " is not within " ++ show delta ++ " of " ++ show y)
just :: Maybe a -> Assertion
just x = assertBool (isJust x) ("just: received Nothing")
nothing :: Show a => Maybe a -> Assertion
nothing x = assertBool (isNothing x) ("nothing: received " ++ showsPrec 11 x "")
left :: Show b => Either a b -> Assertion
left (Left _) = assertionPassed
left (Right b) = assertionFailed ("left: received " ++ showsPrec 11 dummy "") where
dummy = Right b `asTypeOf` Left ()
right :: Show a => Either a b -> Assertion
right (Right _) = assertionPassed
right (Left a) = assertionFailed ("right: received " ++ showsPrec 11 dummy "") where
dummy = Left a `asTypeOf` Right ()
throws :: Exception err => (err -> Bool) -> IO a -> IO Assertion
throws p io = do
either_exc <- Control.Exception.try io
return $ case either_exc of
Left exc -> if p exc
then assertionPassed
else assertionFailed ("throws: exception " ++ show exc ++ " did not match predicate")
Right _ -> assertionFailed "throws: no exception thrown"
throwsEq :: (Eq err, Exception err, Show err) => err -> IO a -> IO Assertion
throwsEq expected io = do
either_exc <- Control.Exception.try io
return $ case either_exc of
Left exc -> if exc == expected
then assertionPassed
else assertionFailed ("throwsEq: exception " ++ show exc ++ " is not equal to " ++ show expected)
Right _ -> assertionFailed "throwsEq: no exception thrown"
greater :: (Ord a, Show a) => a -> a -> Assertion
greater x y = assertBool (x > y) ("greater: " ++ show x ++ " is not greater than " ++ show y)
greaterEqual :: (Ord a, Show a) => a -> a -> Assertion
greaterEqual x y = assertBool (x >= y) ("greaterEqual: " ++ show x ++ " is not greater than or equal to " ++ show y)
lesser :: (Ord a, Show a) => a -> a -> Assertion
lesser x y = assertBool (x < y) ("lesser: " ++ show x ++ " is not less than " ++ show y)
lesserEqual :: (Ord a, Show a) => a -> a -> Assertion
lesserEqual x y = assertBool (x <= y) ("lesserEqual: " ++ show x ++ " is not less than or equal to " ++ show y)
sameItems :: (Foldable container, Show item, Ord item) => container item -> container item -> Assertion
sameItems x y = equalDiff' "sameItems" sort x y
equalItems :: (Foldable container, Show item, Ord item) => container item -> container item -> Assertion
equalItems x y = equalDiff' "equalItems" id x y
equalDiff' :: (Foldable container, Show item, Ord item)
=> String
-> ([item]
-> [item])
-> container item
-> container item
-> Assertion
equalDiff' label norm x y = checkDiff (items x) (items y) where
items = norm . foldMap (:[])
checkDiff xs ys = case checkItems (Patience.diff xs ys) of
(same, diff) -> assertBool same diff
checkItems diffItems = case foldl' checkItem (True, []) diffItems of
(same, diff) -> (same, errorMsg (intercalate "\n" (reverse diff)))
checkItem (same, acc) item = case item of
Patience.Old t -> (False, ("\t- " ++ show t) : acc)
Patience.New t -> (False, ("\t+ " ++ show t) : acc)
Patience.Both t _-> (same, ("\t " ++ show t) : acc)
errorMsg diff = label ++ ": items differ\n" ++ diff
class IsText a where
toLines :: a -> [a]
unpack :: a -> String
instance IsText String where
toLines = lines
unpack = id
instance IsText Text where
toLines = Data.Text.lines
unpack = Data.Text.unpack
instance IsText Data.Text.Lazy.Text where
toLines = Data.Text.Lazy.lines
unpack = Data.Text.Lazy.unpack
instance IsText Data.ByteString.Char8.ByteString where
toLines = Data.ByteString.Char8.lines
unpack = Data.ByteString.Char8.unpack
instance IsText Data.ByteString.Lazy.Char8.ByteString where
toLines = Data.ByteString.Lazy.Char8.lines
unpack = Data.ByteString.Lazy.Char8.unpack
equalLines :: (Ord a, IsText a) => a -> a -> Assertion
equalLines x y = checkLinesDiff "equalLines" (toLines x) (toLines y)
equalLinesWith :: Ord a => (a -> [String]) -> a -> a -> Assertion
equalLinesWith toStringLines x y = checkLinesDiff "equalLinesWith" (toStringLines x) (toStringLines y)
checkLinesDiff :: (Ord a, IsText a) => String -> [a] -> [a] -> Assertion
checkLinesDiff label = go where
go xs ys = case checkItems (Patience.diff xs ys) of
(same, diff) -> assertBool same diff
checkItems diffItems = case foldl' checkItem (True, []) diffItems of
(same, diff) -> (same, errorMsg (intercalate "\n" (reverse diff)))
checkItem (same, acc) item = case item of
Patience.Old t -> (False, ("\t- " ++ unpack t) : acc)
Patience.New t -> (False, ("\t+ " ++ unpack t) : acc)
Patience.Both t _-> (same, ("\t " ++ unpack t) : acc)
errorMsg diff = label ++ ": lines differ\n" ++ diff