Skip to main content

MockContract

Struct MockContract 

Source
pub struct MockContract { /* private fields */ }
Expand description

Handle to a mock contract registered in a TestEnvironment: a contract whose entrypoints are native closures instead of rv32 code.

Implementations§

Source§

impl MockContract

Source

pub fn as_contract(&self) -> Contract

The Contract::kMockTest value identifying this mock.

Source

pub fn as_hash(&self) -> &SHA256Hash

The contract hash (sha256(Contract::kMockTest(index))).

Source

pub fn deploy_contract_to(&self, addr: &ContractAddress)

Deploy this prepared mock contract at a fixed address in test setup.

This uses the test environment’s setup path, so it should be called before executing the transaction under test.

Source

pub fn register_method<F>(&mut self, id: ContractEntrypointID, func: F)
where F: Fn() + UnwindSafe + Clone + Send + Sync + 'static,

Register func as the entrypoint for method id. The closure runs inside the frame’s catch site, so panics and aborts out of it map to frame outcomes.

Panics if id is already registered on this mock: two methods resolving to one dispatch ID means the second registration would silently shadow the first, so duplicate definitions (same interface name, major version, and method name) fail loudly at test setup instead. There is deliberately no replace/override variant; on-chain dispatch has no override either.

Send + Sync on func is what keeps a mock body safe to run on the thread the coroutine resumed on rather than the one that registered it; see invoke_wrapper. Capturing Rc/RefCell state to record calls is rejected here – use Arc/Mutex, or an AtomicUsize for a plain counter.

Source

pub fn register_unknown_method<F>(&mut self, func: F)
where F: Fn() + UnwindSafe + Clone + Send + Sync + 'static,

Install the handler for method ids this contract does not define, replacing any previous one.

The registered closure runs inside the same invoke_wrapper catch site as a real method, so aborting out of it (e.g. via thruput_sdk::exn::library_throw) reaches the caller as an ordinary exception rather than escaping the frame.

Source

pub fn register<F: FnOnce(&mut Self)>(&mut self, f: F) -> &mut Self

Apply a registrar closure (e.g. a generated __mock function), for chaining.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> StaticIfNoAlloc for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.