diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index eced072c7..4a7da1ab2 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -117,13 +117,20 @@ module Yesod.Test , yesodSpecApp , YesodExample , YesodExampleData(..) + , yesodExampleDataFromApp + , yesodExampleDataFromTestApp , TestApp , YSpec , testApp + , minimalTestApp , YesodSpecTree (..) , ydescribe , yit + -- * Hooks + , beforeApp + , beforeWithApp + -- * Modify test site , testModifySite @@ -220,9 +227,11 @@ module Yesod.Test , htmlQuery , parseHTML , withResponse + -- * YesodExa ) where import qualified Test.Hspec.Core.Spec as Hspec +import qualified Test.Hspec.Core.Hooks as Hspec import qualified Data.List as DL import qualified Data.ByteString.Char8 as BS8 import Data.ByteString (ByteString) @@ -287,7 +296,7 @@ import Yesod.Test.Internal (getBodyTextPreview, contentTypeHeaderIsUtf8) -- | The state used in a single test case defined using 'yit' -- --- Since 1.2.4 +-- @since 1.2.4 data YesodExampleData site = YesodExampleData { yedApp :: !Application , yedSite :: !site @@ -295,6 +304,29 @@ data YesodExampleData site = YesodExampleData , yedResponse :: !(Maybe SResponse) } +-- | +-- +-- @since TODO +yesodExampleDataFromApp :: YesodDispatch site => site -> IO (YesodExampleData site) +yesodExampleDataFromApp site = do + app <- toWaiAppPlain site + pure YesodExampleData + { yedApp = app + , yedSite = site + , yedCookies = M.empty + , yedResponse = Nothing + } + +-- | +-- +-- @since TODO +yesodExampleDataFromTestApp :: YesodDispatch site => TestApp site -> IO (YesodExampleData site) +yesodExampleDataFromTestApp (site, middleware) = do + yed <- yesodExampleDataFromApp site + pure yed + { yedApp = middleware (yedApp yed) + } + -- | A single test case, to be run with 'yit'. -- -- Since 1.2.0 @@ -366,13 +398,8 @@ yesodSpec site yspecs = where unYesod (YesodSpecGroup x y) = Hspec.specGroup x $ map unYesod y unYesod (YesodSpecItem x y) = Hspec.specItem x $ do - app <- toWaiAppPlain site - evalSIO y YesodExampleData - { yedApp = app - , yedSite = site - , yedCookies = M.empty - , yedResponse = Nothing - } + yed <- yesodExampleDataFromApp site + evalSIO y yed -- | Same as yesodSpec, but instead of taking already built site it -- takes an action which produces site for each test. @@ -397,13 +424,8 @@ yesodSpecWithSiteGeneratorAndArgument getSiteAction yspecs = unYesod getSiteAction' (YesodSpecGroup x y) = Hspec.specGroup x $ map (unYesod getSiteAction') y unYesod getSiteAction' (YesodSpecItem x y) = Hspec.specItem x $ \a -> do site <- getSiteAction' a - app <- toWaiAppPlain site - evalSIO y YesodExampleData - { yedApp = app - , yedSite = site - , yedCookies = M.empty - , yedResponse = Nothing - } + yed <- yesodExampleDataFromApp site + evalSIO y yed -- | Same as yesodSpec, but instead of taking a site it -- takes an action which produces the 'Application' for each test. @@ -431,7 +453,7 @@ yit :: String -> YesodExample site () -> YesodSpec site yit label example = tell [YesodSpecItem label example] -- | Modifies the site ('yedSite') of the test, and creates a new WAI app ('yedApp') for it. --- +-- -- yesod-test allows sending requests to your application to test that it handles them correctly. -- In rare cases, you may wish to modify that application in the middle of a test. -- This may be useful if you wish to, for example, test your application under a certain configuration, @@ -455,7 +477,7 @@ testModifySite :: YesodDispatch site => (site -> IO (site, Middleware)) -- ^ A function from the existing site, to a new site and middleware for a WAI app. -> YesodExample site () testModifySite newSiteFn = do - currentSite <- getTestYesod + currentSite <- getTestYesod (newSite, middleware) <- liftIO $ newSiteFn currentSite app <- liftIO $ toWaiAppPlain newSite modifySIO $ \yed -> yed { yedSite = newSite, yedApp = middleware app } @@ -812,7 +834,7 @@ printMatches query = do matches <- htmlQuery query liftIO $ hPutStrLn stderr $ show matches --- | Add a parameter with the given name and value to the request body. +-- | Add a parameter with the given name and value to the request body. -- This function can be called multiple times to add multiple parameters, and be mixed with calls to 'addFile'. -- -- "Post parameter" is an informal description of what is submitted by making an HTTP POST with an HTML @\