{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ConstraintKinds #-}
module Test.Hspec.Core.Tree (
SpecTree
, Tree (..)
, Item (..)
, specGroup
, specItem
, location
) where
import Prelude ()
import Test.Hspec.Core.Compat
import Data.CallStack
import Data.Maybe
import Test.Hspec.Core.Example
data Tree c a =
Node String [Tree c a]
| NodeWithCleanup c [Tree c a]
| Leaf a
deriving (Functor, Foldable, Traversable)
type SpecTree a = Tree (ActionWith a) (Item a)
data Item a = Item {
itemRequirement :: String
, itemLocation :: Maybe Location
, itemIsParallelizable :: Maybe Bool
, itemExample :: Params -> (ActionWith a -> IO ()) -> ProgressCallback -> IO Result
}
specGroup :: HasCallStack => String -> [SpecTree a] -> SpecTree a
specGroup s = Node msg
where
msg :: HasCallStack => String
msg
| null s = fromMaybe "(no description given)" defaultDescription
| otherwise = s
specItem :: (HasCallStack, Example a) => String -> a -> SpecTree (Arg a)
specItem s e = Leaf $ Item requirement location Nothing (safeEvaluateExample e)
where
requirement :: HasCallStack => String
requirement
| null s = fromMaybe "(unspecified behavior)" defaultDescription
| otherwise = s
location :: HasCallStack => Maybe Location
location = case reverse callStack of
(_, loc) : _ -> Just (Location (srcLocFile loc) (srcLocStartLine loc) (srcLocStartCol loc))
_ -> Nothing
defaultDescription :: HasCallStack => Maybe String
defaultDescription = case reverse callStack of
(_, loc) : _ -> Just (srcLocModule loc ++ "[" ++ show (srcLocStartLine loc) ++ ":" ++ show (srcLocStartCol loc) ++ "]")
_ -> Nothing