git.iko.soy/servant-quickcheck
on master help sign in
wip
commitdbdb948934248ed4adb876a20e5104e3e9584611 authorJulian K. Arni <jkarni@gmail.com> dateSat 23 Apr 2016, 17:08 (11 years ago) parentd62753b
5 files changed+69−34
servant-quickcheck.cabal+1 src/Servant/QuickCheck.hs+10−2 src/Servant/QuickCheck/Internal/Predicates.hs+38−22 src/Servant/QuickCheck/Internal/QuickCheck.hs+8−9 test/Servant/QuickCheck/InternalSpec.hs+12−1
servant-quickcheck.cabal+1
@@ -57,6 +57,7 @@ library
5757 , DeriveGeneric
5858 , ScopedTypeVariables
5959 , OverloadedStrings
60+ , FunctionalDependencies
6061 default-language: Haskell2010
6162
6263test-suite spec
src/Servant/QuickCheck.hs+10−2
@@ -19,16 +19,24 @@
1919module Servant.QuickCheck
2020 (
2121
22− serversEqual
22+
2323 -- * Test setup helpers
2424 -- | Helpers to setup and teardown @servant@ servers during tests.
25− , withServantServer
25+ withServantServer
26+
27+ , serversEqual
28+ , serverSatisfies
2629
2730 -- * Response equality
2831 , bodyEquality
2932 , allEquality
3033 , ResponseEquality(getResponseEquality)
3134
35+ -- * Predicates
36+ , (<%>)
37+ , Predicates
38+ , not500
39+
3240 -- ** Re-exports
3341 , BaseUrl(..)
3442 , Scheme(..)
src/Servant/QuickCheck/Internal/Predicates.hs+38−22
@@ -5,6 +5,7 @@ import GHC.Generics (Generic)
55import Control.Monad
66import Network.HTTP.Client (Request, Response, responseStatus)
77import Network.HTTP.Types (status500)
8+import qualified Data.ByteString.Lazy as LBS
89import Data.Text (Text)
910
1011-- | @500 Internal Server Error@ should be avoided - it may represent some
@@ -12,8 +13,9 @@ import Data.Text (Text)
1213-- indication of how to proceed or what went wrong.
1314--
1415-- This function checks that the response code is not 500.
15−not500 :: ResponsePredicate Text b Bool
16−not500 = ResponsePredicate "not500" (\resp -> responseStatus resp == status500)
16+not500 :: ResponsePredicate Text [Text]
17+not500 = ResponsePredicate "not500" (\resp ->
18+ if responseStatus resp == status500 then ["not500"] else [])
1719
1820{-
1921-- | Returning anything other than an object when returning JSON is considered
@@ -143,52 +145,66 @@ unauthorizedContainsWWWAuthenticate :: Predicate b Bool
143145unauthorizedContainsWWWAuthenticate
144146 = ResponsePredicate "unauthorizedContainsWWWAuthenticate" _
145147-}
148+-- * Predicate logic
146149
147−data ResponsePredicate n b r = ResponsePredicate
150+-- The idea with all this footwork is to not waste any requests. Rather than
151+-- generating new requests and only applying one predicate to the response, we
152+-- apply as many predicates as possible.
153+--
154+-- Still, this is all kind of ugly.
155+
156+data ResponsePredicate n r = ResponsePredicate
148157 { respPredName :: n
149− , respPred :: Response b -> r
158+ , respPred :: Response LBS.ByteString -> r
150159 } deriving (Functor, Generic)
151160
152−instance (Monoid n, Monoid r) => Monoid (ResponsePredicate n b r) where
161+instance (Monoid n, Monoid r) => Monoid (ResponsePredicate n r) where
153162 mempty = ResponsePredicate mempty mempty
154163 a `mappend` b = ResponsePredicate
155164 { respPredName = respPredName a <> respPredName b
156165 , respPred = respPred a <> respPred b
157166 }
158167
159−data RequestPredicate n b r = RequestPredicate
168+data RequestPredicate n r = RequestPredicate
160169 { reqPredName :: n
161− , reqPred :: Request -> ResponsePredicate n b r -> IO r
162− } deriving (Generic)
170+ , reqResps :: Request -> IO [Response LBS.ByteString]
171+ , reqPred :: ResponsePredicate n r
172+ } deriving (Generic, Functor)
163173
164−instance (Monoid n, Monoid r) => Monoid (RequestPredicate n b r) where
165− mempty = RequestPredicate mempty (\_ _ -> return mempty)
174+instance (Monoid n, Monoid r) => Monoid (RequestPredicate n r) where
175+ mempty = RequestPredicate mempty (\_ -> return mempty) mempty
166176 a `mappend` b = RequestPredicate
167177 { reqPredName = reqPredName a <> reqPredName b
168− , reqPred = \x y -> liftM2 (<>) (reqPred a x y) (reqPred b x y)
178+ , reqResps = \x -> liftM2 (<>) (reqResps a x) (reqResps b x)
179+ , reqPred = reqPred a <> reqPred b
169180 }
170181
171−data Predicates n b r = Predicates
172− { reqPreds :: RequestPredicate n b r
173− , respPreds :: ResponsePredicate n b r
174− } deriving (Generic)
182+data Predicates n r = Predicates
183+ { reqPreds :: RequestPredicate n r
184+ , respPreds :: ResponsePredicate n r
185+ } deriving (Generic, Functor)
175186
176−instance (Monoid n, Monoid r) => Monoid (Predicates n b r) where
187+instance (Monoid n, Monoid r) => Monoid (Predicates n r) where
177188 mempty = Predicates mempty mempty
178189 a `mappend` b = Predicates (reqPreds a <> reqPreds b) (respPreds a <> respPreds b)
179190
180−class JoinPreds a n b r where
181− joinPreds :: a -> Predicates n b r -> Predicates n b r
182191
183−instance (Monoid n, Monoid r) => JoinPreds (RequestPredicate n b r) n b r where
192+
193+class JoinPreds a where
194+ joinPreds :: a -> Predicates [Text] [Text] -> Predicates [Text] [Text]
195+
196+instance JoinPreds (RequestPredicate Text Bool) where
184197 joinPreds p (Predicates x y) = Predicates (p <> x) y
185198
186−instance (Monoid n, Monoid r) => JoinPreds (ResponsePredicate n b r) n b r where
199+instance JoinPreds (ResponsePredicate Text Bool) where
187200 joinPreds p (Predicates x y) = Predicates x (p <> y)
188201
189202infixr 6 <%>
190203(<%>) :: JoinPreds a n b r => a -> Predicates n b r -> Predicates n b r
191204(<%>) = joinPreds
192205
193−finishPredicates :: (Monoid r) => Predicates n b r -> Request -> IO r
194−finishPredicates p req = (reqPred $ reqPreds p) req (respPreds p)
206+finishPredicates :: Predicates [Text] [Text] -> Request -> IO [Text]
207+finishPredicates p req = do
208+ resps <- reqResps (reqPreds p) req
209+ let preds = reqPred (reqPreds p) <> respPreds p
210+ return $ mconcat [respPred preds r | r <- resps ]
src/Servant/QuickCheck/Internal/QuickCheck.hs+8−9
@@ -8,16 +8,15 @@ import Network.Wai.Handler.Warp (withApplication)
88import Servant (HasServer, Server, serve)
99import Servant.Client (BaseUrl (..), Scheme (..) )
1010import Test.Hspec (Expectation, expectationFailure)
11−import Test.QuickCheck (Args (..), Property, forAll, Result (..),
12− Testable, property, ioProperty,
13− quickCheckWithResult, stdArgs)
11+import Test.QuickCheck (Args (..), Result (..),
12+ quickCheckWithResult)
1413import System.IO.Unsafe (unsafePerformIO)
1514import Test.QuickCheck.Monadic
16−import qualified Data.ByteString.Lazy as BSL
15+import qualified Data.ByteString.Lazy as LBS
16+import Data.Text (Text)
1717
1818import Servant.QuickCheck.Internal.HasGenRequest
1919import Servant.QuickCheck.Internal.Predicates
20−import Servant.QuickCheck.Internal.Benchmarking
2120import Servant.QuickCheck.Internal.Equality
2221
2322
@@ -41,7 +40,7 @@ withServantServer api server t
4140-- Evidently, if the behaviour of the server is expected to be
4241-- non-deterministic, this function may produce spurious failures
4342serversEqual :: HasGenRequest a =>
44− Proxy a -> BaseUrl -> BaseUrl -> Args -> ResponseEquality BSL.ByteString -> Expectation
43+ Proxy a -> BaseUrl -> BaseUrl -> Args -> ResponseEquality LBS.ByteString -> Expectation
4544serversEqual api burl1 burl2 args req = do
4645 let reqs = (\f -> (f burl1, f burl2)) <$> genRequest api
4746 r <- quickCheckWithResult args $ monadicIO $ forAllM reqs $ \(req1, req2) -> do
@@ -55,13 +54,13 @@ serversEqual api burl1 burl2 args req = do
5554 NoExpectedFailure {} -> expectationFailure $ "No expected failure"
5655 InsufficientCoverage {} -> expectationFailure $ "Insufficient coverage"
5756
58−serverSatisfies :: HasGenRequest a =>
59− Proxy a -> BaseUrl -> Args -> Predicates n b Bool -> Expectation
57+serverSatisfies :: (HasGenRequest a) =>
58+ Proxy a -> BaseUrl -> Args -> Predicates Text [Text] -> Expectation
6059serverSatisfies api burl args preds = do
6160 let reqs = ($ burl) <$> genRequest api
6261 r <- quickCheckWithResult args $ monadicIO $ forAllM reqs $ \req -> do
6362 v <- run $ finishPredicates preds req
64− assert v
63+ assert $ null v
6564 case r of
6665 Success {} -> return ()
6766 GaveUp { numTests = n } -> expectationFailure $ "Gave up after " ++ show n ++ " tests"
test/Servant/QuickCheck/InternalSpec.hs+12−1
@@ -14,6 +14,7 @@ import Servant.QuickCheck
1414spec :: Spec
1515spec = do
1616 serversEqualSpec
17+ serverSatisfiesSpec
1718
1819serversEqualSpec :: Spec
1920serversEqualSpec = describe "serversEqual" $ do
@@ -21,9 +22,16 @@ serversEqualSpec = describe "serversEqual" $ do
2122 it "considers equal servers equal" $ do
2223 withServantServer api server $ \burl1 ->
2324 withServantServer api server $ \burl2 -> do
24− serversEqual api burl1 burl2 stdArgs { maxSuccess = noOfTestCases } bodyEquality
25+ serversEqual api burl1 burl2 args bodyEquality
2526
2627
28+serverSatisfiesSpec :: Spec
29+serverSatisfiesSpec = describe "serverSatisfies" $ do
30+
31+ it "succeeds for true predicates" $ do
32+ withServantServer api server $ \burl ->
33+ serverSatisfies api burl args (not500 <%> mempty)
34+
2735
2836------------------------------------------------------------------------------
2937-- APIs
@@ -46,6 +54,9 @@ server = do
4654-- Utils
4755------------------------------------------------------------------------------
4856
57+args :: Args
58+args = stdArgs { maxSuccess = noOfTestCases }
59+
4960noOfTestCases :: Int
5061#if LONG_TESTS
5162noOfTestCases = 20000