|
Packit |
755117 |
{-# LANGUAGE RecordWildCards #-}
|
|
Packit |
755117 |
import Test.Tasty
|
|
Packit |
755117 |
import Test.Tasty.Options
|
|
Packit |
755117 |
import Test.Tasty.Providers as Tasty
|
|
Packit |
755117 |
import Test.Tasty.Runners as Tasty
|
|
Packit |
755117 |
import Test.Tasty.QuickCheck
|
|
Packit |
755117 |
import Test.Tasty.HUnit
|
|
Packit |
755117 |
import Data.Monoid
|
|
Packit |
755117 |
import Data.Maybe
|
|
Packit |
755117 |
import Text.Regex.PCRE.Light.Char8
|
|
Packit |
755117 |
import Text.Printf
|
|
Packit |
755117 |
|
|
Packit |
755117 |
(=~), (!~)
|
|
Packit |
755117 |
:: String -- ^ text
|
|
Packit |
755117 |
-> String -- ^ regex
|
|
Packit |
755117 |
-> Assertion
|
|
Packit |
755117 |
text =~ regexStr =
|
|
Packit |
755117 |
let
|
|
Packit |
755117 |
msg = printf "Expected /%s/, got %s" regexStr (show text)
|
|
Packit |
755117 |
-- NB show above the intentional -- to add quotes around the string and
|
|
Packit |
755117 |
-- escape newlines etc.
|
|
Packit |
755117 |
in assertBool msg $ match' text regexStr
|
|
Packit |
755117 |
text !~ regexStr =
|
|
Packit |
755117 |
let
|
|
Packit |
755117 |
msg = printf "Did not expect /%s/, got %s" regexStr (show text)
|
|
Packit |
755117 |
in assertBool msg $ not $ match' text regexStr
|
|
Packit |
755117 |
|
|
Packit |
755117 |
-- note: the order of arguments is reversed relative to match from
|
|
Packit |
755117 |
-- pcre-light, but consistent with =~ and !~
|
|
Packit |
755117 |
match' :: String -> String -> Bool
|
|
Packit |
755117 |
match' text regexStr =
|
|
Packit |
755117 |
let
|
|
Packit |
755117 |
regex = compile regexStr []
|
|
Packit |
755117 |
in
|
|
Packit |
755117 |
isJust $ match regex text []
|
|
Packit |
755117 |
|
|
Packit |
755117 |
main =
|
|
Packit |
755117 |
defaultMain $
|
|
Packit |
755117 |
testGroup "Unit tests for Test.Tasty.QuickCheck"
|
|
Packit |
755117 |
[ testCase "Success" $ do
|
|
Packit |
755117 |
Result{..} <- run' $ \x -> x >= (x :: Int)
|
|
Packit |
755117 |
-- there is no instance Show Outcome(
|
|
Packit |
755117 |
-- (because there is no instance Show SomeException),
|
|
Packit |
755117 |
-- so we can't use @?= for this
|
|
Packit |
755117 |
case resultOutcome of
|
|
Packit |
755117 |
Tasty.Success -> return ()
|
|
Packit |
755117 |
_ -> assertFailure $ show resultOutcome
|
|
Packit |
755117 |
resultDescription =~ "OK, passed 100 tests"
|
|
Packit |
755117 |
resultDescription !~ "Use .* to reproduce"
|
|
Packit |
755117 |
|
|
Packit |
755117 |
, testCase "Success, replay requested" $ do
|
|
Packit |
755117 |
Result{..} <- runReplay $ \x -> x >= (x :: Int)
|
|
Packit |
755117 |
-- there is no instance Show Outcome(
|
|
Packit |
755117 |
-- (because there is no instance Show SomeException),
|
|
Packit |
755117 |
-- so we can't use @?= for this
|
|
Packit |
755117 |
case resultOutcome of
|
|
Packit |
755117 |
Tasty.Success -> return ()
|
|
Packit |
755117 |
_ -> assertFailure $ show resultOutcome
|
|
Packit |
755117 |
resultDescription =~ "OK, passed 100 tests"
|
|
Packit |
755117 |
resultDescription =~ "Use .* to reproduce"
|
|
Packit |
755117 |
|
|
Packit |
755117 |
, testCase "Unexpected failure" $ do
|
|
Packit |
755117 |
Result{..} <- run' $ \x -> x > (x :: Int)
|
|
Packit |
755117 |
case resultOutcome of
|
|
Packit |
755117 |
Tasty.Failure {} -> return ()
|
|
Packit |
755117 |
_ -> assertFailure $ show resultOutcome
|
|
Packit |
755117 |
resultDescription =~ "Failed"
|
|
Packit |
755117 |
resultDescription =~ "Use .* to reproduce"
|
|
Packit |
755117 |
|
|
Packit |
755117 |
, testCase "Gave up" $ do
|
|
Packit |
755117 |
Result{..} <- run' $ \x -> x > x ==> x > (x :: Int)
|
|
Packit |
755117 |
case resultOutcome of
|
|
Packit |
755117 |
Tasty.Failure {} -> return ()
|
|
Packit |
755117 |
_ -> assertFailure $ show resultOutcome
|
|
Packit |
755117 |
resultDescription =~ "Gave up"
|
|
Packit |
755117 |
resultDescription =~ "Use .* to reproduce"
|
|
Packit |
755117 |
|
|
Packit |
755117 |
, testCase "No expected failure" $ do
|
|
Packit |
755117 |
Result{..} <- run' $ expectFailure $ \x -> x >= (x :: Int)
|
|
Packit |
755117 |
case resultOutcome of
|
|
Packit |
755117 |
Tasty.Failure {} -> return ()
|
|
Packit |
755117 |
_ -> assertFailure $ show resultOutcome
|
|
Packit |
755117 |
resultDescription =~ "Failed.*expected failure"
|
|
Packit |
755117 |
resultDescription =~ "Use .* to reproduce"
|
|
Packit |
755117 |
|
|
Packit |
755117 |
]
|
|
Packit |
755117 |
|
|
Packit |
755117 |
run' :: Testable p => p -> IO Result
|
|
Packit |
755117 |
run' p =
|
|
Packit |
755117 |
run
|
|
Packit |
755117 |
mempty -- options
|
|
Packit |
755117 |
(QC $ property p)
|
|
Packit |
755117 |
(const $ return ()) -- callback
|
|
Packit |
755117 |
|
|
Packit |
755117 |
runReplay :: Testable p => p -> IO Result
|
|
Packit |
755117 |
runReplay p =
|
|
Packit |
755117 |
run
|
|
Packit |
755117 |
(singleOption $ QuickCheckShowReplay True)
|
|
Packit |
755117 |
(QC $ property p)
|
|
Packit |
755117 |
(const $ return ())
|