mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
some additions to testing utilities in preparation for part2
This commit is contained in:
@@ -1 +1 @@
|
||||
module Mooc.LimitedPrelude (Int,Integer,Bool(..),Ordering(..),Eq(..),Ord(..),Num(..),Maybe(..),otherwise) where
|
||||
module Mooc.LimitedPrelude (Int,Integer,Bool(..),Ordering(..),Eq(..),Ord(..),Num(..),Maybe(..),otherwise,undefined) where
|
||||
|
||||
+19
-3
@@ -2,7 +2,7 @@
|
||||
|
||||
module Mooc.Test where
|
||||
|
||||
import Control.Exception (evaluate,SomeException,fromException)
|
||||
import Control.Exception (try,evaluate,SomeException,fromException)
|
||||
import Control.Monad
|
||||
import Control.DeepSeq (deepseq)
|
||||
import Data.Foldable
|
||||
@@ -54,6 +54,10 @@ forAllShrink_ gen = forAllShrinkBlind gen shrink
|
||||
forAll_ :: Arbitrary a => (a -> Property) -> Property
|
||||
forAll_ = forAllShrink_ arbitrary
|
||||
|
||||
-- nondeterministic conjoin
|
||||
conjoin' :: Testable prop => [prop] -> Property
|
||||
conjoin' ps = property $ elements ps
|
||||
|
||||
-- timeouts for evaluation
|
||||
|
||||
timedMillis = 500
|
||||
@@ -64,6 +68,18 @@ timed val k = monadicIO $ do
|
||||
Nothing -> return $ counterexample' (" didn't return in "++show timedMillis++"ms") $ False
|
||||
Just v -> return $ k v
|
||||
|
||||
-- exceptions
|
||||
|
||||
eval :: a -> PropertyM IO (Either SomeException a)
|
||||
eval x = run $ try $ evaluate x
|
||||
|
||||
isFail :: Either SomeException a -> Property
|
||||
isFail (Left e) = property True
|
||||
isFail (Right _) = counterexample " should fail" False
|
||||
|
||||
shouldFail :: a -> Property
|
||||
shouldFail x = monadicIO $ fmap isFail $ eval x
|
||||
|
||||
-- handling TODO excercises
|
||||
|
||||
isTodo :: SomeException -> Bool
|
||||
@@ -86,10 +102,10 @@ instance Monoid Outcome where
|
||||
|
||||
quietArgs = stdArgs {chatty=False}
|
||||
|
||||
timeLimit = 10 * 1000 * 1000 -- 10 seconds in microseconds
|
||||
globalTimeLimit = 10 * 1000 * 1000 -- 10 seconds in microseconds
|
||||
|
||||
myCheck :: Testable prop => prop -> IO Outcome
|
||||
myCheck prop = quickCheckWithResult quietArgs (within timeLimit prop) >>= interpret
|
||||
myCheck prop = quickCheckWithResult quietArgs (within globalTimeLimit prop) >>= interpret
|
||||
where interpret res
|
||||
| resultIsTodo res = return Todo
|
||||
| isSuccess res = return Pass
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{-# LANGUAGE TemplateHaskell, DeriveLift, StandaloneDeriving #-}
|
||||
|
||||
module Mooc.Th (testing, testing',
|
||||
module Mooc.Th (testing, testing', timeLimit,
|
||||
isDefined, withDefined, hasType, importsOnly, show',
|
||||
reifyType, DataType(..), FieldType(..), Constructor(..),
|
||||
withInstance, withInstance1, classContains, defineInstance)
|
||||
@@ -199,6 +199,7 @@ unqualify (UInfixE left op right) = UInfixE (unqualify left) (unqualify op) (unq
|
||||
unqualify (LitE l) = LitE l
|
||||
unqualify (TupE exps) = TupE (map unqualify exps)
|
||||
unqualify (ListE exps) = ListE (map unqualify exps)
|
||||
unqualify (ArithSeqE (FromR x)) = ArithSeqE (FromR (unqualify x))
|
||||
unqualify x = error $ "Unsupported: " ++ show x
|
||||
|
||||
testing' :: Q Exp -> Q Exp
|
||||
|
||||
Reference in New Issue
Block a user