-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #448 from symbiont-io/journal-assert
feat(journal): Add Assert module that can be controlled by cabal flag…
- Loading branch information
Showing
4 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Assert (assert, assertM, assertIO) where | ||
|
||
import Control.Exception (throw, AssertionFailed(AssertionFailed)) | ||
import GHC.Stack (HasCallStack, callStack, prettyCallStack) | ||
|
||
assert :: HasCallStack => Bool -> a -> a | ||
assert True x = x | ||
assert False _ = throw (AssertionFailed errMsg) | ||
where | ||
errMsg = concat | ||
[ "Assertion failed\n" | ||
, prettyCallStack callStack | ||
] | ||
|
||
assertM :: (HasCallStack, Monad m) => Bool -> m () | ||
assertM b = do | ||
if b | ||
then pure () | ||
else throw (AssertionFailed errMsg) | ||
where | ||
errMsg = concat | ||
[ "Assertion failed\n" | ||
, prettyCallStack callStack | ||
] | ||
|
||
assertIO :: HasCallStack => IO Bool -> IO () | ||
assertIO mb = do | ||
b <- mb | ||
if b | ||
then pure () | ||
else throw (AssertionFailed errMsg) | ||
where | ||
errMsg = concat | ||
[ "Assertion failed\n" | ||
, prettyCallStack callStack | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Assert (assert, assertM, assertIO) where | ||
|
||
import GHC.Stack (HasCallStack) | ||
|
||
{-# INLINE assert #-} | ||
assert :: Bool -> a -> a | ||
assert _ = id | ||
|
||
{-# INLINE assertM #-} | ||
assertM :: (Monad m) => Bool -> m () | ||
assertM _ = pure () | ||
|
||
{-# INLINE assertIO #-} | ||
assertIO :: IO Bool -> IO () | ||
assertIO _ = pure () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters