-- | A module containing functions for creating and managing tests.
module Test
  ( -- * Organizing Tests
    Internal.Test,
    Internal.test,
    Internal.describe,
    Internal.skip,
    Internal.only,
    Internal.todo,

    -- * Fuzz Testing
    Internal.fuzz,
    Internal.fuzz2,
    Internal.fuzz3,

    -- * Task Testing
    Internal.task,

    -- * Running test
    run,
  )
where

import NriPrelude
import qualified Platform
import qualified System.Environment
import qualified System.FilePath as FilePath
import qualified System.IO
import qualified Task
import qualified Test.Internal as Internal
import qualified Test.Reporter.ExitCode
import qualified Test.Reporter.Junit
import qualified Test.Reporter.Stdout
import qualified Prelude

-- | Turn a test suite into a program that can be executed in Haskell. Use like
-- this:
--
-- > module Main (main) where
-- >
-- > import qualified Test
-- >
-- > main :: IO ()
-- > main = Test.run (Test.todo "write your tests here!")
run :: Internal.Test -> Prelude.IO ()
run :: Test -> IO ()
run Test
suite = do
  LogHandler
log <- IO LogHandler
Platform.silentHandler
  SuiteResult
results <- LogHandler -> Task Never SuiteResult -> IO SuiteResult
forall a. LogHandler -> Task Never a -> IO a
Task.perform LogHandler
log (Test -> Task Never SuiteResult
forall e. Test -> Task e SuiteResult
Internal.run Test
suite)
  Handle -> SuiteResult -> IO ()
Test.Reporter.Stdout.report Handle
System.IO.stdout SuiteResult
results
  [String]
args <- IO [String]
System.Environment.getArgs
  case [String] -> Maybe String
getPath [String]
args of
    Maybe String
Nothing -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure ()
    Just String
path -> String -> SuiteResult -> IO ()
Test.Reporter.Junit.report String
path SuiteResult
results
  SuiteResult -> IO ()
Test.Reporter.ExitCode.report SuiteResult
results

getPath :: [Prelude.String] -> Maybe FilePath.FilePath
getPath :: [String] -> Maybe String
getPath [String]
args =
  case [String]
args of
    [] -> Maybe String
forall a. Maybe a
Nothing
    String
"--xml" : String
path : [String]
_ -> String -> Maybe String
forall a. a -> Maybe a
Just String
path
    String
_ : [String]
rest -> [String] -> Maybe String
getPath [String]
rest