Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Use HUnit assertions as QuickCheck properties
branch: master
Failed to load latest commit information.
src/Test/QuickCheck Fix a warning with QuickCheck-2.7.*
.gitignore Initial commit
.travis.yml Initial commit
LICENSE Initial commit
README.md Update README
Setup.lhs Initial commit
quickcheck-io.cabal Bump version

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
Something went wrong with that request. Please try again.