-- |
-- maintainer: Simon Hengel <sol@typeful.net>
module Test.Hspec.Contrib.HUnit (
-- * Interoperability with HUnit
  fromHUnitTest
, specListFromHUnitTest
) where

import           Test.Hspec.Core.Spec
import           Test.HUnit (Test (..))

-- |
-- Convert a HUnit test suite to a spec.  This can be used to run existing
-- HUnit tests with Hspec.
fromHUnitTest :: Test -> Spec
fromHUnitTest :: Test -> Spec
fromHUnitTest = [SpecTree ()] -> Spec
forall a. [SpecTree a] -> SpecWith a
fromSpecList ([SpecTree ()] -> Spec) -> (Test -> [SpecTree ()]) -> Test -> Spec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Test -> [SpecTree ()]
specListFromHUnitTest

-- |
-- @specListFromHUnitTest@ is similar to `fromHUnitTest`, but it constructs a
-- list of `SpecTree`s instead of a `Spec`.
specListFromHUnitTest :: Test -> [SpecTree ()]
specListFromHUnitTest :: Test -> [SpecTree ()]
specListFromHUnitTest t :: Test
t = case Test
t of
  TestList xs :: [Test]
xs -> (Test -> SpecTree ()) -> [Test] -> [SpecTree ()]
forall a b. (a -> b) -> [a] -> [b]
map Test -> SpecTree ()
go [Test]
xs
  x :: Test
x -> [Test -> SpecTree ()
go Test
x]
  where
    go :: Test -> SpecTree ()
    go :: Test -> SpecTree ()
go t_ :: Test
t_ = case Test
t_ of
      TestLabel s :: String
s (TestCase e :: Assertion
e) -> String -> Assertion -> SpecTree (Arg Assertion)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecTree (Arg a)
specItem String
s Assertion
e
      TestLabel s :: String
s (TestList xs :: [Test]
xs) -> String -> [SpecTree ()] -> SpecTree ()
forall a. HasCallStack => String -> [SpecTree a] -> SpecTree a
specGroup String
s ((Test -> SpecTree ()) -> [Test] -> [SpecTree ()]
forall a b. (a -> b) -> [a] -> [b]
map Test -> SpecTree ()
go [Test]
xs)
      TestLabel s :: String
s x :: Test
x -> String -> [SpecTree ()] -> SpecTree ()
forall a. HasCallStack => String -> [SpecTree a] -> SpecTree a
specGroup String
s [Test -> SpecTree ()
go Test
x]
      TestList xs :: [Test]
xs -> String -> [SpecTree ()] -> SpecTree ()
forall a. HasCallStack => String -> [SpecTree a] -> SpecTree a
specGroup "<unlabeled>" ((Test -> SpecTree ()) -> [Test] -> [SpecTree ()]
forall a b. (a -> b) -> [a] -> [b]
map Test -> SpecTree ()
go [Test]
xs)
      TestCase e :: Assertion
e -> String -> Assertion -> SpecTree (Arg Assertion)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecTree (Arg a)
specItem "<unlabeled>" Assertion
e