mockery 0.1.0 → 0.2.0
raw patch · 3 files changed
+63/−1 lines, 3 filesdep +logging-facadePVP ok
version bump matches the API change (PVP)
Dependencies added: logging-facade
API changes (from Hackage documentation)
+ Test.Mockery.Logging: captureLogMessages :: IO a -> IO ([(LogLevel, String)], a)
+ Test.Mockery.Logging: captureLogMessages_ :: IO a -> IO [(LogLevel, String)]
Files
- mockery.cabal +5/−1
- src/Test/Mockery/Logging.hs +29/−0
- test/Test/Mockery/LoggingSpec.hs +29/−0
mockery.cabal view
@@ -3,7 +3,7 @@ -- see: https://github.com/sol/hpack name: mockery-version: 0.1.0+version: 0.2.0 synopsis: Support functions for automated testing description: Support functions for automated testing category: Testing@@ -24,10 +24,12 @@ hs-source-dirs: src exposed-modules: Test.Mockery.Directory+ Test.Mockery.Logging build-depends: base == 4.* , temporary , directory+ , logging-facade ghc-options: -Wall default-language: Haskell2010 @@ -37,10 +39,12 @@ main-is: Spec.hs other-modules: Test.Mockery.DirectorySpec+ Test.Mockery.LoggingSpec build-depends: base == 4.* , temporary , directory+ , logging-facade , mockery , hspec == 2.*
+ src/Test/Mockery/Logging.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE RecordWildCards #-}+module Test.Mockery.Logging (+ captureLogMessages+, captureLogMessages_+) where++import Control.Applicative+import Control.Exception+import Data.IORef+import System.Logging.Facade.Types+import System.Logging.Facade.Sink++-- | Capture all log messages produced by an IO action.+-- Logs are kept in memory.+captureLogMessages :: IO a -> IO ([(LogLevel, String)], a)+captureLogMessages action = bracket getLogSink setLogSink act+ where+ logToRef ref record = atomicModifyIORef' ref $ \logs -> (record : logs, ())+ unwrap LogRecord{..} = (logRecordLevel, logRecordMessage)+ act _ = do+ ref <- newIORef []+ setLogSink $ logToRef ref+ val <- action+ logs <- readIORef ref+ return (unwrap <$> reverse logs, val)++-- | Like 'captureLogsMessages', but ignores the result.+captureLogMessages_ :: IO a -> IO [(LogLevel, String)]+captureLogMessages_ action = fst <$> captureLogMessages action
+ test/Test/Mockery/LoggingSpec.hs view
@@ -0,0 +1,29 @@+module Test.Mockery.LoggingSpec (spec) where++import Test.Hspec+import Data.IORef+import System.Logging.Facade.Types+import System.Logging.Facade.Sink+import System.Logging.Facade as Log++import Test.Mockery.Logging++spec :: Spec+spec = describe "captureLogs" $ do+ let logToIORef :: IORef [LogRecord] -> LogSink+ logToIORef ref record = modifyIORef ref (record :)++ it "returns all log messages of an action" $ do+ (logs, ()) <- captureLogMessages $ do+ Log.trace "this should be captured"+ Log.trace "this should be captured next"+ logs `shouldBe` [ (TRACE, "this should be captured")+ , (TRACE, "this should be captured next")+ ]++ it "restores the original log sink" $ do+ ref <- newIORef []+ setLogSink $ logToIORef ref+ _ <- captureLogMessages $ Log.trace "this should be captured"+ Log.trace "this should not be captured"+ readIORef ref `shouldReturn` [LogRecord TRACE Nothing "this should not be captured"]