module Test.Tasty.ExpectedFailure (expectFail, expectFailBecause, ignoreTest, ignoreTestBecause, wrapTest) where
import Test.Tasty.Options
import Test.Tasty.Runners
import Test.Tasty.Providers
import Data.Typeable
import Data.Tagged
import Data.Maybe
import Data.Monoid
data WrappedTest t = WrappedTest (IO Result -> IO Result) t
deriving Typeable
instance forall t. IsTest t => IsTest (WrappedTest t) where
run opts (WrappedTest wrap t) prog = wrap (run opts t prog)
testOptions = retag (testOptions :: Tagged t [OptionDescription])
wrapTest :: (IO Result -> IO Result) -> TestTree -> TestTree
wrapTest wrap = go
where
go (SingleTest n t) = SingleTest n (WrappedTest wrap t)
go (TestGroup name tests) = TestGroup name (map go tests)
go (PlusTestOptions plus tree) = PlusTestOptions plus (go tree)
go (WithResource spec gentree) = WithResource spec (go . gentree)
go (AskOptions f) = AskOptions (go . f)
expectFail :: TestTree -> TestTree
expectFail = expectFail' Nothing
expectFailBecause :: String -> TestTree -> TestTree
expectFailBecause reason = expectFail' (Just reason)
expectFail' :: Maybe String -> TestTree -> TestTree
expectFail' reason = wrapTest (fmap change)
where
change r
| resultSuccessful r
= r { resultOutcome = Failure TestFailed
, resultDescription = resultDescription r <> "(unexpected success" <> comment <> ")"
, resultShortDescription = "PASS (unexpected" <> comment <> ")"
}
| otherwise
= r { resultOutcome = Success
, resultDescription = resultDescription r <> "(expected failure)"
, resultShortDescription = "FAIL (expected" <> comment <> ")"
}
"" `append` s = s
t `append` s | last t == '\n' = t ++ s ++ "\n"
| otherwise = t ++ "\n" ++ s
comment = maybe "" (mappend ": ") reason
ignoreTest :: TestTree -> TestTree
ignoreTest = ignoreTest' Nothing
ignoreTestBecause :: String -> TestTree -> TestTree
ignoreTestBecause reason = ignoreTest' (Just reason)
ignoreTest' :: Maybe String -> TestTree -> TestTree
ignoreTest' reason = wrapTest $ const $ return $
(testPassed "") {
resultShortDescription = "IGNORED" <> maybe "" (mappend ": ") reason
}