@@ -1,135 +1,170 @@
1+{-# LANGUAGE DerivingVia #-}
2+{-# LANGUAGE LambdaCase #-}
13{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE StandaloneDeriving #-}
34{-# LANGUAGE RecordWildCards #-}
5+{-# LANGUAGE StandaloneDeriving #-}
46{-# LANGUAGE TypeApplications #-}
5−{-# LANGUAGE LambdaCase #-}
6−{-# LANGUAGE DerivingVia #-}
7−
7+{-# LANGUAGE RankNTypes #-}
8+{-# LANGUAGE FlexibleContexts #-}
9+{-# LANGUAGE AllowAmbiguousTypes #-}
10+{-# LANGUAGE ScopedTypeVariables #-}
11+{-# LANGUAGE DataKinds #-}
812{-# OPTIONS_GHC -fno-warn-orphans #-}
13+{-# LANGUAGE TypeFamilies #-}
14+
15+import qualified Breakdown
16+import Data.Dynamic (toDyn)
17+import Data.Hashable (Hashable (hash))
18+import Data.Maybe (isNothing)
19+import Data.Void (Void)
920import qualified Foo
10−import qualified Seeded
11−import qualified Valid
1221import qualified Headers
22+import qualified Nested
1323import qualified Post
24+import qualified Put
1425import qualified Product
15−import qualified Breakdown
16−
17−import Test.Hspec.Core.Spec(ResultStatus(Failure,Success),resultStatus,itemExample,FailureReason(Reason),mapSpecItem_)
18−import Data.Dynamic(toDyn)
19−import qualified Roboservant as RS
26+import qualified QueryParams
27+import qualified Roboservant as R
28+import qualified Roboservant.Server as RS
29+import qualified Roboservant.Client as RC
30+import qualified Seeded
2031import Test.Hspec
21−import Data.Void ( Void )
22−import Data.Maybe ( isNothing )
23−import Data.Hashable ( Hashable(hash) )
32+import Test.Hspec.Core.Spec (FailureReason (Reason), ResultStatus (Failure, Success), itemExample, mapSpecItem_, resultStatus)
33+import qualified Valid
34+import Servant ( Server, Proxy(..), serve, Endpoints, HasServer )
35+
36+import Servant.Client(ClientEnv, mkClientEnv, baseUrlPort, parseBaseUrl,HasClient,ClientM)
37+import Network.Wai(Application)
38+import qualified Network.Wai.Handler.Warp as Warp
39+import Network.HTTP.Client (newManager, defaultManagerSettings)
40+import Control.Monad((>=>))
2441
2542main :: IO ()
2643main = hspec spec
2744
28−noCheck :: IO ()
29−noCheck = pure ()
30−
31−defaultConfig :: RS.Config
32−defaultConfig = RS.Config
33− { RS.seed = []
34− , RS.maxRuntime = 0.5
35− , RS.maxReps = 1000
36− , RS.rngSeed = 0
37− , RS.coverageThreshold = 0
38− }
45+fuzzBoth
46+ :: forall a .
47+ (R.ToReifiedApi (Endpoints a), HasServer a '[], RS.FlattenServer a, RC.ToReifiedClientApi (Endpoints a), RC.FlattenClient a,
48+ HasClient ClientM a)
49+ => String -> Server a -> R.Config -> (Maybe R.Report -> IO ()) -> Spec
50+fuzzBoth name server config condition = do
51+ it (name <> " via server") $
52+ RS.fuzz @a server config >>= condition
53+
54+ around (withServer (serve (Proxy :: Proxy a) server)) $
55+ it (name <> " via client") $ \(clientEnv::ClientEnv) -> do
56+ RC.fuzz @a clientEnv config >>= condition
57+
58+withServer :: Application -> ActionWith ClientEnv -> IO ()
59+withServer app action = Warp.testWithApplication (pure app) (genClientEnv >=> action)
60+ where genClientEnv port = do
61+ baseUrl <- parseBaseUrl "http://localhost"
62+ manager <- newManager defaultManagerSettings
63+ pure $ mkClientEnv manager (baseUrl { baseUrlPort = port })
3964
4065spec :: Spec
4166spec = do
4267 describe "Basic usage" $ do
4368 describe "noError" $ do
44− it "finds no error in a valid app" $ do
45− RS.fuzz @Valid.Api Valid.server defaultConfig noCheck
46− >>= (`shouldSatisfy` isNothing)
47− it "finds no error in a valid generic app" $ do
48− RS.fuzz @Valid.RoutedApi Valid.routedServer defaultConfig noCheck
49− >>= (`shouldSatisfy` isNothing)
50−
51− it "does fail coverage check" $ do
52− r <- RS.fuzz @Valid.Api Valid.server defaultConfig { RS.coverageThreshold = 0.6 } noCheck
53− fmap (RS.failureReason . RS.rsException) r `shouldSatisfy`
54− (\case
55− Just (RS.InsufficientCoverage _) -> True
56− _ -> False)
57−
58− describe "posted body" $ do
59− it "passes a coverage check using a posted body" $ do
60− RS.fuzz @Post.Api Post.server defaultConfig { RS.coverageThreshold = 0.99 } noCheck
61− >>= (`shouldSatisfy` isNothing)
69+ fuzzBoth @Valid.Api "find no error in a basic app" Valid.server R.defaultConfig (`shouldSatisfy` isNothing)
70+ fuzzBoth @Valid.RoutedApi "finds no error in a valid generic app" Valid.routedServer R.defaultConfig (`shouldSatisfy` isNothing)
71+ fuzzBoth @Valid.Api "fails coverage check" Valid.server R.defaultConfig {R.coverageThreshold = 0.6}
72+ (\r ->
73+ fmap (R.failureReason . R.rsException) r
74+ `shouldSatisfy` ( \case
75+ Just (R.InsufficientCoverage _) -> True
76+ _ -> False
77+ ))
78+ describe "posted body" $
79+ fuzzBoth @Post.Api "passes a coverage check using a posted body" Post.server R.defaultConfig {R.coverageThreshold = 0.99}
80+ (`shouldSatisfy` isNothing)
6281
63− describe "seeded" $ do
64− shouldFail $
65− it "finds an error using information passed in" $
66− let res = Seeded.Seed 1 in
67− RS.fuzz @Seeded.Api Seeded.server (defaultConfig{ RS.seed = [(toDyn res,hash res) ] }) noCheck
68− >>= (`shouldSatisfy` isNothing)
69− describe "Foo" $ do
70− it "finds an error in a basic app" $
71− RS.fuzz @Foo.Api Foo.server defaultConfig noCheck
72− >>= (`shouldSatisfy` serverFailure)
7382
74− describe "BuildFrom" $ do
75− describe "headers (and sum types)" $ do
76− it "should find a failure that's dependent on using header info" $ do
77− RS.fuzz @Headers.Api Headers.server defaultConfig noCheck
78− >>= (`shouldSatisfy` serverFailure)
83+ describe "PUTted body" $
84+ fuzzBoth @Put.Api "passes a coverage check using a posted body" Put.server R.defaultConfig {R.coverageThreshold = 0.99}
85+ (`shouldSatisfy` isNothing)
7986
80− describe "product types" $ do
81− it "should find a failure that's dependent on creating a product" $ do
82− RS.fuzz @Product.Api Product.server defaultConfig { RS.seed = [RS.hashedDyn 'a', RS.hashedDyn (1::Int)]} noCheck
83− >>= (`shouldSatisfy` serverFailure)
8487
85− describe "Breakdown" $ do
86− it "handles products" $ do
87− RS.fuzz @Breakdown.ProductApi Breakdown.productServer defaultConfig noCheck
88− >>= (`shouldSatisfy` serverFailure)
88+ describe "seeded" $ do
89+ let res = Seeded.Seed 1
90+ shouldFail $ fuzzBoth @Seeded.Api "finds an error using information passed in" Seeded.server
91+ (R.defaultConfig {R.seed = [(toDyn res, hash res)]})
92+ (`shouldSatisfy` isNothing)
8993
90− it "handles sums" $ do
91− RS.fuzz @Breakdown.SumApi Breakdown.sumServer defaultConfig noCheck
92− >>= (`shouldSatisfy` serverFailure)
94+ describe "Foo" $
95+ fuzzBoth @Foo.Api "finds an error in a basic app" Foo.server R.defaultConfig (`shouldSatisfy` serverFailure)
9396
97+ describe "QueryParams" $
98+ fuzzBoth @QueryParams.Api "can handle query params" QueryParams.server R.defaultConfig { R.seed = [R.hashedDyn (12::Int)] }
99+ (`shouldSatisfy` isNothing)
94100
95−serverFailure :: Maybe RS.Report -> Bool
101+ describe "BuildFrom" $ do
102+ describe "headers (and sum types)" $
103+ fuzzBoth @Headers.Api "should find a failure that's dependent on using header info" Headers.server R.defaultConfig
104+ (`shouldSatisfy` serverFailure)
105+ describe "product types" $
106+ fuzzBoth @Product.Api "should find a failure that's dependent on creating a product" Product.server
107+ R.defaultConfig {R.seed = [R.hashedDyn 'a', R.hashedDyn (1 :: Int)]}
108+ (`shouldSatisfy` serverFailure)
109+ describe "Breakdown" $ do
110+ fuzzBoth @Breakdown.ProductApi "handles products" Breakdown.productServer R.defaultConfig
111+ (`shouldSatisfy` serverFailure)
112+ fuzzBoth @Breakdown.SumApi "handles sums" Breakdown.sumServer R.defaultConfig
113+ (`shouldSatisfy` serverFailure)
114+ describe "flattening" $
115+ fuzzBoth @Nested.FlatApi "can handle nested apis" Nested.server R.defaultConfig {R.coverageThreshold = 0.99}
116+ (`shouldSatisfy` isNothing)
117+
118+serverFailure :: Maybe R.Report -> Bool
96119serverFailure = \case
97− Just RS.Report{..} ->
98− let RS.RoboservantException{..} = rsException
99− in failureReason /= RS.NoPossibleMoves
120+ Just R.Report {..} ->
121+ let R.RoboservantException {..} = rsException
122+ in failureReason /= R.NoPossibleMoves
100123 _ -> False
101124
125+deriving via (R.Atom Foo.Foo) instance R.Breakdown Foo.Foo
126+
127+deriving via (R.Atom Foo.Foo) instance R.BuildFrom Foo.Foo
128+
129+deriving via (R.Atom Headers.Foo) instance R.Breakdown Headers.Foo
130+
131+deriving via (R.Atom Headers.Foo) instance R.BuildFrom Headers.Foo
132+
133+deriving via (R.Atom Seeded.Seed) instance R.Breakdown Seeded.Seed
134+
135+deriving via (R.Atom Seeded.Seed) instance R.BuildFrom Seeded.Seed
102136
103−deriving via (RS.Atom Foo.Foo) instance RS.Breakdown Foo.Foo
104−deriving via (RS.Atom Foo.Foo) instance RS.BuildFrom Foo.Foo
137+deriving via (R.Atom Void) instance R.BuildFrom Void
105138
106−deriving via (RS.Atom Headers.Foo) instance RS.Breakdown Headers.Foo
107−deriving via (RS.Atom Headers.Foo) instance RS.BuildFrom Headers.Foo
139+deriving via (R.Atom Post.FooPost) instance R.Breakdown Post.FooPost
140+deriving via (R.Atom Post.FooPost) instance R.BuildFrom Post.FooPost
108141
109−deriving via (RS.Atom Seeded.Seed) instance RS.Breakdown Seeded.Seed
110−deriving via (RS.Atom Seeded.Seed) instance RS.BuildFrom Seeded.Seed
142+deriving via (R.Atom Put.Foo) instance R.Breakdown Put.Foo
143+deriving via (R.Atom Put.Foo) instance R.BuildFrom Put.Foo
111144
112145
113−-- instance RS.BuildFrom Seeded.Seed
114146
115−deriving via (RS.Atom Void) instance RS.BuildFrom Void
147+deriving via (R.Compound Breakdown.Foo) instance R.Breakdown Breakdown.Foo
116148
117−deriving via (RS.Atom Post.FooPost) instance RS.BuildFrom Post.FooPost
118−deriving via (RS.Atom Post.FooPost) instance RS.Breakdown Post.FooPost
149+deriving via (R.Compound Product.Foo) instance R.BuildFrom Product.Foo
119150
120−deriving via (RS.Compound Product.Foo) instance RS.BuildFrom Product.Foo
121−deriving via (RS.Compound Breakdown.Foo) instance RS.Breakdown Breakdown.Foo
122−deriving via (RS.Compound Breakdown.SomeSum) instance RS.Breakdown Breakdown.SomeSum
151+deriving via (R.Compound Breakdown.SomeSum) instance R.Breakdown Breakdown.SomeSum
123152
124153-- | `shouldFail` allows you to assert that a given `Spec` should contain at least one failing test.
125154-- this is often useful when testing tests.
126155shouldFail :: SpecWith a -> SpecWith a
127−shouldFail = mapSpecItem_ (\i -> i {
128− itemExample = \p a cb -> do
129− r <- (itemExample i) p a cb
130− pure r {resultStatus = case resultStatus r of
131− Success -> Failure Nothing (Reason "Unexpected success")
132− Failure _ _ -> Success
133− x -> x
134− }
135− })
156+shouldFail =
157+ mapSpecItem_
158+ ( \i ->
159+ i
160+ { itemExample = \p a cb -> do
161+ r <- itemExample i p a cb
162+ pure
163+ r
164+ { resultStatus = case resultStatus r of
165+ Success -> Failure Nothing (Reason "Unexpected success")
166+ Failure _ _ -> Success
167+ x -> x
168+ }
169+ }
170+ )