tasty-quickcheck-0.10.2/Test/0000755000000000000000000000000013762443710014162 5ustar0000000000000000tasty-quickcheck-0.10.2/Test/Tasty/0000755000000000000000000000000014146241734015265 5ustar0000000000000000tasty-quickcheck-0.10.2/tests/0000755000000000000000000000000014120402324014366 5ustar0000000000000000tasty-quickcheck-0.10.2/Test/Tasty/QuickCheck.hs0000644000000000000000000002016014146243457017636 0ustar0000000000000000-- | This module allows to use QuickCheck properties in tasty. {-# LANGUAGE GeneralizedNewtypeDeriving, DeriveDataTypeable #-} module Test.Tasty.QuickCheck ( testProperty , testProperties , QuickCheckTests(..) , QuickCheckReplay(..) , QuickCheckShowReplay(..) , QuickCheckMaxSize(..) , QuickCheckMaxRatio(..) , QuickCheckVerbose(..) , QuickCheckMaxShrinks(..) -- * Re-export of Test.QuickCheck , module Test.QuickCheck -- * Internal -- | If you are building a test suite, you don't need these functions. -- -- They may be used by other tasty add-on packages (such as tasty-hspec). , QC(..) , optionSetToArgs ) where import Test.Tasty ( testGroup ) import Test.Tasty.Providers import Test.Tasty.Options import qualified Test.QuickCheck as QC import Test.Tasty.Runners (formatMessage) import Test.QuickCheck hiding -- for re-export ( quickCheck , Args(..) , Result , stdArgs , quickCheckWith , quickCheckWithResult , quickCheckResult , verboseCheck , verboseCheckWith , verboseCheckWithResult , verboseCheckResult , verbose -- Template Haskell functions #if MIN_VERSION_QuickCheck(2,11,0) , allProperties #endif , forAllProperties , quickCheckAll , verboseCheckAll ) import Data.Typeable import Data.List import Text.Printf import Test.QuickCheck.Random (mkQCGen) import Options.Applicative (metavar) import System.Random (getStdRandom, randomR) #if !MIN_VERSION_base(4,9,0) import Control.Applicative import Data.Monoid #endif newtype QC = QC QC.Property deriving Typeable -- | Create a 'Test' for a QuickCheck 'QC.Testable' property testProperty :: QC.Testable a => TestName -> a -> TestTree testProperty name prop = singleTest name $ QC $ QC.property prop -- | Create a test from a list of QuickCheck properties. To be used -- with 'Test.QuickCheck.allProperties'. E.g. -- -- >tests :: TestTree -- >tests = testProperties "Foo" $allProperties testProperties :: TestName -> [(String, Property)] -> TestTree testProperties name = testGroup name . map (uncurry testProperty) -- | Number of test cases for QuickCheck to generate newtype QuickCheckTests = QuickCheckTests Int deriving (Num, Ord, Eq, Real, Enum, Integral, Typeable) newtype QuickCheckReplay = QuickCheckReplay (Maybe Int) deriving (Typeable) -- | If a test case fails unexpectedly, show the replay token newtype QuickCheckShowReplay = QuickCheckShowReplay Bool deriving (Typeable) -- | Size of the biggest test cases newtype QuickCheckMaxSize = QuickCheckMaxSize Int deriving (Num, Ord, Eq, Real, Enum, Integral, Typeable) -- | Maximum number of of discarded tests per successful test before giving up. newtype QuickCheckMaxRatio = QuickCheckMaxRatio Int deriving (Num, Ord, Eq, Real, Enum, Integral, Typeable) -- | Show the test cases that QuickCheck generates newtype QuickCheckVerbose = QuickCheckVerbose Bool deriving (Typeable) -- | Number of shrinks allowed before QuickCheck will fail a test. -- -- @since 0.10.2 newtype QuickCheckMaxShrinks = QuickCheckMaxShrinks Int deriving (Num, Ord, Eq, Real, Enum, Integral, Typeable) instance IsOption QuickCheckTests where defaultValue = 100 parseValue = -- We allow numeric underscores for readability; see -- https://github.com/UnkindPartition/tasty/issues/263 fmap QuickCheckTests . safeRead . filter (/= '_') optionName = return "quickcheck-tests" optionHelp = return "Number of test cases for QuickCheck to generate. Underscores accepted: e.g. 10_000_000" optionCLParser = mkOptionCLParser $ metavar "NUMBER" instance IsOption QuickCheckReplay where defaultValue = QuickCheckReplay Nothing -- Reads a replay int seed parseValue v = QuickCheckReplay . Just <$> safeRead v optionName = return "quickcheck-replay" optionHelp = return "Random seed to use for replaying a previous test run (use same --quickcheck-max-size)" optionCLParser = mkOptionCLParser $ metavar "SEED" instance IsOption QuickCheckShowReplay where defaultValue = QuickCheckShowReplay False parseValue = fmap QuickCheckShowReplay . safeReadBool optionName = return "quickcheck-show-replay" optionHelp = return "Show a replay token for replaying tests" optionCLParser = flagCLParser Nothing (QuickCheckShowReplay True) defaultMaxSize :: Int defaultMaxSize = QC.maxSize QC.stdArgs instance IsOption QuickCheckMaxSize where defaultValue = fromIntegral defaultMaxSize parseValue = fmap QuickCheckMaxSize . safeRead optionName = return "quickcheck-max-size" optionHelp = return "Size of the biggest test cases quickcheck generates" optionCLParser = mkOptionCLParser $ metavar "NUMBER" instance IsOption QuickCheckMaxRatio where defaultValue = fromIntegral $ QC.maxDiscardRatio QC.stdArgs parseValue = fmap QuickCheckMaxRatio . safeRead optionName = return "quickcheck-max-ratio" optionHelp = return "Maximum number of discared tests per successful test before giving up" optionCLParser = mkOptionCLParser $ metavar "NUMBER" instance IsOption QuickCheckVerbose where defaultValue = QuickCheckVerbose False parseValue = fmap QuickCheckVerbose . safeReadBool optionName = return "quickcheck-verbose" optionHelp = return "Show the generated test cases" optionCLParser = mkFlagCLParser mempty (QuickCheckVerbose True) instance IsOption QuickCheckMaxShrinks where defaultValue = QuickCheckMaxShrinks (QC.maxShrinks QC.stdArgs) parseValue = fmap QuickCheckMaxShrinks . safeRead optionName = return "quickcheck-shrinks" optionHelp = return "Number of shrinks allowed before QuickCheck will fail a test" optionCLParser = mkOptionCLParser $ metavar "NUMBER" -- | Convert tasty options into QuickCheck options. -- -- This is a low-level function that was originally added for tasty-hspec -- but may be used by others. -- -- @since 0.9.1 optionSetToArgs :: OptionSet -> IO (Int, QC.Args) optionSetToArgs opts = do replaySeed <- case mReplay of Nothing -> getStdRandom (randomR (1,999999)) Just seed -> return seed let args = QC.stdArgs { QC.chatty = False , QC.maxSuccess = nTests , QC.maxSize = maxSize , QC.replay = Just (mkQCGen replaySeed, 0) , QC.maxDiscardRatio = maxRatio , QC.maxShrinks = maxShrinks } return (replaySeed, args) where QuickCheckTests nTests = lookupOption opts QuickCheckReplay mReplay = lookupOption opts QuickCheckMaxSize maxSize = lookupOption opts QuickCheckMaxRatio maxRatio = lookupOption opts QuickCheckMaxShrinks maxShrinks = lookupOption opts instance IsTest QC where testOptions = return [ Option (Proxy :: Proxy QuickCheckTests) , Option (Proxy :: Proxy QuickCheckReplay) , Option (Proxy :: Proxy QuickCheckShowReplay) , Option (Proxy :: Proxy QuickCheckMaxSize) , Option (Proxy :: Proxy QuickCheckMaxRatio) , Option (Proxy :: Proxy QuickCheckVerbose) , Option (Proxy :: Proxy QuickCheckMaxShrinks) ] run opts (QC prop) _yieldProgress = do (replaySeed, args) <- optionSetToArgs opts let QuickCheckShowReplay showReplay = lookupOption opts QuickCheckVerbose verbose = lookupOption opts maxSize = QC.maxSize args testRunner = if verbose then QC.verboseCheckWithResult else QC.quickCheckWithResult replayMsg = makeReplayMsg replaySeed maxSize -- Quickcheck already catches exceptions, no need to do it here. r <- testRunner args prop qcOutput <- formatMessage $ QC.output r let qcOutputNl = if "\n" `isSuffixOf` qcOutput then qcOutput else qcOutput ++ "\n" testSuccessful = successful r putReplayInDesc = (not testSuccessful) || showReplay return $ (if testSuccessful then testPassed else testFailed) (qcOutputNl ++ (if putReplayInDesc then replayMsg else "")) successful :: QC.Result -> Bool successful r = case r of QC.Success {} -> True _ -> False makeReplayMsg :: Int -> Int -> String makeReplayMsg seed size = let sizeStr = if (size /= defaultMaxSize) then printf " --quickcheck-max-size=%d" size else "" in printf "Use --quickcheck-replay=%d%s to reproduce." seed sizeStr tasty-quickcheck-0.10.2/tests/test.hs0000644000000000000000000000634514120402324015711 0ustar0000000000000000{-# LANGUAGE RecordWildCards #-} import Test.Tasty import Test.Tasty.Options import Test.Tasty.Providers as Tasty import Test.Tasty.Runners as Tasty import Test.Tasty.QuickCheck import Test.Tasty.HUnit import Data.Maybe import Text.Regex.PCRE.Light.Char8 import Text.Printf (=~), (!~) :: String -- ^ text -> String -- ^ regex -> Assertion text =~ regexStr = let msg = printf "Expected /%s/, got %s" regexStr (show text) -- NB show above the intentional -- to add quotes around the string and -- escape newlines etc. in assertBool msg $ match' text regexStr text !~ regexStr = let msg = printf "Did not expect /%s/, got %s" regexStr (show text) in assertBool msg $ not $ match' text regexStr -- note: the order of arguments is reversed relative to match from -- pcre-light, but consistent with =~ and !~ match' :: String -> String -> Bool match' text regexStr = let regex = compile regexStr [] in isJust $ match regex text [] main :: IO () main = defaultMain $ testGroup "Unit tests for Test.Tasty.QuickCheck" [ testCase "Success" $ do Result{..} <- run' $ \x -> x >= (x :: Int) -- there is no instance Show Outcome( -- (because there is no instance Show SomeException), -- so we can't use @?= for this case resultOutcome of Tasty.Success -> return () _ -> assertFailure $ show resultOutcome resultDescription =~ "OK, passed 100 tests" resultDescription !~ "Use .* to reproduce" , testCase "Success, replay requested" $ do Result{..} <- runReplay $ \x -> x >= (x :: Int) -- there is no instance Show Outcome( -- (because there is no instance Show SomeException), -- so we can't use @?= for this case resultOutcome of Tasty.Success -> return () _ -> assertFailure $ show resultOutcome resultDescription =~ "OK, passed 100 tests" resultDescription =~ "Use .* to reproduce" , testCase "Unexpected failure" $ do Result{..} <- run' $ \x -> x > (x :: Int) case resultOutcome of Tasty.Failure {} -> return () _ -> assertFailure $ show resultOutcome resultDescription =~ "Failed" resultDescription =~ "Use .* to reproduce" , testCase "Gave up" $ do Result{..} <- run' $ \x -> x > x ==> x > (x :: Int) case resultOutcome of Tasty.Failure {} -> return () _ -> assertFailure $ show resultOutcome resultDescription =~ "Gave up" resultDescription =~ "Use .* to reproduce" , testCase "No expected failure" $ do Result{..} <- run' $ expectFailure $ \x -> x >= (x :: Int) case resultOutcome of Tasty.Failure {} -> return () _ -> assertFailure $ show resultOutcome resultDescription =~ "Failed.*expected failure" resultDescription =~ "Use .* to reproduce" ] run' :: Testable p => p -> IO Result run' p = run mempty -- options (QC $ property p) (const $ return ()) -- callback runReplay :: Testable p => p -> IO Result runReplay p = run (singleOption $ QuickCheckShowReplay True) (QC $ property p) (const $ return ()) tasty-quickcheck-0.10.2/CHANGELOG.md0000644000000000000000000000531114146243462015053 0ustar0000000000000000Changes ======= Version 0.10.2 -------------- Export `QuickCheckMaxShrinks` Version 0.10.1.2 ---------------- The only point of this release is to introduce compatibility with GHCs back to 7.0 (see https://github.com/UnkindPartition/tasty/pull/287). Note, however, that these changes are not merged to the master branch, and the future releases will only support the GHC/base versions from the last 5 years, as per our usual policy. To test with even older GHCs, you'll have to use this particular version of tasty-quickcheck (or have the constraint solver pick it for you when testing with older GHCs). The source of this release is in the `support-old-ghcs` branch of the tasty repository. Version 0.10.1.1 ---------------- * Accept underscores in `--quickcheck-tests` for readability, e.g. `--quickcheck-tests 10_000_000`. Version 0.10.1 -------------- * Add a --quickcheck-shrinks flag Version 0.10 ------------ * Do not re-export irrelevant Template Haskell QuickCheck functions * Make boolean options case-insensitive * Make --quickcheck-show-replay a command-line flag rather than an option requiring an argument `True` Version 0.9.2 ------------- * Add a `testProperties` function, which allows to test all QuickCheck properties defined in a module. Version 0.9.1 ------------- * Expose internal `optionSetToArgs` function Version 0.9 ------------- * Drop support for QuickCheck < 2.7 * Change the `--quickcheck-show-replay` semantics Previously, when the flag was set to `True`, the seed was only shown on failure. Now, the seed is shown on failure regardless of the value of the flag, and the flag causes the seed to be shown on success. The default value for `--quickcheck-show-replay` is now `False`. Version 0.8.4 ------------- Add a "--quickcheck-verbose" option Version 0.8.3.2 --------------- * Put correct lower version bound on tasty * Allow use of older QuickCheck for HP compatibility Version 0.8.3.1 --------------- When QC message throws an exception, still print the replay message Version 0.8.3 ------------- * Export 'show replay' option * Fixed a run-time error when using QuickCheck's `expectFailure` Version 0.8.2 ------------- * Allow suppressing the --quickcheck-replay hint * Split the changelog out of the main tasty changelog Version 0.8.1 ------------- Re-export `Gen` from `Test.Tasty.QuickCheck` Version 0.8.0.3 --------------- Upgrade to QuickCheck 2.7 Version 0.8 ----------- Update to tasty-0.8 Version 0.3.1 ------------- Use the original QuickCheck's output format Version 0.3 ----------- Add options for maximum size and maximum ratio; support replay. Version 0.2 ----------- Re-export useful bits of `Test.QuickCheck` from `Test.Tasty.QuickCheck` tasty-quickcheck-0.10.2/LICENSE0000644000000000000000000000204312457221174014245 0ustar0000000000000000Copyright (c) 2013 Roman Cheplyaka Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tasty-quickcheck-0.10.2/Setup.hs0000644000000000000000000000005614015452234014671 0ustar0000000000000000import Distribution.Simple main = defaultMain tasty-quickcheck-0.10.2/tasty-quickcheck.cabal0000644000000000000000000000313514146244001017472 0ustar0000000000000000-- Initial tasty-quickcheck.cabal generated by cabal init. For further -- documentation, see http://haskell.org/cabal/users-guide/ name: tasty-quickcheck version: 0.10.2 synopsis: QuickCheck support for the Tasty test framework. description: QuickCheck support for the Tasty test framework. license: MIT license-file: LICENSE author: Roman Cheplyaka maintainer: Roman Cheplyaka -- copyright: homepage: https://github.com/UnkindPartition/tasty bug-reports: https://github.com/UnkindPartition/tasty/issues category: Testing build-type: Simple extra-source-files: CHANGELOG.md cabal-version: >=1.10 Source-repository head type: git location: git://github.com/UnkindPartition/tasty.git subdir: quickcheck library exposed-modules: Test.Tasty.QuickCheck -- other-modules: other-extensions: GeneralizedNewtypeDeriving, DeriveDataTypeable build-depends: base >= 4.8 && < 5, tagged, tasty >= 1.0.1, random, QuickCheck >= 2.10, optparse-applicative -- hs-source-dirs: default-language: Haskell2010 default-extensions: CPP ghc-options: -Wall test-suite test default-language: Haskell2010 default-extensions: CPP type: exitcode-stdio-1.0 hs-source-dirs: tests main-is: test.hs build-depends: base >= 4.7 && < 5 , tasty , tasty-quickcheck , tasty-hunit , pcre-light ghc-options: -Wall if (!impl(ghc >= 8.0) || os(windows)) buildable: False