Safe Haskell | None |
---|---|
Language | Haskell2010 |
Module containing some utilities for testing Michelson contracts using Haskell testing frameworks (hspec and QuickCheck in particular). It's Morley testing EDSL.
Synopsis
- specWithContract :: (Each [Typeable, SingI] [cp, st], HasCallStack) => FilePath -> ((Contract, Contract cp st) -> Spec) -> Spec
- specWithContractL :: (Each [Typeable, SingI] [ToT cp, ToT st], HasCallStack) => FilePath -> ((Contract, Contract cp st) -> Spec) -> Spec
- specWithTypedContract :: (Each [Typeable, SingI] [cp, st], HasCallStack) => FilePath -> (Contract cp st -> Spec) -> Spec
- specWithUntypedContract :: FilePath -> (Contract -> Spec) -> Spec
- importUntypedContract :: FilePath -> IO Contract
- type ContractReturn st = (Either MichelsonFailed ([Operation], Value st), InterpreterState)
- type ContractPropValidator st prop = ContractReturn st -> prop
- contractProp :: (IsoValue param, IsoValue storage, ToT param ~ cp, ToT storage ~ st) => Contract cp st -> ContractPropValidator st prop -> ContractEnv -> param -> storage -> prop
- contractPropVal :: Contract cp st -> ContractPropValidator st prop -> ContractEnv -> Value cp -> Value st -> prop
- contractRepeatedProp :: (IsoValue param, IsoValue storage, ToT param ~ cp, ToT storage ~ st) => Contract cp st -> ContractPropValidator st prop -> ContractEnv -> [param] -> storage -> prop
- contractRepeatedPropVal :: Contract cp st -> ContractPropValidator st prop -> ContractEnv -> [Value cp] -> Value st -> prop
- type IntegrationalValidator = Either (InterpreterError -> Bool) SuccessValidator
- type SuccessValidator = InternalState -> GState -> [GStateUpdate] -> Either ValidationError ()
- type IntegrationalScenario = IntegrationalScenarioM Validated
- type IntegrationalScenarioM = StateT InternalState (Except ValidationError)
- integrationalTestExpectation :: IntegrationalScenario -> Expectation
- integrationalTestProperty :: IntegrationalScenario -> Property
- originate :: Contract -> Text -> Value -> Mutez -> IntegrationalScenarioM Address
- transfer :: TxData -> Address -> IntegrationalScenarioM ()
- validate :: IntegrationalValidator -> IntegrationalScenario
- setMaxSteps :: RemainingSteps -> IntegrationalScenarioM ()
- setNow :: Timestamp -> IntegrationalScenarioM ()
- composeValidators :: SuccessValidator -> SuccessValidator -> SuccessValidator
- composeValidatorsList :: [SuccessValidator] -> SuccessValidator
- expectAnySuccess :: SuccessValidator
- expectStorageUpdate :: Address -> (Value -> Either ValidationError ()) -> SuccessValidator
- expectStorageUpdateConst :: Address -> Value -> SuccessValidator
- expectBalance :: Address -> Mutez -> SuccessValidator
- expectStorageConst :: Address -> Value -> SuccessValidator
- expectGasExhaustion :: InterpreterError -> Bool
- expectMichelsonFailed :: (MichelsonFailed -> Bool) -> Address -> InterpreterError -> Bool
- data TxData = TxData {
- tdSenderAddress :: !Address
- tdParameter :: !Value
- tdAmount :: !Mutez
- genesisAddress :: Address
- failedProp :: Text -> Property
- succeededProp :: Property
- qcIsLeft :: Show b => Either a b -> Property
- qcIsRight :: Show a => Either a b -> Property
- dummyContractEnv :: ContractEnv
- minTimestamp :: Timestamp
- maxTimestamp :: Timestamp
- midTimestamp :: Timestamp
Importing a contract
specWithContract :: (Each [Typeable, SingI] [cp, st], HasCallStack) => FilePath -> ((Contract, Contract cp st) -> Spec) -> Spec Source #
Import contract and use it in the spec. Both versions of contract are passed to the callback function (untyped and typed).
If contract's import failed, a spec with single failing expectation will be generated (so tests will run unexceptionally, but a failing result will notify about problem).
specWithContractL :: (Each [Typeable, SingI] [ToT cp, ToT st], HasCallStack) => FilePath -> ((Contract, Contract cp st) -> Spec) -> Spec Source #
Like specWithContract
, but for Lorentz types.
specWithTypedContract :: (Each [Typeable, SingI] [cp, st], HasCallStack) => FilePath -> (Contract cp st -> Spec) -> Spec Source #
A version of specWithContract
which passes only the typed
representation of the contract.
Unit testing
type ContractReturn st = (Either MichelsonFailed ([Operation], Value st), InterpreterState) Source #
type ContractPropValidator st prop = ContractReturn st -> prop Source #
Type for contract execution validation.
It's a function which is supplied with contract execution output (failure or new storage with operation list).
Function returns a property which type is designated by type variable prop
and might be Property
or Expectation
or anything else relevant.
contractProp :: (IsoValue param, IsoValue storage, ToT param ~ cp, ToT storage ~ st) => Contract cp st -> ContractPropValidator st prop -> ContractEnv -> param -> storage -> prop Source #
Contract's property tester against given input. Takes contract environment, initial storage and parameter, interprets contract on this input and invokes validation function.
contractPropVal :: Contract cp st -> ContractPropValidator st prop -> ContractEnv -> Value cp -> Value st -> prop Source #
Version of contractProp
which takes Val
as arguments instead
of regular Haskell values.
contractRepeatedProp :: (IsoValue param, IsoValue storage, ToT param ~ cp, ToT storage ~ st) => Contract cp st -> ContractPropValidator st prop -> ContractEnv -> [param] -> storage -> prop Source #
contractRepeatedPropVal :: Contract cp st -> ContractPropValidator st prop -> ContractEnv -> [Value cp] -> Value st -> prop Source #
Integrational testing
Testing engine
type IntegrationalValidator = Either (InterpreterError -> Bool) SuccessValidator Source #
Validator for integrational testing.
If an error is expected, it should be Left
with validator for errors.
Otherwise it should check final global state and its updates.
type SuccessValidator = InternalState -> GState -> [GStateUpdate] -> Either ValidationError () Source #
Validator for integrational testing that expects successful execution.
type IntegrationalScenario = IntegrationalScenarioM Validated Source #
type IntegrationalScenarioM = StateT InternalState (Except ValidationError) Source #
A monad inside which integrational tests can be described using do-notation.
integrationalTestExpectation :: IntegrationalScenario -> Expectation Source #
Integrational test that executes given operations and validates
them using given validator. It can fail using Expectation
capability.
It starts with initGState
and some reasonable dummy values for
gas limit and current timestamp. You can update blockchain state
by performing some operations.
integrationalTestProperty :: IntegrationalScenario -> Property Source #
Integrational test similar to integrationalTestExpectation
.
It can fail using Property
capability.
It can be used with QuickCheck's forAll
to make a
property-based test with arbitrary data.
originate :: Contract -> Text -> Value -> Mutez -> IntegrationalScenarioM Address Source #
Originate a contract with given initial storage and balance. Its address is returned.
transfer :: TxData -> Address -> IntegrationalScenarioM () Source #
Transfer tokens to given address.
validate :: IntegrationalValidator -> IntegrationalScenario Source #
Execute all operations that were added to the scenarion since
last validate
call. If validator fails, the execution will be aborted.
setMaxSteps :: RemainingSteps -> IntegrationalScenarioM () Source #
Make all further interpreter calls (which are triggered by the
validate
function) use given gas limit.
setNow :: Timestamp -> IntegrationalScenarioM () Source #
Make all further interpreter calls (which are triggered by the
validate
function) use given timestamp as the current one.
Validators
composeValidators :: SuccessValidator -> SuccessValidator -> SuccessValidator Source #
Compose two success validators.
For example:
expectBalance bal addr composeValidators
expectStorageUpdateConst addr2 ValueUnit
composeValidatorsList :: [SuccessValidator] -> SuccessValidator Source #
Compose a list of success validators.
expectAnySuccess :: SuccessValidator Source #
SuccessValidator
that always passes.
expectStorageUpdate :: Address -> (Value -> Either ValidationError ()) -> SuccessValidator Source #
Check that storage value is updated for given address. Takes a predicate that is used to check the value.
It works even if updates are not filtered (i. e. a value can be updated more than once).
expectStorageUpdateConst :: Address -> Value -> SuccessValidator Source #
Like expectStorageUpdate
, but expects a constant.
expectBalance :: Address -> Mutez -> SuccessValidator Source #
Check that eventually address has some particular balance.
expectStorageConst :: Address -> Value -> SuccessValidator Source #
Check that eventually address has some particular storage value.
expectGasExhaustion :: InterpreterError -> Bool Source #
Check that interpreter failed due to gas exhaustion.
expectMichelsonFailed :: (MichelsonFailed -> Bool) -> Address -> InterpreterError -> Bool Source #
Expect that interpretation of contract with given address ended with [FAILED].
Various
Data associated with a particular transaction.
TxData | |
|
genesisAddress :: Address Source #
One of genesis addresses.
General utilities
succeededProp :: Property Source #
A Property
that always succeeds.
Dummy values
dummyContractEnv :: ContractEnv Source #
Dummy ContractEnv
with some reasonable hardcoded values. You
can override values you are interested in using record update
syntax.
Arbitrary data
minTimestamp :: Timestamp Source #
Minimal (earliest) timestamp used for Arbitrary (CValue 'CTimestamp)
maxTimestamp :: Timestamp Source #
Maximal (latest) timestamp used for Arbitrary (CValue 'CTimestamp)
midTimestamp :: Timestamp Source #
Median of minTimestamp
and maxTimestamp
.
Useful for testing (exactly half of generated dates will be before and after
this date).