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

testBoolMsg :: IsProp prop => String -> Bool -> prop Source #

Turns a boolean into a prop, displaying an error message when applicable

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 -> UtxoState -> 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 LogProp prop = PrettyCookedOpts -> [MockChainLogEntry] -> prop Source #

Type of properties over the mockchain log

type StateProp prop = PrettyCookedOpts -> UtxoState -> prop Source #

Type of properties over the UtxoState

type Runner effs a b = MockChainState -> InitialDistribution -> Sem effs a -> [MockChainReturn b] Source #

Type of trace runners

data Test effs a b 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, Show b) => Test effs a b 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 :: forall effs a b. Show b => String -> Test effs a b 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.

testCookedFromInitDistTemplate :: forall effs a b. Show b => String -> Test effs a b Assertion -> TestTree Source #

Same as testCooked but first assigns the initial distribution template as a starting point to the test

testCookedQC :: forall effs a b. Show b => String -> Test effs a b Property -> TestTree Source #

Same as testCooked, but for Property

testCookedQCFromInitDistTemplate :: forall effs a b. Show b => String -> Test effs a b Property -> TestTree Source #

Same as testCookedQC but first assigns the initial distribution template as a starting point to the test

Simple test templates

mustSucceedTest' :: IsProp prop => Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template which expects a success from a trace. This test template is built from a trace and a dedicated runner, to be used for runs that do not implement RunnableMockChain. One of the intended uses is for running StagedInjectMockChain when the additional effect results in a extended return value (such as a resulting state).

mustSucceedTest :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => Sem effs a -> Test effs a a prop Source #

A test template which expects a success from a RunnableMockChain trace.

mustFailTest' :: IsProp prop => Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template which expects a failure from a trace. See mustSucceedTest' for more information on its intended usage.

mustFailTest :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => Sem effs a -> Test effs a a prop Source #

A test template which expects a failure from a RunnableMockChain trace.

Appending elements (in particular requirements) to existing tests

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

Gives an initial distribution from which the trace will be run

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

Gives some pretty options to render test messages

withLogProp :: IsProp prop => Test effs a b prop -> LogProp prop -> Test effs a b 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.

withStateProp :: IsProp prop => Test effs a b prop -> StateProp prop -> Test effs a b prop Source #

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

withSuccessProp :: IsProp prop => Test effs a b prop -> SuccessProp b prop -> Test effs a b 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 effs a b prop -> (b -> prop) -> Test effs a b prop Source #

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

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

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

withFailureProp :: IsProp prop => Test effs a b prop -> FailureProp prop -> Test effs a b 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 effs a b prop -> (MockChainError -> prop) -> Test effs a b 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 log

happened :: IsProp prop => String -> LogProp 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 -> LogProp 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

isAtAddress :: (IsProp prop, ToAddress addr, Show addr) => [(addr, [(AssetClass, Integer -> Bool)])] -> SuccessProp a prop Source #

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

possesses :: (IsProp prop, ToAddress addr, Show addr) => addr -> AssetClass -> Integer -> SuccessProp a prop Source #

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

Advanced test templates

mustFailInPhase2Test' :: IsProp prop => Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template expecting a Phase 2 failure for arbitrary runs

mustFailInPhase2Test :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => Sem effs a -> Test effs a a prop Source #

A test template expecting a Phase 2 failure for RunnableMockChain runs

mustFailInPhase2WithMsgTest' :: IsProp prop => String -> Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template expecting a specific phase 2 error message for arbitrary runs

mustFailInPhase2WithMsgTest :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => String -> Sem effs a -> Test effs a a prop Source #

A test template expecting a specific phase 2 error message for RunnableMockChain runs

mustFailInPhase1Test' :: IsProp prop => Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template expecting a Phase 1 failure

mustFailInPhase1Test :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => Sem effs a -> Test effs a a prop Source #

A test template expecting a Phase 1 failure for RunnableMockChain runs

mustFailInPhase1WithMsgTest' :: IsProp prop => String -> Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template expecting a specific phase 1 error message

mustFailInPhase1WithMsgTest :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => String -> Sem effs a -> Test effs a a prop Source #

A test template expecting a specific phase 1 error message for RunnableMockChain runs

mustSucceedWithSizeTest' :: IsProp prop => Integer -> Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template expecting a certain number of successful outcomes for arbitrary runs

mustSucceedWithSizeTest :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => Integer -> Sem effs a -> Test effs a a prop Source #

A test template expecting a certain number of successful outcomes for RunnableMockChain runs

mustFailWithSizeTest' :: IsProp prop => Integer -> Runner effs a b -> Sem effs a -> Test effs a b prop Source #

A test template expecting a certain number of unsuccessful outcomes for arbitrary runs

mustFailWithSizeTest :: (IsProp prop, RunnableMockChain effs, Member MockChainWrite effs) => Integer -> Sem effs a -> Test effs a a prop Source #

A test template expecting a certain number of unsuccessful outcomes for RunnableMockChain runs