Use HUnit assertions as QuickCheck properties
Haskell
Switch branches/tags
Latest commit 6885f23 Oct 16, 2016 @sol sol Bump version
Permalink
Failed to load latest commit information.
src/Test/QuickCheck HUnit 1.5 support Oct 12, 2016
.gitignore Initial commit Mar 30, 2013
.travis.yml Initial commit Mar 30, 2013
LICENSE Initial commit Mar 30, 2013
README.md Update README Mar 30, 2013
Setup.lhs Initial commit Mar 30, 2013
package.yaml Bump version Oct 16, 2016
quickcheck-io.cabal Bump version Oct 16, 2016

README.md

quickcheck-io: Use HUnit assertions as QuickCheck properties

This package provides an orphan instance that allows you to use HUnit assertions as QuickCheck properties.

Why would you want to do this?

Convenient testing of IO actions

Example: Setting an environment variable with setEnv and then getting the value back with getEnv should work for arbitrary key–value pairs. Sounds like a job for QuickCheck, huh?

import Test.QuickCheck
import Test.QuickCheck.IO ()
import Test.Hspec.Expectations (shouldReturn)

import System.SetEnv (setEnv)
import System.Environment (getEnv)

prop_setEnv k v = validKey k && validValue v ==> do
  setEnv k v
  getEnv k `shouldReturn` v

validKey   k = (not . null) k && '\NUL' `notElem` k && '=' `notElem` k
validValue v = (not . null) v && '\NUL' `notElem` v
ghci> quickCheck prop_setEnv
+++ OK, passed 100 tests.

Better error messages for failing QuickCheck properties

You can e.g. use HUnit's @?=

ghci> import Test.QuickCheck
ghci> import Test.QuickCheck.IO
ghci> import Test.HUnit
ghci> quickCheck $ \x -> x + 23 @?= 23
*** Failed! (after 3 tests and 2 shrinks):                             
expected: 23
 but got: 24
1

or Hspec's shouldBe

ghci> import Test.QuickCheck
ghci> import Test.QuickCheck.IO
ghci> import Test.Hspec.Expectations
ghci> quickCheck $ \x -> x + 23 `shouldBe` 23
*** Failed! (after 3 tests and 2 shrinks):                             
expected: 23
 but got: 24
1