hunit-effectful-1.0.0: Effectful driver for HUnit
Copyright(c) 2026 Institute for Digital Autonomy
LicenseEUPL-1.2
MaintainerIDA
Safe HaskellNone
LanguageHaskell2010

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 True

Use 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 @?= 120

Group 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

Effect

data HUnit (a :: Type -> Type) b Source #

Instances

Instances details
type DispatchOf HUnit Source # 
Instance details

Defined in Effectful.HUnit

newtype StaticRep HUnit Source # 
Instance details

Defined in Effectful.HUnit

newtype StaticRep HUnit = HUnit (forall (es :: [Effect]) a. IO a -> Eff es a)

runHUnit :: forall (es :: [Effect]) a. IOE :> es => Eff (HUnit ': es) a -> Eff es a Source #

Declaring tests

data Test (es :: [Effect]) Source #

The basic structure used to create an annotated tree of test cases.

Constructors

TestCase (Assertion es)

A single, independent test case composed.

TestList [Test es]

A set of Tests sharing the same level in the hierarchy.

TestLabel String (Test es)

A name or description for a subtree of the Tests.

Instances

Instances details
Testable es (Test es) Source # 
Instance details

Defined in Effectful.HUnit

Methods

test :: Test es -> Test es Source #

Show (Test es) Source # 
Instance details

Defined in Effectful.HUnit

Methods

showsPrec :: Int -> Test es -> ShowS #

show :: Test es -> String #

showList :: [Test es] -> ShowS #

(~=?) infix 1 Source #

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.

(~?=) infix 1 Source #

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 #

Creates a test from the specified Testable, with the specified label attached to it.

Since Test is Testable, this can be used as a shorthand way of attaching a TestLabel to one or more tests.

(~?) infix 1 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

type Assertion (es :: [Effect]) = Eff es () Source #

assertFailure :: forall (es :: [Effect]) a. (HasCallStack, HUnit :> es) => String -> Eff es a Source #

Unconditionally signals that a failure has occurred.

assertBool Source #

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.

assertEqual Source #

Arguments

:: forall a (es :: [Effect]). (HasCallStack, Eq a, Show a, HUnit :> es) 
=> String

The message prefix

-> a

The expected value

-> a

The actual value

-> Assertion es 

assertString Source #

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.

(@=?) infix 1 Source #

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.

(@?=) infix 1 Source #

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.

(@?) infix 1 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 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 #

Extending the assertion functionality

class Assertable (es :: [Effect]) t where Source #

Methods

assert :: t -> Assertion es Source #

Instances

Instances details
Assertable es () Source # 
Instance details

Defined in Effectful.HUnit

Methods

assert :: () -> Assertion es Source #

HUnit :> es => Assertable es Bool Source # 
Instance details

Defined in Effectful.HUnit

Methods

assert :: Bool -> Assertion es Source #

Assertable es (Assertion es) Source # 
Instance details

Defined in Effectful.HUnit

Methods

assert :: Assertion es -> Assertion es Source #

ListAssertable es t => Assertable es [t] Source # 
Instance details

Defined in Effectful.HUnit

Methods

assert :: [t] -> Assertion es Source #

Assertable es t => Assertable es (Eff es t) Source # 
Instance details

Defined in Effectful.HUnit

Methods

assert :: Eff es t -> Assertion es Source #

class ListAssertable (es :: [Effect]) t where Source #

A specialised form of Assertable to handle lists.

Methods

listAssert :: [t] -> Assertion es Source #

Instances

Instances details
HUnit :> es => ListAssertable es Char Source # 
Instance details

Defined in Effectful.HUnit

Methods

listAssert :: [Char] -> Assertion es Source #

class AssertionPredicable (es :: [Effect]) t where Source #

Instances

Instances details
AssertionPredicable es Bool Source # 
Instance details

Defined in Effectful.HUnit

AssertionPredicable es t => AssertionPredicable es (Eff es t) Source # 
Instance details

Defined in Effectful.HUnit

class Testable (es :: [Effect]) t where Source #

Provides a way to convert data into a Test or set of Test.

Methods

test :: t -> Test es Source #

Instances

Instances details
Assertable es t => Testable es t Source # 
Instance details

Defined in Effectful.HUnit

Methods

test :: t -> Test es Source #

Testable es (Test es) Source # 
Instance details

Defined in Effectful.HUnit

Methods

test :: Test es -> Test es Source #

Testable es t => Testable es [t] Source # 
Instance details

Defined in Effectful.HUnit

Methods

test :: [t] -> Test es Source #

Re-eports from HUnit

data Counts #

A data structure that hold the results of tests that have been performed up until this point.

Constructors

Counts 

Fields

Instances

Instances details
Read Counts 
Instance details

Defined in Test.HUnit.Base

Show Counts 
Instance details

Defined in Test.HUnit.Base

Eq Counts 
Instance details

Defined in Test.HUnit.Base

Methods

(==) :: Counts -> Counts -> Bool #

(/=) :: Counts -> Counts -> Bool #