Skip to content

Commit

Permalink
WIP on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Oct 10, 2024
1 parent c98b2ce commit c1be15a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 5 additions & 5 deletions rust/examples/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ pub mod bank {
use ixc::*;
use ixc_core::error::unimplemented_ok;
use ixc_core::handler::ClientFactory;
use ixc_message_api::code::{ErrorCode};

#[derive(Resources)]
pub struct Bank {
#[state(prefix = 1, key(address, denom), value(amount))]
pub(crate) balances: AccumulatorMap<(AccountID, Str)>,
#[state(prefix = 2, key(denom), value(total))]
supply: AccumulatorMap<Str>,
pub(crate) supply: AccumulatorMap<Str>,
#[state(prefix = 3)]
super_admin: Item<AccountID>,
#[state(prefix = 4)]
Expand Down Expand Up @@ -206,12 +205,13 @@ mod tests {
let bob_balance = bank_client.get_balance(&bob, bob.account_id(), "foo").unwrap();
assert_eq!(bob_balance, 100);

// look inside bank to check the balance of alice directly
// look inside bank to check the balance of alice directly as well as the supply of foo
app.exec_in(&bank_client, |bank, ctx| {
let alice_balance = bank.balances.get(ctx, (alice_id, "foo")).unwrap();
assert_eq!(alice_balance, 900);
Ok(())
}).unwrap();
let foo_supply = bank.supply.get(ctx, "foo").unwrap();
assert_eq!(foo_supply, 1000);
})
}
}

Expand Down
8 changes: 5 additions & 3 deletions rust/testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ impl TestApp {
}

/// Executes a function in the context of a handler.
pub fn exec_in<HC: HandlerClient, F, R>(&self, client: &HC, f: F) -> ClientResult<R>
/// This provides a way for tests to peek inside and manipulate a handler's state directly.
/// This method will panic if we can't call into the handler, but panicking is acceptable in tests.
pub fn exec_in<HC: HandlerClient, F, R>(&self, client: &HC, f: F) -> R
where
F: FnOnce(&HC::Handler, &mut Context) -> ClientResult<R>,
F: FnOnce(&HC::Handler, &mut Context) -> R,
{
// TODO lookup handler ID to make sure this is the correct handler
let scope = ResourceScope::default();
let h = unsafe { HC::Handler::new(&scope) }.unwrap(); // TODO error
let h = unsafe { HC::Handler::new(&scope) }.unwrap();
let mut ctx = self.client_context_for(client.account_id());
f(&h, &mut ctx)
}
Expand Down

0 comments on commit c1be15a

Please sign in to comment.