Skip to content

Commit

Permalink
[#47] Add doctest tests to co-log-core package (#55)
Browse files Browse the repository at this point in the history
* [#47] Add `doctest` tests to `co-log-core` package

* Fix warning

* Fix review and format issues

* Add doctests for Colog.Core.IO module
  • Loading branch information
lucazulian authored and chshersh committed Oct 11, 2018
1 parent ae78cd5 commit 2541b61
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions co-log-core/co-log-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ library
other-extensions: CPP

test-suite doctest
build-depends: base, doctest ^>= 0.16.0
default-language: Haskell2010
hs-source-dirs: tests
main-is: doctest.hs
type: exitcode-stdio-1.0
build-depends: base, doctest ^>= 0.16.0
default-language: Haskell2010
hs-source-dirs: test
main-is: Doctests.hs
type: exitcode-stdio-1.0
1 change: 0 additions & 1 deletion co-log-core/src/Colog/Core/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ foo.g.f2
>>> unLogAction (logToStdout =>> f =>> g) "foo"
foo.g.f1
foo.g.f2
-}
extend :: Semigroup msg => (LogAction m msg -> m ()) -> LogAction m msg -> LogAction m msg
extend f (LogAction action) = LogAction $ \m -> f $ LogAction $ \m' -> action (m <> m')
Expand Down
27 changes: 23 additions & 4 deletions co-log-core/src/Colog/Core/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,27 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
import System.IO (Handle, IOMode( AppendMode ), hPutStrLn, stderr, withFile)
import Colog.Core.Action (LogAction (..))

{- | Action that prints 'String' to stdout.
{- | Action that prints 'String' to stdout. -}
>>> unLogAction logStringStdout "foo"
foo
-}
logStringStdout :: MonadIO m => LogAction m String
logStringStdout = LogAction (liftIO . putStrLn)

{- | Action that prints 'String' to stderr. -}
{- | Action that prints 'String' to stderr.
>>> unLogAction logStringStderr "foo"
foo
-}
logStringStderr :: MonadIO m => LogAction m String
logStringStderr = logStringHandle stderr

{- | Action that prints 'String' to 'Handle'. -}
{- | Action that prints 'String' to 'Handle'.
>>> (unLogAction . logStringHandle) stderr "foo"
foo
-}
logStringHandle :: MonadIO m => Handle -> LogAction m String
logStringHandle handle = LogAction $ liftIO . hPutStrLn handle

Expand All @@ -29,10 +40,18 @@ file only once at the start of the application and write to 'Handle' instead of
opening file each time we need to write to it.
Opens file in 'AppendMode'.
>>> logger action = unLogAction action "foo"
>>> withLogStringFile "/dev/stdout" logger
foo
-}
withLogStringFile :: MonadIO m => FilePath -> (LogAction m String -> IO r) -> IO r
withLogStringFile path action = withFile path AppendMode $ action . logStringHandle

{- | Lifts a LogAction over IO into a more general Monad. -}
{- | Lifts a LogAction over IO into a more general Monad.
>>> logToStdout = LogAction putStrLn
>>> unLogAction (liftLogIO logToStdout) "foo"
foo
-}
liftLogIO :: MonadIO m => LogAction IO msg -> LogAction m msg
liftLogIO (LogAction action) = LogAction (liftIO . action)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module Main
import Test.DocTest (doctest)

main :: IO ()
main = doctest ["-XInstanceSigs", "src"]
main = doctest ["-XInstanceSigs", "src"]

0 comments on commit 2541b61

Please sign in to comment.