Type Definition concordium_std::test_infrastructure::TestInitContext
source · [−]pub type TestInitContext<'a> = TestContext<'a, TestInitOnlyData>;
Expand description
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!
Setters
Every field has a setter function prefixed with set_
.
Example
Creating an empty context and setting the init_origin
.
let mut ctx = TestInitContext::empty();
ctx.set_init_origin(AccountAddress([0u8; 32]));
Set chain meta data
Chain meta data is set using setters on the context or by setters on a
mutable reference of TestChainMeta
.
Example
Creating an empty context and setting the slot_time
metadata.
let mut ctx = TestInitContext::empty();
ctx.set_metadata_slot_time(Timestamp::from_timestamp_millis(1609459200));
or
let mut ctx = TestInitContext::empty();
ctx.metadata_mut().set_slot_time(Timestamp::from_timestamp_millis(1609459200));
Use case example
type ParameterType = u64;
#[init(contract = "noop", parameter = "ParameterType")]
fn contract_init<S: HasStateApi>(
ctx: &impl HasInitContext,
state_builder: &mut StateBuilder<S>,
) -> InitResult<()> {
let init_origin = ctx.init_origin();
let parameter: ParameterType = ctx.parameter_cursor().get()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use concordium_std::test_infrastructure::*;
#[test]
fn test() {
let ctx = TestInitContext::empty();
let mut state_builder = TestStateBuilder::new();
ctx.set_init_origin(AccountAddress([0u8; 32]));
let result = contract_init(&ctx, &mut state_builder);
// Reads the init_origin without any problems.
// But then fails because the parameter is not set.
}
}
Implementations
sourceimpl<'a> TestInitContext<'a>
impl<'a> TestInitContext<'a>
sourcepub fn empty() -> Self
pub fn empty() -> Self
Create an TestInitContext
where every field is unset, and getting any
of the fields will result in fail!
.
sourcepub fn set_init_origin(&mut self, value: AccountAddress) -> &mut Self
pub fn set_init_origin(&mut self, value: AccountAddress) -> &mut Self
Set init_origin
in the TestInitContext
Trait Implementations
sourceimpl<'a> HasInitContext<()> for TestInitContext<'a>
impl<'a> HasInitContext<()> for TestInitContext<'a>
sourcefn init_origin(&self) -> AccountAddress
fn init_origin(&self) -> AccountAddress
Who invoked this init call.