Module concordium_std::test_infrastructure
source · Expand description
The test infrastructure module provides alternative implementations of
HasInitContext
, HasReceiveContext
, HasParameter
, HasStateApi
,
and HasHost
traits intended for testing.
They allow writing unit tests directly in contract modules with little to no external tooling, depending on what is required.
Example
// Some contract
#[init(contract = "noop")]
fn contract_init<S: HasStateApi>(
ctx: &impl HasInitContext,
state_builder: &mut StateBuilder<S>,
) -> InitResult<State> {
// ...
}
#[receive(contract = "noop", name = "receive", payable, enable_logger, mutable)]
fn contract_receive<S: HasStateApi>(
ctx: &impl HasReceiveContext,
host: &mut impl HasHost<State, StateApiType = S>,
amount: Amount,
logger: &mut impl HasLogger,
) -> ReceiveResult<MyReturnValue> {
// ...
}
#[cfg(test)]
mod tests {
use super::*;
use concordium_std::test_infrastructure::*;
#[test]
fn test_init() {
let mut ctx = TestInitContext::empty();
let mut state_builder = TestStateBuilder::new();
ctx.set_init_origin(AccountAddress([0u8; 32]));
let result = contract_init(&ctx, &mut state_builder);
// claim!(...)
}
#[test]
fn test_receive() {
let mut ctx = TestReceiveContext::empty();
let mut host = TestHost::new(State::new(), TestStateBuilder::new());
ctx.set_owner(AccountAddress([0u8; 32]));
// ...
let mut logger = TestLogger::init();
host.setup_mock_entrypoint(
ContractAddress {
index: 0,
subindex: 0,
},
OwnedEntrypointName::new_unchecked("get".into()),
MockFn::returning_ok(MyReturnValue::new()),
);
let result: ReceiveResult<MyReturnValue> =
contract_receive(&ctx, &mut host, Amount::zero(), &mut logger);
// claim!(...)
}
}
Structs
Holds a function used for mocking invocations of contracts with
invoke_contract
.Placeholder for the context chain meta data.
All the fields are optionally set and the getting an unset field will result
in test failing.
For most cases it is used as part of either
TestInitContext
or
TestReceiveContext
.
Use only in unit tests!Context used for testing. The type parameter C is used to determine whether
this will be an init or receive context.
A
HasCryptoPrimitives
implementation used for unit testing smart
contracts.A logger that simply accumulates all the logged items to be inspected at the
end of execution.
Test parameter cursor.
Should not be constructed directly, use TestReceiveContext or
TestInitContext.
Policy type used by init and receive contexts for testing.
This type should not be used directly, but rather through
its
HasPolicy
interface.A state api used for testing. Implements
HasStateApi
.A state entry used for testing. Implements
HasStateEntry
.Enums
A wrapper for the data stored in
TestStateEntry
, which is used to match
the semantics of the host functions. Specifically, it is used to ensure that
interactions with a deleted entry result in a error.An error that is raised when operating with
Seek
, Write
, Read
, or
HasStateEntry
trait methods of the TestStateApi
type.Type Definitions
Placeholder for the initial context. All the fields can be set optionally
and the getting an unset field will result in calling
fail!
. Use only in tests!Placeholder for the receiving context. All the fields can be set optionally
and the getting an unset field will result in calling
fail!
. Use only in tests!