Created
June 7, 2014 20:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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