{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}

module Distribution.Types.TestSuite (
    TestSuite(..),
    emptyTestSuite,
    testType,
    testModules,
    testModulesAutogen
) where

import Prelude ()
import Distribution.Compat.Prelude

import Distribution.Types.BuildInfo
import Distribution.Types.TestType
import Distribution.Types.TestSuiteInterface
import Distribution.Types.UnqualComponentName

import Distribution.ModuleName

import qualified Distribution.Types.BuildInfo.Lens as L

-- | A \"test-suite\" stanza in a cabal file.
--
data TestSuite = TestSuite {
        TestSuite -> UnqualComponentName
testName      :: UnqualComponentName,
        TestSuite -> TestSuiteInterface
testInterface :: TestSuiteInterface,
        TestSuite -> BuildInfo
testBuildInfo :: BuildInfo
    }
    deriving ((forall x. TestSuite -> Rep TestSuite x)
-> (forall x. Rep TestSuite x -> TestSuite) -> Generic TestSuite
forall x. Rep TestSuite x -> TestSuite
forall x. TestSuite -> Rep TestSuite x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TestSuite x -> TestSuite
$cfrom :: forall x. TestSuite -> Rep TestSuite x
Generic, Int -> TestSuite -> ShowS
[TestSuite] -> ShowS
TestSuite -> String
(Int -> TestSuite -> ShowS)
-> (TestSuite -> String)
-> ([TestSuite] -> ShowS)
-> Show TestSuite
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TestSuite] -> ShowS
$cshowList :: [TestSuite] -> ShowS
show :: TestSuite -> String
$cshow :: TestSuite -> String
showsPrec :: Int -> TestSuite -> ShowS
$cshowsPrec :: Int -> TestSuite -> ShowS
Show, ReadPrec [TestSuite]
ReadPrec TestSuite
Int -> ReadS TestSuite
ReadS [TestSuite]
(Int -> ReadS TestSuite)
-> ReadS [TestSuite]
-> ReadPrec TestSuite
-> ReadPrec [TestSuite]
-> Read TestSuite
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TestSuite]
$creadListPrec :: ReadPrec [TestSuite]
readPrec :: ReadPrec TestSuite
$creadPrec :: ReadPrec TestSuite
readList :: ReadS [TestSuite]
$creadList :: ReadS [TestSuite]
readsPrec :: Int -> ReadS TestSuite
$creadsPrec :: Int -> ReadS TestSuite
Read, TestSuite -> TestSuite -> Bool
(TestSuite -> TestSuite -> Bool)
-> (TestSuite -> TestSuite -> Bool) -> Eq TestSuite
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TestSuite -> TestSuite -> Bool
$c/= :: TestSuite -> TestSuite -> Bool
== :: TestSuite -> TestSuite -> Bool
$c== :: TestSuite -> TestSuite -> Bool
Eq, Typeable, Typeable TestSuite
DataType
Constr
Typeable TestSuite
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> TestSuite -> c TestSuite)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c TestSuite)
-> (TestSuite -> Constr)
-> (TestSuite -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c TestSuite))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TestSuite))
-> ((forall b. Data b => b -> b) -> TestSuite -> TestSuite)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> TestSuite -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> TestSuite -> r)
-> (forall u. (forall d. Data d => d -> u) -> TestSuite -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> TestSuite -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> TestSuite -> m TestSuite)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TestSuite -> m TestSuite)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> TestSuite -> m TestSuite)
-> Data TestSuite
TestSuite -> DataType
TestSuite -> Constr
(forall b. Data b => b -> b) -> TestSuite -> TestSuite
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TestSuite -> c TestSuite
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TestSuite
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> TestSuite -> u
forall u. (forall d. Data d => d -> u) -> TestSuite -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TestSuite -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TestSuite -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TestSuite
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TestSuite -> c TestSuite
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TestSuite)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TestSuite)
$cTestSuite :: Constr
$tTestSuite :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
gmapMp :: (forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
gmapM :: (forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> TestSuite -> m TestSuite
gmapQi :: Int -> (forall d. Data d => d -> u) -> TestSuite -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> TestSuite -> u
gmapQ :: (forall d. Data d => d -> u) -> TestSuite -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> TestSuite -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TestSuite -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> TestSuite -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TestSuite -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> TestSuite -> r
gmapT :: (forall b. Data b => b -> b) -> TestSuite -> TestSuite
$cgmapT :: (forall b. Data b => b -> b) -> TestSuite -> TestSuite
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TestSuite)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c TestSuite)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c TestSuite)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c TestSuite)
dataTypeOf :: TestSuite -> DataType
$cdataTypeOf :: TestSuite -> DataType
toConstr :: TestSuite -> Constr
$ctoConstr :: TestSuite -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TestSuite
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c TestSuite
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TestSuite -> c TestSuite
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> TestSuite -> c TestSuite
$cp1Data :: Typeable TestSuite
Data)

instance L.HasBuildInfo TestSuite where
    buildInfo :: LensLike f TestSuite TestSuite BuildInfo BuildInfo
buildInfo BuildInfo -> f BuildInfo
f TestSuite
l = (\BuildInfo
x -> TestSuite
l { testBuildInfo :: BuildInfo
testBuildInfo = BuildInfo
x }) (BuildInfo -> TestSuite) -> f BuildInfo -> f TestSuite
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BuildInfo -> f BuildInfo
f (TestSuite -> BuildInfo
testBuildInfo TestSuite
l)

instance Binary TestSuite
instance Structured TestSuite

instance NFData TestSuite where rnf :: TestSuite -> ()
rnf = TestSuite -> ()
forall a. (Generic a, GNFData (Rep a)) => a -> ()
genericRnf

instance Monoid TestSuite where
    mempty :: TestSuite
mempty = TestSuite :: UnqualComponentName -> TestSuiteInterface -> BuildInfo -> TestSuite
TestSuite {
        testName :: UnqualComponentName
testName      = UnqualComponentName
forall a. Monoid a => a
mempty,
        testInterface :: TestSuiteInterface
testInterface = TestSuiteInterface
forall a. Monoid a => a
mempty,
        testBuildInfo :: BuildInfo
testBuildInfo = BuildInfo
forall a. Monoid a => a
mempty
    }
    mappend :: TestSuite -> TestSuite -> TestSuite
mappend = TestSuite -> TestSuite -> TestSuite
forall a. Semigroup a => a -> a -> a
(<>)

instance Semigroup TestSuite where
    TestSuite
a <> :: TestSuite -> TestSuite -> TestSuite
<> TestSuite
b = TestSuite :: UnqualComponentName -> TestSuiteInterface -> BuildInfo -> TestSuite
TestSuite {
        testName :: UnqualComponentName
testName      = (TestSuite -> UnqualComponentName) -> UnqualComponentName
combine' TestSuite -> UnqualComponentName
testName,
        testInterface :: TestSuiteInterface
testInterface = (TestSuite -> TestSuiteInterface) -> TestSuiteInterface
forall a. Monoid a => (TestSuite -> a) -> a
combine  TestSuite -> TestSuiteInterface
testInterface,
        testBuildInfo :: BuildInfo
testBuildInfo = (TestSuite -> BuildInfo) -> BuildInfo
forall a. Monoid a => (TestSuite -> a) -> a
combine  TestSuite -> BuildInfo
testBuildInfo
    }
        where combine :: (TestSuite -> a) -> a
combine  TestSuite -> a
field = TestSuite -> a
field TestSuite
a a -> a -> a
forall a. Monoid a => a -> a -> a
`mappend` TestSuite -> a
field TestSuite
b
              combine' :: (TestSuite -> UnqualComponentName) -> UnqualComponentName
combine' TestSuite -> UnqualComponentName
field = case ( UnqualComponentName -> String
unUnqualComponentName (UnqualComponentName -> String) -> UnqualComponentName -> String
forall a b. (a -> b) -> a -> b
$ TestSuite -> UnqualComponentName
field TestSuite
a
                                    , UnqualComponentName -> String
unUnqualComponentName (UnqualComponentName -> String) -> UnqualComponentName -> String
forall a b. (a -> b) -> a -> b
$ TestSuite -> UnqualComponentName
field TestSuite
b) of
                        (String
"", String
_) -> TestSuite -> UnqualComponentName
field TestSuite
b
                        (String
_, String
"") -> TestSuite -> UnqualComponentName
field TestSuite
a
                        (String
x, String
y) -> String -> UnqualComponentName
forall a. HasCallStack => String -> a
error (String -> UnqualComponentName) -> String -> UnqualComponentName
forall a b. (a -> b) -> a -> b
$ String
"Ambiguous values for test field: '"
                            String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"' and '" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
y String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"'"

emptyTestSuite :: TestSuite
emptyTestSuite :: TestSuite
emptyTestSuite = TestSuite
forall a. Monoid a => a
mempty


testType :: TestSuite -> TestType
testType :: TestSuite -> TestType
testType TestSuite
test = case TestSuite -> TestSuiteInterface
testInterface TestSuite
test of
  TestSuiteExeV10 Version
ver String
_         -> Version -> TestType
TestTypeExe Version
ver
  TestSuiteLibV09 Version
ver ModuleName
_         -> Version -> TestType
TestTypeLib Version
ver
  TestSuiteUnsupported TestType
testtype -> TestType
testtype

-- | Get all the module names from a test suite.
testModules :: TestSuite -> [ModuleName]
testModules :: TestSuite -> [ModuleName]
testModules TestSuite
test = (case TestSuite -> TestSuiteInterface
testInterface TestSuite
test of
                     TestSuiteLibV09 Version
_ ModuleName
m -> [ModuleName
m]
                     TestSuiteInterface
_                   -> [])
                [ModuleName] -> [ModuleName] -> [ModuleName]
forall a. [a] -> [a] -> [a]
++ BuildInfo -> [ModuleName]
otherModules (TestSuite -> BuildInfo
testBuildInfo TestSuite
test)

-- | Get all the auto generated module names from a test suite.
-- This are a subset of 'testModules'.
testModulesAutogen :: TestSuite -> [ModuleName]
testModulesAutogen :: TestSuite -> [ModuleName]
testModulesAutogen TestSuite
test = BuildInfo -> [ModuleName]
autogenModules (TestSuite -> BuildInfo
testBuildInfo TestSuite
test)