| Copyright | (c) 2026 Institute for Digital Autonomy |
|---|---|
| License | EUPL-1.2 |
| Maintainer | IDA |
| Safe Haskell | None |
| Language | Haskell2010 |
Effectful.HUnit
Description
Effectful bindings for the HUnit library.
Overview
This library provides HUnit's Assertion and Test types, along with all the standard
test combinators and operators, expressed in terms of the HUnit effect.
This effect allows you to intersperse test assertions with arbitrary other effects.
Example usage
Suppose we wish to test stateful operations on an account balance:
deposit :: (State Int :> es) => Int -> Eff es () deposit amount = modify (+ amount) balance :: (State Int :> es) => Eff es Int balance = get
withdraw :: (State Int :> es) => Int -> Eff es Bool
withdraw amount = do
funds <- balance
if funds < amount
then pure False
else do
put $ funds - amount
pure TrueUse TestCase to define individual unit tests, and TestLabel to give them a description:
depositsAccumulate :: (State Int :> es, HUnit :> es) => Test es
depositsAccumulate = TestLabel "deposits accumulate" . TestCase $ do
deposit 100
deposit 50
funds <- balance
funds @?= 150
withdrawalsAreChecked :: (State Int :> es, HUnit :> es) => Test es
withdrawalsAreChecked = TestLabel "withdrawals are checked" . TestCase $ do
withdraw 30 >>= (@? "Insufficient funds")
funds <- balance
funds @?= 120Group multiple test cases with TestList:
accountTests :: (State Int :> es, HUnit :> es) => Test es accountTests = TestLabel "account tests" $ TestList [depositsAccumulate, withdrawalsAreChecked]
Finally, run the tests with runTestTTAndExit, and use runHUnit to resolve the effect:
main :: IO () main = runEff . runHUnit . evalState @Int 0 $ runTestTTAndExit accountTests
Alternatively, use runTestTT to handle the test results manually.
Synopsis
- data HUnit (a :: Type -> Type) b
- runHUnit :: forall (es :: [Effect]) a. IOE :> es => Eff (HUnit ': es) a -> Eff es a
- data Test (es :: [Effect])
- (~=?) :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) => a -> a -> Test es
- (~?=) :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) => a -> a -> Test es
- (~:) :: forall (es :: [Effect]) t. (HasCallStack, Testable es t) => String -> t -> Test es
- (~?) :: forall (es :: [Effect]) t. (HasCallStack, AssertionPredicable es t, HUnit :> es) => t -> String -> Test es
- type Assertion (es :: [Effect]) = Eff es ()
- assertFailure :: forall (es :: [Effect]) a. (HasCallStack, HUnit :> es) => String -> Eff es a
- assertBool :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) => String -> Bool -> Assertion es
- assertEqual :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) => String -> a -> a -> Assertion es
- assertString :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) => String -> Assertion es
- (@=?) :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) => a -> a -> Assertion es
- (@?=) :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) => a -> a -> Assertion es
- (@?) :: forall (es :: [Effect]) t. (HasCallStack, AssertionPredicable es t, HUnit :> es) => t -> String -> Assertion es
- runTestTT :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) => Test es -> Eff es Counts
- runTestTTAndExit :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) => Test es -> Eff es ()
- class Assertable (es :: [Effect]) t where
- class ListAssertable (es :: [Effect]) t where
- listAssert :: [t] -> Assertion es
- type AssertionPredicate (es :: [Effect]) = Eff es Bool
- class AssertionPredicable (es :: [Effect]) t where
- assertionPredicate :: t -> AssertionPredicate es
- class Testable (es :: [Effect]) t where
- data Counts = Counts {}
Effect
data HUnit (a :: Type -> Type) b Source #
Instances
| type DispatchOf HUnit Source # | |
Defined in Effectful.HUnit | |
| newtype StaticRep HUnit Source # | |
Declaring tests
data Test (es :: [Effect]) Source #
The basic structure used to create an annotated tree of test cases.
Arguments
| :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) | |
| => a | The expected value |
| -> a | The actual value |
| -> Test es |
Shorthand for a test case that asserts equality.
Arguments
| :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) | |
| => a | The actual value |
| -> a | The expected value |
| -> Test es |
Shorthand for a test case that asserts equality.
(~:) :: forall (es :: [Effect]) t. (HasCallStack, Testable es t) => String -> t -> Test es infixr 0 Source #
Arguments
| :: forall (es :: [Effect]) t. (HasCallStack, AssertionPredicable es t, HUnit :> es) | |
| => t | A value of which the asserted condition is predicated |
| -> String | A message that is displayed on test failure |
| -> Test es |
Creates a test case resulting from asserting the condition obtained
from the specified AssertionPredicable.
Making assertions
assertFailure :: forall (es :: [Effect]) a. (HasCallStack, HUnit :> es) => String -> Eff es a Source #
Unconditionally signals that a failure has occurred.
Arguments
| :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) | |
| => String | The message that is displayed if the assertion fails |
| -> Bool | The condition |
| -> Assertion es |
Asserts that the specified condition holds.
Arguments
| :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) | |
| => String | The message that is displayed with the assertion failure |
| -> Assertion es |
Signals an assertion failure if a non-empty message (i.e., a message
other than "") is passed.
Arguments
| :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) | |
| => a | The expected value |
| -> a | The actual value |
| -> Assertion es |
Asserts that the specified actual value is equal to the expected value.
Arguments
| :: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) | |
| => a | The actual value |
| -> a | The expected value |
| -> Assertion es |
Asserts that the specified actual value is equal to the expected value.
Arguments
| :: forall (es :: [Effect]) t. (HasCallStack, AssertionPredicable es t, HUnit :> es) | |
| => t | A value of which the asserted condition is predicated |
| -> String | A message that is displayed if the assertion fails |
| -> Assertion es |
Asserts that the condition obtained from the specified
AssertionPredicable holds.
Running tests
runTestTT :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) => Test es -> Eff es Counts Source #
Lifted runTestTT.
runTestTTAndExit :: forall (es :: [Effect]). (HasCallStack, HUnit :> es) => Test es -> Eff es () Source #
Lifted runTestTTAndExit.
Extending the assertion functionality
class Assertable (es :: [Effect]) t where Source #
Instances
| Assertable es () Source # | |
Defined in Effectful.HUnit | |
| HUnit :> es => Assertable es Bool Source # | |
| Assertable es (Assertion es) Source # | |
| ListAssertable es t => Assertable es [t] Source # | |
Defined in Effectful.HUnit | |
| Assertable es t => Assertable es (Eff es t) Source # | |
class ListAssertable (es :: [Effect]) t where Source #
A specialised form of Assertable to handle lists.
Methods
listAssert :: [t] -> Assertion es Source #
Instances
| HUnit :> es => ListAssertable es Char Source # | |
Defined in Effectful.HUnit Methods listAssert :: [Char] -> Assertion es Source # | |
class AssertionPredicable (es :: [Effect]) t where Source #
Methods
assertionPredicate :: t -> AssertionPredicate es Source #
Instances
| AssertionPredicable es Bool Source # | |
Defined in Effectful.HUnit Methods assertionPredicate :: Bool -> AssertionPredicate es Source # | |
| AssertionPredicable es t => AssertionPredicable es (Eff es t) Source # | |
Defined in Effectful.HUnit Methods assertionPredicate :: Eff es t -> AssertionPredicate es Source # | |
class Testable (es :: [Effect]) t where Source #
Provides a way to convert data into a Test or set of Test.
Instances
| Assertable es t => Testable es t Source # | |
Defined in Effectful.HUnit | |
| Testable es (Test es) Source # | |
| Testable es t => Testable es [t] Source # | |
Defined in Effectful.HUnit | |