> import Control.Exception (Exception, PatternMatchFail,> SomeException, evaluate,> fromException, try)> import Data.Either (either)> import Test.Hspec (anyException, describe, hspec, it,> shouldThrow)> import Test.QuickCheck hiding (Result)> import Test.QuickCheck.Monadic (assert, monadicIO, run)对于初学者,让我们编写一个函数,使我们能够检查是否抛出了某些异常:> throwsExceptionOr :: Exception e => (e -> Bool) -> (a -> Bool) -> a -> IO Bool> throwsExceptionOr pe pa = fmap (either pe pa) . try . evaluate这使您可以编写如下测试:> prop_fun_1 x = monadicIO . run $> throwsExceptionOr (const True :: SomeException -> Bool)> (== 5)> (foo x) throwsExceptionOr非常笼统,因此您可以定义自己的助手:> -- | Should always throw an exception.> throwsException :: Exception e => (e -> Bool) -> a -> IO Bool> throwsException p = fmap (either p (const False)) . try . evaluate> -- | Should either pass the test or throw an exception.> exceptionOr :: a -> (a -> Bool) -> IO Bool> exceptionOr x p = fmap (either anyException p) . try . evaluate $ x> where> anyException :: SomeException -> Bool> anyException = const True现在您可以照常编写测试了:> data Result = A | B deriving (Enum, Show, Eq, Ord, Bounded, Read)>> instance Arbitrary Result where> arbitrary = elements [A, B]>> foo :: Result -> Int> foo A = 5>> prop_foo x = monadicIO . run $ foo x `exceptionOr` (== 5)您可以进一步将monadicIO . run移到另一个帮助器中,但是剩下来作为练习.此外,您可以使功能兼容与其他测试框架,例如hspec或tasty:> main :: IO ()> main = hspec $ do> describe "foo" $ do> it "either returns five or throws a pattern match fail" $ propertyIO $ \x ->> throwsExceptionOr patternMatchFail (==5) (foo x)>> it "throws an exception on input B" $> evaluate (foo B) `shouldThrow` anyException>> where> patternMatchFail :: PatternMatchFail -> Bool> patternMatchFail _ = True>> -- I think there is a better combinator for this> propertyIO f = property $ \x -> monadicIO . run $ f x话虽这么说,您想摆脱的语言如果可能的话,可能会在编译时出现运行时错误.这包括获取摆脱部分功能或可能的废话.这取决于实际使用,当然.如果您可以验证head 总是被非空调用在整个程序中列出来,继续使用它.如果不能,请使用模式匹配(请参见此讨论 (按head类型).无论哪种方式,鉴于较旧版本的GHC不提供堆栈调用,您宁愿希望在编译时出错,而不是在运行时没有堆栈跟踪的错误(最近版本的GHC为此提供了一些不错的功能)./p>I have interest in using the quick check library but it seems that it is designed to test properties. What I would like to do is generate random data for my defined data types and test functions I have written. I do not care about what the result is, just if the function produces a runtime error when fed random data. All of the quick check examples I have seen are for testing properties of functions like is the result greater than 5 when fed random data. Is there a way to use quick check in this manner? Something likedata Result = A | Bfun :: Result -> Intfun A = 5main = check funIn the above code I have a custom data type, and a incomplete function. This function will fail if passed B. There are of course more types of runtime errors than just incomplete functions. I would like to have check generate data, and feed it to the function. Not caring what the result is. Is quick check able to do this?Edit -I should note that I am not looking for flags that check for incomplete patterns, and what not. I am interested in general purpose runtime error checks. 解决方案 Rather make sure that functions that don't handle IO don't throw exceptions if possible.Exceptions can only get caught in IO, and they kind of break the things you otherwise expect from a pure function.It's possible with some helpers fromControl.Exceptionand Test.QuickCheck.Monadic:> import Control.Exception (Exception, PatternMatchFail,> SomeException, evaluate,> fromException, try)> import Data.Either (either)> import Test.Hspec (anyException, describe, hspec, it,> shouldThrow)> import Test.QuickCheck hiding (Result)> import Test.QuickCheck.Monadic (assert, monadicIO, run)For starters, let's write a function that enables us to check that a certain exception is thrown:> throwsExceptionOr :: Exception e => (e -> Bool) -> (a -> Bool) -> a -> IO Bool> throwsExceptionOr pe pa = fmap (either pe pa) . try . evaluateThis enables you to write tests like this:> prop_fun_1 x = monadicIO . run $> throwsExceptionOr (const True :: SomeException -> Bool)> (== 5)> (foo x)throwsExceptionOr is very general, so that you can define your own helpers:> -- | Should always throw an exception.> throwsException :: Exception e => (e -> Bool) -> a -> IO Bool> throwsException p = fmap (either p (const False)) . try . evaluate> -- | Should either pass the test or throw an exception.> exceptionOr :: a -> (a -> Bool) -> IO Bool> exceptionOr x p = fmap (either anyException p) . try . evaluate $ x> where> anyException :: SomeException -> Bool> anyException = const TrueNow you can write your tests as usual:> data Result = A | B deriving (Enum, Show, Eq, Ord, Bounded, Read)>> instance Arbitrary Result where> arbitrary = elements [A, B]>> foo :: Result -> Int> foo A = 5>> prop_foo x = monadicIO . run $ foo x `exceptionOr` (== 5)You can go further and move the monadicIO . run into another helper, butthat's left as an exercise. Furthermore, you can make the functions compatiblewith other testing frameworks, such as hspec or tasty:> main :: IO ()> main = hspec $ do> describe "foo" $ do> it "either returns five or throws a pattern match fail" $ propertyIO $ \x ->> throwsExceptionOr patternMatchFail (==5) (foo x)>> it "throws an exception on input B" $> evaluate (foo B) `shouldThrow` anyException>> where> patternMatchFail :: PatternMatchFail -> Bool> patternMatchFail _ = True>> -- I think there is a better combinator for this> propertyIO f = property $ \x -> monadicIO . run $ f xThat being said, regardless of the used languages you want to get rid ofpossible runtime errors at compile time if possible. This includes gettingrid of partial functions or possible type nonsense. This depends on the actualuse, of course. If you can verify that head always gets called on a non-emptylist throughout your program, go ahead and use it. If you can't, use patternmatching (see this discussion on head's type).Either way, given that older versions of GHC don't provide stack calls, you rather want to have errors at compile time than errors without a stack trace at runtime (recent versions of GHC have some nice features for this). 这篇关于快速检查运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 10:28