assert-0.0.1.1: Helpers for Control.Exception.assert

Safe HaskellNone

Control.Exception.Assert

Synopsis

Documentation

newtype Arse Source

A distict Exception from AssertionFailed, so that we stop fudging the exception message after the first mapException.

The name comes from abbreviating ‘assert’ and translating the result to my native tongue.

Constructors

Arse String 

assertMessage :: String -> String -> (a -> a) -> a -> aSource

Generic helper for assert that maps AssertFailure Exceptions to Arse, adding a descriptive message along the way. Use this to build your own assert helpers, such as byOrd. A rule is included which rewrites assertMessage to id when compiling with -fignore-asserts.

byEq :: (Eq x, Show x) => (Bool -> a -> a) -> String -> x -> x -> a -> aSource

Assert that two values are equal.

>>> byEq assert "Bool" False True ()
*** Exception: … Assertion failed "Bool", False ≠ True

byOrd :: (Ord x, Show x) => (Bool -> a -> a) -> String -> Ordering -> x -> x -> a -> aSource

Assert that two values obey the given Ordering.

>>> byOrd assert "Int" LT 0 1 ()
()

byPred :: Show x => (Bool -> a -> a) -> String -> (x -> Bool) -> x -> a -> aSource

Assert that a value satisfies the given predicate.

>>> byPred assert "Odd" odd 4 ()
*** Exception: … Assertion failed "Odd", 4

assert :: Bool -> a -> a

If the first argument evaluates to True, then the result is the second argument. Otherwise an AssertionFailed exception is raised, containing a String with the source file and line number of the call to assert.

Assertions can normally be turned on or off with a compiler flag (for GHC, assertions are normally on unless optimisation is turned on with -O or the -fignore-asserts option is given). When assertions are turned off, the first argument to assert is ignored, and the second argument is returned as the result.