module Test.Hspec.Internal (
Spec (..)
, Specs
, Example (..)
, safeEvaluateExample
, Result (..)
, describe
, it
, quantify
)
where
import Control.Exception
type Specs = [Spec]
data Result = Success | Pending (Maybe String) | Fail String
deriving (Eq, Show)
data Spec = SpecGroup String [Spec]
| SpecExample String (IO Result)
describe :: String -> [Spec] -> Spec
describe = SpecGroup
safeEvaluateExample :: IO Result -> IO Result
safeEvaluateExample action = do
action `catches` [
Handler (\e -> throw (e :: AsyncException)),
Handler (\e -> return $ Fail (show (e :: SomeException)))
]
it :: Example a => String -> a -> Spec
it requirement = SpecExample requirement . evaluateExample
class Example a where
evaluateExample :: a -> IO Result
instance Example Bool where
evaluateExample b = if b then return Success else return (Fail "")
instance Example Result where
evaluateExample r = r `seq` return r
quantify :: Int -> String -> String
quantify 1 s = "1 " ++ s
quantify n s = show n ++ " " ++ s ++ "s"