{-# LANGUAGE CPP #-}
module Test.Hspec.Core.Runner.Result (
-- RE-EXPORTED from Test.Hspec.Core.Runner
  SpecResult(SpecResult)
, specResultItems
, specResultSuccess

, ResultItem(ResultItem)
, resultItemPath
, resultItemStatus
, resultItemIsFailure

, ResultItemStatus(..)

, Summary(..)
, toSummary
, isSuccess
-- END RE-EXPORTED from Test.Hspec.Core.Runner

, toSpecResult
) where

import           Prelude ()
import           Test.Hspec.Core.Compat

import           Test.Hspec.Core.Util
import qualified Test.Hspec.Core.Format as Format

-- |
-- @since 2.10.0
data SpecResult = SpecResult {
  -- |
  -- @since 2.10.0
  SpecResult -> [ResultItem]
specResultItems :: [ResultItem]

  -- |
  -- @since 2.10.0
, SpecResult -> Bool
specResultSuccess :: !Bool
} deriving (SpecResult -> SpecResult -> Bool
(SpecResult -> SpecResult -> Bool)
-> (SpecResult -> SpecResult -> Bool) -> Eq SpecResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SpecResult -> SpecResult -> Bool
== :: SpecResult -> SpecResult -> Bool
$c/= :: SpecResult -> SpecResult -> Bool
/= :: SpecResult -> SpecResult -> Bool
Eq, Int -> SpecResult -> ShowS
[SpecResult] -> ShowS
SpecResult -> String
(Int -> SpecResult -> ShowS)
-> (SpecResult -> String)
-> ([SpecResult] -> ShowS)
-> Show SpecResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SpecResult -> ShowS
showsPrec :: Int -> SpecResult -> ShowS
$cshow :: SpecResult -> String
show :: SpecResult -> String
$cshowList :: [SpecResult] -> ShowS
showList :: [SpecResult] -> ShowS
Show)

-- |
-- @since 2.10.0
data ResultItem = ResultItem {
  -- |
  -- @since 2.10.0
  ResultItem -> Path
resultItemPath :: Path

  -- |
  -- @since 2.10.0
, ResultItem -> ResultItemStatus
resultItemStatus :: ResultItemStatus
} deriving (ResultItem -> ResultItem -> Bool
(ResultItem -> ResultItem -> Bool)
-> (ResultItem -> ResultItem -> Bool) -> Eq ResultItem
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ResultItem -> ResultItem -> Bool
== :: ResultItem -> ResultItem -> Bool
$c/= :: ResultItem -> ResultItem -> Bool
/= :: ResultItem -> ResultItem -> Bool
Eq, Int -> ResultItem -> ShowS
[ResultItem] -> ShowS
ResultItem -> String
(Int -> ResultItem -> ShowS)
-> (ResultItem -> String)
-> ([ResultItem] -> ShowS)
-> Show ResultItem
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ResultItem -> ShowS
showsPrec :: Int -> ResultItem -> ShowS
$cshow :: ResultItem -> String
show :: ResultItem -> String
$cshowList :: [ResultItem] -> ShowS
showList :: [ResultItem] -> ShowS
Show)

-- |
-- @since 2.10.0
resultItemIsFailure :: ResultItem -> Bool
resultItemIsFailure :: ResultItem -> Bool
resultItemIsFailure ResultItem
item = case ResultItem -> ResultItemStatus
resultItemStatus ResultItem
item of
  ResultItemStatus
ResultItemSuccess -> Bool
False
  ResultItemStatus
ResultItemPending -> Bool
False
  ResultItemStatus
ResultItemFailure -> Bool
True

data ResultItemStatus =
    ResultItemSuccess
  | ResultItemPending
  | ResultItemFailure
  deriving (ResultItemStatus -> ResultItemStatus -> Bool
(ResultItemStatus -> ResultItemStatus -> Bool)
-> (ResultItemStatus -> ResultItemStatus -> Bool)
-> Eq ResultItemStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ResultItemStatus -> ResultItemStatus -> Bool
== :: ResultItemStatus -> ResultItemStatus -> Bool
$c/= :: ResultItemStatus -> ResultItemStatus -> Bool
/= :: ResultItemStatus -> ResultItemStatus -> Bool
Eq, Int -> ResultItemStatus -> ShowS
[ResultItemStatus] -> ShowS
ResultItemStatus -> String
(Int -> ResultItemStatus -> ShowS)
-> (ResultItemStatus -> String)
-> ([ResultItemStatus] -> ShowS)
-> Show ResultItemStatus
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ResultItemStatus -> ShowS
showsPrec :: Int -> ResultItemStatus -> ShowS
$cshow :: ResultItemStatus -> String
show :: ResultItemStatus -> String
$cshowList :: [ResultItemStatus] -> ShowS
showList :: [ResultItemStatus] -> ShowS
Show)

toSpecResult :: [(Path, Format.Item)] -> SpecResult
toSpecResult :: [(Path, Item)] -> SpecResult
toSpecResult [(Path, Item)]
results = [ResultItem] -> Bool -> SpecResult
SpecResult [ResultItem]
items Bool
success
  where
    items :: [ResultItem]
items = ((Path, Item) -> ResultItem) -> [(Path, Item)] -> [ResultItem]
forall a b. (a -> b) -> [a] -> [b]
map (Path, Item) -> ResultItem
toResultItem [(Path, Item)]
results
    success :: Bool
success = (ResultItem -> Bool) -> [ResultItem] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Bool -> Bool
not (Bool -> Bool) -> (ResultItem -> Bool) -> ResultItem -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResultItem -> Bool
resultItemIsFailure) [ResultItem]
items

toResultItem :: (Path, Format.Item) -> ResultItem
toResultItem :: (Path, Item) -> ResultItem
toResultItem (Path
path, Item
item) = Path -> ResultItemStatus -> ResultItem
ResultItem Path
path ResultItemStatus
status
  where
    status :: ResultItemStatus
status = case Item -> Result
Format.itemResult Item
item of
      Format.Success{} -> ResultItemStatus
ResultItemSuccess
      Format.Pending{} -> ResultItemStatus
ResultItemPending
      Format.Failure{} -> ResultItemStatus
ResultItemFailure

-- | Summary of a test run.
data Summary = Summary {
  Summary -> Int
summaryExamples :: !Int
, Summary -> Int
summaryFailures :: !Int
} deriving (Summary -> Summary -> Bool
(Summary -> Summary -> Bool)
-> (Summary -> Summary -> Bool) -> Eq Summary
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Summary -> Summary -> Bool
== :: Summary -> Summary -> Bool
$c/= :: Summary -> Summary -> Bool
/= :: Summary -> Summary -> Bool
Eq, Int -> Summary -> ShowS
[Summary] -> ShowS
Summary -> String
(Int -> Summary -> ShowS)
-> (Summary -> String) -> ([Summary] -> ShowS) -> Show Summary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Summary -> ShowS
showsPrec :: Int -> Summary -> ShowS
$cshow :: Summary -> String
show :: Summary -> String
$cshowList :: [Summary] -> ShowS
showList :: [Summary] -> ShowS
Show)

instance Monoid Summary where
  mempty :: Summary
mempty = Int -> Int -> Summary
Summary Int
0 Int
0
#if MIN_VERSION_base(4,11,0)
instance Semigroup Summary where
#endif
  (Summary Int
x1 Int
x2)
#if MIN_VERSION_base(4,11,0)
    <> :: Summary -> Summary -> Summary
<>
#else
    `mappend`
#endif
    (Summary Int
y1 Int
y2) = Int -> Int -> Summary
Summary (Int
x1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y1) (Int
x2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y2)

toSummary :: SpecResult -> Summary
toSummary :: SpecResult -> Summary
toSummary SpecResult
result = Summary {
  summaryExamples :: Int
summaryExamples = [ResultItem] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ResultItem]
items
, summaryFailures :: Int
summaryFailures = [ResultItem] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ResultItem]
failures
} where
    items :: [ResultItem]
items = SpecResult -> [ResultItem]
specResultItems SpecResult
result
    failures :: [ResultItem]
failures = (ResultItem -> Bool) -> [ResultItem] -> [ResultItem]
forall a. (a -> Bool) -> [a] -> [a]
filter ResultItem -> Bool
resultItemIsFailure [ResultItem]
items

-- | `True` if the given `Summary` indicates that there were no
-- failures, `False` otherwise.
isSuccess :: Summary -> Bool
isSuccess :: Summary -> Bool
isSuccess Summary
summary = Summary -> Int
summaryFailures Summary
summary Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0