cooked-validators
Safe HaskellSafe-Inferred
LanguageHaskell2010

Cooked.MockChain.Testing

Description

This modules provides primitives to run tests over mockchain executions and to provide requirements on the the number and results of these runs.

Synopsis

Common interface between HUnit and QuickCheck

class IsProp prop where Source #

IsProp is a common interface for HUnit and QuickCheck tests. It abstracts uses of Assertion and Property for (IsProp prop) => prop, then provide instances for both HU.Asserton and QC.Property.

Minimal complete definition

testCounterexample, testConjoin, testDisjoin

Methods

testCounterexample :: String -> prop -> prop Source #

Displays the string to the user in case of failure

testConjoin :: [prop] -> prop Source #

Conjunction of a number of results

testDisjoin :: [prop] -> prop Source #

Disjunction of a number of results

testFailure :: prop Source #

Flags a failure

testSuccess :: prop Source #

Flags a success

testFailureMsg :: String -> prop Source #

Flags a failure with a message

testBool :: IsProp prop => Bool -> prop Source #

Turns a boolean into a prop

testAll :: IsProp prop => (a -> prop) -> [a] -> prop Source #

Ensures all elements of a list satisfy a given prop

testAny :: IsProp prop => (a -> prop) -> [a] -> prop Source #

Ensures at least one element of a list satisfy a given prop

(.==.) :: (IsProp prop, Eq a) => a -> a -> prop infix 4 Source #

Lifts an equality test to a prop

(.&&.) :: IsProp prop => prop -> prop -> prop infixr 3 Source #

Conjunction of two props

(.||.) :: IsProp prop => prop -> prop -> prop infixr 2 Source #

Disjunction of two props

assertionToMaybe :: Assertion -> IO (Maybe HUnitFailure) Source #

Catches a HUnit test failure, if the test fails.

forAll :: Show a => Gen a -> (a -> Property) -> Property Source #

Here we provide our own universsal quantifier instead of forAll, so we can monomorphize it to returning a Property

Extra HUnit assertions

assertSubset :: (Show a, Eq a) => [a] -> [a] -> Assertion Source #

Asserts whether a set is a subset of another one, both given as lists.

assertSameSets :: (Show a, Eq a) => [a] -> [a] -> Assertion Source #

Asserts whether 2 sets are equal, both given as lists.

Data structure to test mockchain traces

type FailureProp prop = PrettyCookedOpts -> [MockChainLogEntry] -> MockChainError -> prop Source #

Type of properties over failures

type SuccessProp a prop = PrettyCookedOpts -> [MockChainLogEntry] -> a -> UtxoState -> prop Source #

Type of properties over successes

type SizeProp prop = Integer -> prop Source #

Type of properties over the number of run outcomes. This does not necessitate a PrettyCookedOpts as parameter as an Integer does not contain anything significant that can be pretty printed.

type JournalProp prop = PrettyCookedOpts -> [MockChainLogEntry] -> prop Source #

Type of properties over the mockchain journal

data Test a prop Source #

Data structure to test a mockchain trace. a is the return typed of the tested trace, prop is the domain in which the properties live. This is not enforced here, but it will often be assumed that prop satisfies IsProp.

Constructors

Test 

Fields

testToProp :: IsProp prop => Test a prop -> prop Source #

This takes a Test and transforms it into an actual test case in prop. This is the main function justifying the existence of Test. This runs the traces, ensures there is the right number of outcomes and, depending on the nature of these outcomes, either calls testFailureProp or testSuccessProp. It also uses the aliases emitted during the mockchain run to pretty print messages when applicable.

testCooked :: String -> Test a Assertion -> TestTree Source #

A convenience helper when using Assertion which allows to replace testCase with testCooked and thus avoid the use of testToProp. Sadly we cannot generalise it with type classes on prop to work for QuichCheck at GHC will never be able to instantiate prop.

Simple test templates

mustSucceedTest :: IsProp prop => StagedMockChain a -> Test a prop Source #

A test template which expects a success from a trace

mustFailTest :: (IsProp prop, Show a) => StagedMockChain a -> Test a prop Source #

A test template which expects a failure from a trace

Appending elements (in particular requirements) to existing tests

withInitDist :: Test a prop -> InitialDistribution -> Test a prop Source #

Gives an initial distribution from which the trace will be run

withPrettyOpts :: Test a prop -> PrettyCookedOpts -> Test a prop Source #

Gives some pretty options to render test messages

withJournalProp :: IsProp prop => Test a prop -> JournalProp prop -> Test a prop Source #

Appends a requirements over the emitted log, which will need to be satisfied both in case of success or failure of the run.

withSuccessProp :: IsProp prop => Test a prop -> SuccessProp a prop -> Test a prop Source #

Appends a requirement over the resulting value and state of the mockchain run which will need to be satisfied if the run is successful

withResultProp :: IsProp prop => Test a prop -> (a -> prop) -> Test a prop Source #

Same as withSuccessProp but only considers the returning value of the run

withStateProp :: IsProp prop => Test a prop -> (UtxoState -> prop) -> Test a prop Source #

Some as withSuccessProp but only considers the returning state of the run

withSizeProp :: IsProp prop => Test a prop -> SizeProp prop -> Test a prop Source #

Appends a requirement over the resulting number of outcomes of the run

withFailureProp :: IsProp prop => Test a prop -> FailureProp prop -> Test a prop Source #

Appends a requirement over the resulting value and state of the mockchain run which will need to be satisfied if the run is successful

withErrorProp :: IsProp prop => Test a prop -> (MockChainError -> prop) -> Test a prop Source #

Same as withFailureProp but only considers the returning error of the run

Specific properties around failures

isPhase1Failure :: IsProp prop => FailureProp prop Source #

A property to ensure a phase 1 failure

isPhase2Failure :: IsProp prop => FailureProp prop Source #

A property to ensure a phase 2 failure

isPhase1FailureWithMsg :: IsProp prop => String -> FailureProp prop Source #

Same as isPhase1Failure with an added predicate on the text error

isPhase2FailureWithMsg :: IsProp prop => String -> FailureProp prop Source #

Same as isPhase2Failure with an added predicate over the text error

Specific properties around number of outcomes

isOfSize :: IsProp prop => Integer -> SizeProp prop Source #

Ensures the run has an exact given number of outcomes

isAtLeastOfSize :: IsProp prop => Integer -> SizeProp prop Source #

Ensures the run has a minimal number of outcomes

isAtMostOfSize :: IsProp prop => Integer -> SizeProp prop Source #

Ensures the run has a minimal number of outcomes

Specific properties over the journal

happened :: IsProp prop => String -> JournalProp prop Source #

Ensures a certain event has been emitted. This uses the constructor's name of the MockChainLogEntry by relying on show being lazy.

didNotHappen :: IsProp prop => String -> JournalProp prop Source #

Ensures a certain event has not been emitted. This uses the constructor's name of the MockChainLogEntry by relying on show being lazy.

Specific properties over successes

isInWallets :: IsProp prop => [(Wallet, [(AssetClass, Integer -> Bool)])] -> SuccessProp a prop Source #

Ensures that the given wallets satisfy certain amount requirements over a list of given asset classes in the end of the run

isInWallet :: IsProp prop => (Wallet, AssetClass, Integer) -> SuccessProp a prop Source #

Ensures that a given wallet possesses exactly a certain amount of a given asset class in the end of the run

Advanced test templates

mustFailInPhase2Test :: (IsProp prop, Show a) => StagedMockChain a -> Test a prop Source #

A test template which expects a Phase 2 failure

mustFailInPhase2WithMsgTest :: (IsProp prop, Show a) => String -> StagedMockChain a -> Test a prop Source #

A test template which expects a specific phase 2 error message

mustFailInPhase1Test :: (IsProp prop, Show a) => StagedMockChain a -> Test a prop Source #

A test template which expects a Phase 1 failure

mustFailInPhase1WithMsgTest :: (IsProp prop, Show a) => String -> StagedMockChain a -> Test a prop Source #

A test template which expects a specific phase 1 error message

mustSucceedWithSizeTest :: IsProp prop => Integer -> StagedMockChain a -> Test a prop Source #

A test template which expects a certain number of successful outcomes

mustFailWithSizeTest :: (IsProp prop, Show a) => Integer -> StagedMockChain a -> Test a prop Source #

A test template which expects a certain number of unsuccessful outcomes