Skip to content

Instantly share code, notes, and snippets.

@nh2
Created June 7, 2014 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nh2/1ce734759b196c3483fa to your computer and use it in GitHub Desktop.
Save nh2/1ce734759b196c3483fa to your computer and use it in GitHub Desktop.
How to QuickCheck that a certain error is thrown in pure Haskell code
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Exception
import Control.DeepSeq
import Data.List (isPrefixOf)
import System.IO.Unsafe (unsafePerformIO)
import Test.QuickCheck
main :: IO ()
main = quickCheck $ property $ \(list :: [Char], n :: Int) ->
n >= length list ==> throwsIndexError (list !! n)
throwsIndexError :: (NFData a) => a -> Bool
throwsIndexError expr = unsafePerformIO $ do
res <- try $ evaluate (force expr)
case res of
Left (ErrorCall msg) -> return $ "Prelude.(!!): index too large" `isPrefixOf` msg
Right _ -> return False -- no exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment