{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | This module defines functions for declaring different test settings
module Test.Syd.Modify
  ( -- * Declaring different test settings
    modifyMaxSuccess,
    modifyMaxDiscardRatio,
    modifyMaxSize,
    modifyMaxShrinks,
    modifyRunSettings,
    TestRunSettings (..),

    -- * Declaring parallelism
    sequential,
    parallel,
    withParallelism,
    Parallelism (..),

    -- * Declaring randomisation order
    randomiseExecutionOrder,
    doNotRandomiseExecutionOrder,
    withExecutionOrderRandomisation,
    ExecutionOrderRandomisation (..),

    -- * Declaring flakiness
    flaky,
    flakyWith,
    notFlaky,
    withFlakiness,
    FlakinessMode (..),
  )
where

import Control.Monad.RWS.Strict
import Test.QuickCheck.IO ()
import Test.Syd.Def
import Test.Syd.Run
import Test.Syd.SpecDef

modifyRunSettings :: (TestRunSettings -> TestRunSettings) -> TestDefM a b c -> TestDefM a b c
modifyRunSettings :: (TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
modifyRunSettings TestRunSettings -> TestRunSettings
func = (TestDefEnv -> TestDefEnv) -> TestDefM a b c -> TestDefM a b c
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\TestDefEnv
tde -> TestDefEnv
tde {testDefEnvTestRunSettings :: TestRunSettings
testDefEnvTestRunSettings = TestRunSettings -> TestRunSettings
func (TestRunSettings -> TestRunSettings)
-> TestRunSettings -> TestRunSettings
forall a b. (a -> b) -> a -> b
$ TestDefEnv -> TestRunSettings
testDefEnvTestRunSettings TestDefEnv
tde})

modifyMaxSuccess :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxSuccess :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxSuccess Int -> Int
func = (TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
(TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
modifyRunSettings ((TestRunSettings -> TestRunSettings)
 -> TestDefM a b c -> TestDefM a b c)
-> (TestRunSettings -> TestRunSettings)
-> TestDefM a b c
-> TestDefM a b c
forall a b. (a -> b) -> a -> b
$ \TestRunSettings
trs -> TestRunSettings
trs {testRunSettingMaxSuccess :: Int
testRunSettingMaxSuccess = Int -> Int
func (TestRunSettings -> Int
testRunSettingMaxSuccess TestRunSettings
trs)}

modifyMaxDiscardRatio :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxDiscardRatio :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxDiscardRatio Int -> Int
func = (TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
(TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
modifyRunSettings ((TestRunSettings -> TestRunSettings)
 -> TestDefM a b c -> TestDefM a b c)
-> (TestRunSettings -> TestRunSettings)
-> TestDefM a b c
-> TestDefM a b c
forall a b. (a -> b) -> a -> b
$ \TestRunSettings
trs -> TestRunSettings
trs {testRunSettingMaxDiscardRatio :: Int
testRunSettingMaxDiscardRatio = Int -> Int
func (TestRunSettings -> Int
testRunSettingMaxDiscardRatio TestRunSettings
trs)}

modifyMaxSize :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxSize :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxSize Int -> Int
func = (TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
(TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
modifyRunSettings ((TestRunSettings -> TestRunSettings)
 -> TestDefM a b c -> TestDefM a b c)
-> (TestRunSettings -> TestRunSettings)
-> TestDefM a b c
-> TestDefM a b c
forall a b. (a -> b) -> a -> b
$ \TestRunSettings
trs -> TestRunSettings
trs {testRunSettingMaxSize :: Int
testRunSettingMaxSize = Int -> Int
func (TestRunSettings -> Int
testRunSettingMaxSize TestRunSettings
trs)}

modifyMaxShrinks :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxShrinks :: (Int -> Int) -> TestDefM a b c -> TestDefM a b c
modifyMaxShrinks Int -> Int
func = (TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
(TestRunSettings -> TestRunSettings)
-> TestDefM a b c -> TestDefM a b c
modifyRunSettings ((TestRunSettings -> TestRunSettings)
 -> TestDefM a b c -> TestDefM a b c)
-> (TestRunSettings -> TestRunSettings)
-> TestDefM a b c
-> TestDefM a b c
forall a b. (a -> b) -> a -> b
$ \TestRunSettings
trs -> TestRunSettings
trs {testRunSettingMaxShrinks :: Int
testRunSettingMaxShrinks = Int -> Int
func (TestRunSettings -> Int
testRunSettingMaxShrinks TestRunSettings
trs)}

-- | Declare that all tests below must be run sequentially
sequential :: TestDefM a b c -> TestDefM a b c
sequential :: TestDefM a b c -> TestDefM a b c
sequential = Parallelism -> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
Parallelism -> TestDefM a b c -> TestDefM a b c
withParallelism Parallelism
Sequential

-- | Declare that all tests below may be run in parallel. (This is the default.)
parallel :: TestDefM a b c -> TestDefM a b c
parallel :: TestDefM a b c -> TestDefM a b c
parallel = Parallelism -> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
Parallelism -> TestDefM a b c -> TestDefM a b c
withParallelism Parallelism
Parallel

-- | Annotate a test group with 'Parallelism'.
withParallelism :: Parallelism -> TestDefM a b c -> TestDefM a b c
withParallelism :: Parallelism -> TestDefM a b c -> TestDefM a b c
withParallelism Parallelism
p = ([SpecDefTree a b ()] -> [SpecDefTree a b ()])
-> TestDefM a b c -> TestDefM a b c
forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor ((SpecDefTree a b () -> [SpecDefTree a b ()] -> [SpecDefTree a b ()]
forall a. a -> [a] -> [a]
: []) (SpecDefTree a b () -> [SpecDefTree a b ()])
-> ([SpecDefTree a b ()] -> SpecDefTree a b ())
-> [SpecDefTree a b ()]
-> [SpecDefTree a b ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parallelism -> [SpecDefTree a b ()] -> SpecDefTree a b ()
forall (outers :: [*]) inner extra.
Parallelism
-> SpecDefForest outers inner extra
-> SpecDefTree outers inner extra
DefParallelismNode Parallelism
p)

-- | Declare that the order of execution of all tests below must not be randomised.
doNotRandomiseExecutionOrder :: TestDefM a b c -> TestDefM a b c
doNotRandomiseExecutionOrder :: TestDefM a b c -> TestDefM a b c
doNotRandomiseExecutionOrder = ExecutionOrderRandomisation -> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
ExecutionOrderRandomisation -> TestDefM a b c -> TestDefM a b c
withExecutionOrderRandomisation ExecutionOrderRandomisation
DoNotRandomiseExecutionOrder

-- | Declare that the order of execution of all tests below may be randomised.
randomiseExecutionOrder :: TestDefM a b c -> TestDefM a b c
randomiseExecutionOrder :: TestDefM a b c -> TestDefM a b c
randomiseExecutionOrder = ExecutionOrderRandomisation -> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
ExecutionOrderRandomisation -> TestDefM a b c -> TestDefM a b c
withExecutionOrderRandomisation ExecutionOrderRandomisation
RandomiseExecutionOrder

-- | Annotate a test group with 'ExecutionOrderRandomisation'.
withExecutionOrderRandomisation :: ExecutionOrderRandomisation -> TestDefM a b c -> TestDefM a b c
withExecutionOrderRandomisation :: ExecutionOrderRandomisation -> TestDefM a b c -> TestDefM a b c
withExecutionOrderRandomisation ExecutionOrderRandomisation
p = ([SpecDefTree a b ()] -> [SpecDefTree a b ()])
-> TestDefM a b c -> TestDefM a b c
forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor ((SpecDefTree a b () -> [SpecDefTree a b ()] -> [SpecDefTree a b ()]
forall a. a -> [a] -> [a]
: []) (SpecDefTree a b () -> [SpecDefTree a b ()])
-> ([SpecDefTree a b ()] -> SpecDefTree a b ())
-> [SpecDefTree a b ()]
-> [SpecDefTree a b ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExecutionOrderRandomisation
-> [SpecDefTree a b ()] -> SpecDefTree a b ()
forall (outers :: [*]) inner extra.
ExecutionOrderRandomisation
-> SpecDefForest outers inner extra
-> SpecDefTree outers inner extra
DefRandomisationNode ExecutionOrderRandomisation
p)

-- | Mark a test suite as "potentially flaky".
--
-- This will retry any test in the given test group up to the given number of tries, and pass a test if it passes once.
-- The test output will show which tests were flaky.
--
-- WARNING: This is only a valid approach to dealing with test flakiness if it is true that tests never pass accidentally.
-- In other words: tests using flaky must be guaranteed to fail every time if
-- an error is introduced in the code, it should only be added to deal with
-- accidental failures, never accidental passes.
flaky :: Int -> TestDefM a b c -> TestDefM a b c
flaky :: Int -> TestDefM a b c -> TestDefM a b c
flaky Int
i = FlakinessMode -> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
FlakinessMode -> TestDefM a b c -> TestDefM a b c
withFlakiness (FlakinessMode -> TestDefM a b c -> TestDefM a b c)
-> FlakinessMode -> TestDefM a b c -> TestDefM a b c
forall a b. (a -> b) -> a -> b
$ Int -> Maybe String -> FlakinessMode
MayBeFlakyUpTo Int
i Maybe String
forall a. Maybe a
Nothing

-- | Like 'flaky', but also shows the given message to the user whenever the test is flaky.
--
-- You could use it like this:
--
-- >>> flakyWith 3 "Something sometimes goes wrong with the database, see issue 6346" ourTestSuite
flakyWith :: Int -> String -> TestDefM a b c -> TestDefM a b c
flakyWith :: Int -> String -> TestDefM a b c -> TestDefM a b c
flakyWith Int
i String
message = FlakinessMode -> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
FlakinessMode -> TestDefM a b c -> TestDefM a b c
withFlakiness (FlakinessMode -> TestDefM a b c -> TestDefM a b c)
-> FlakinessMode -> TestDefM a b c -> TestDefM a b c
forall a b. (a -> b) -> a -> b
$ Int -> Maybe String -> FlakinessMode
MayBeFlakyUpTo Int
i (String -> Maybe String
forall a. a -> Maybe a
Just String
message)

-- | Mark a test suite as "must not be flaky".
--
-- This is useful to have a subgroup of a group marked as 'flaky' that must not be flaky afteral.
notFlaky :: TestDefM a b c -> TestDefM a b c
notFlaky :: TestDefM a b c -> TestDefM a b c
notFlaky = FlakinessMode -> TestDefM a b c -> TestDefM a b c
forall (a :: [*]) b c.
FlakinessMode -> TestDefM a b c -> TestDefM a b c
withFlakiness FlakinessMode
MayNotBeFlaky

-- | Annotate a test group with 'FlakinessMode'.
withFlakiness :: FlakinessMode -> TestDefM a b c -> TestDefM a b c
withFlakiness :: FlakinessMode -> TestDefM a b c -> TestDefM a b c
withFlakiness FlakinessMode
f = ([SpecDefTree a b ()] -> [SpecDefTree a b ()])
-> TestDefM a b c -> TestDefM a b c
forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor ((SpecDefTree a b () -> [SpecDefTree a b ()] -> [SpecDefTree a b ()]
forall a. a -> [a] -> [a]
: []) (SpecDefTree a b () -> [SpecDefTree a b ()])
-> ([SpecDefTree a b ()] -> SpecDefTree a b ())
-> [SpecDefTree a b ()]
-> [SpecDefTree a b ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FlakinessMode -> [SpecDefTree a b ()] -> SpecDefTree a b ()
forall (outers :: [*]) inner extra.
FlakinessMode
-> SpecDefForest outers inner extra
-> SpecDefTree outers inner extra
DefFlakinessNode FlakinessMode
f)