Skip to content

Commit

Permalink
Getting rid of SimpleCustomHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
hashedone committed Sep 7, 2021
1 parent 1f82b41 commit a3e574b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use serde::Serialize;

use crate::bank::Bank;
use crate::contracts::Contract;
use crate::custom_handler::CustomHandler;
use crate::custom_handler::{CustomHandler, PanickingCustomHandler};
use crate::executor::{AppResponse, Executor};
use crate::transactions::transactional;
use crate::wasm::{ContractData, Wasm, WasmKeeper};
use crate::{BankKeeper, PanickingCustomHandler};
use crate::BankKeeper;

use anyhow::Result as AnyResult;
use derivative::Derivative;
Expand Down
66 changes: 31 additions & 35 deletions packages/multi-test/src/custom_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,33 @@ pub trait CustomHandler<ExecC = Empty, QueryC = Empty> {
) -> AnyResult<Binary>;
}

/// Simplified version of `CustomHandler` having only arguments which are not app internals - they
/// are just discarded. Useful for simpler mocking.
pub trait SimpleCustomHandler<ExecC = Empty, QueryC = Empty> {
fn execute(&self, block: &BlockInfo, sender: Addr, msg: ExecC) -> AnyResult<AppResponse>;
fn query(&self, block: &BlockInfo, msg: QueryC) -> AnyResult<Binary>;
}
/// Custom handler implementation panicking on each call. Assuming, that unless specific behavior
/// is implemented, custom messages should not be send.
pub(crate) struct PanickingCustomHandler;

impl<ExecC, QueryC, T: SimpleCustomHandler<ExecC, QueryC>> CustomHandler<ExecC, QueryC> for T {
impl<ExecC, QueryC> CustomHandler<ExecC, QueryC> for PanickingCustomHandler
where
ExecC: std::fmt::Debug,
QueryC: std::fmt::Debug,
{
fn execute(
&self,
_: &dyn Api,
_: &mut dyn Storage,
block: &BlockInfo,
_api: &dyn Api,
_storage: &mut dyn Storage,
_block: &BlockInfo,
sender: Addr,
msg: ExecC,
) -> AnyResult<AppResponse> {
self.execute(block, sender, msg)
panic!("Unexpected custom exec msg {:?} from {:?}", msg, sender)
}

fn query(
&self,
_: &dyn Api,
_: &dyn Storage,
block: &BlockInfo,
_api: &dyn Api,
_storage: &dyn Storage,
_block: &BlockInfo,
msg: QueryC,
) -> AnyResult<Binary> {
self.query(block, msg)
}
}

/// Custom handler implementation panicking on each call. Assuming, that unless specific behavior
/// is implemented, custom messages should not be send.
pub struct PanickingCustomHandler;

impl<ExecC, QueryC> SimpleCustomHandler<ExecC, QueryC> for PanickingCustomHandler
where
ExecC: std::fmt::Debug,
QueryC: std::fmt::Debug,
{
fn execute(&self, _block: &BlockInfo, sender: Addr, msg: ExecC) -> AnyResult<AppResponse> {
panic!("Unexpected custom exec msg {:?} from {:?}", msg, sender)
}

fn query(&self, _block: &BlockInfo, msg: QueryC) -> AnyResult<Binary> {
panic!("Unexpected custom query {:?}", msg)
}
}
Expand Down Expand Up @@ -116,13 +99,26 @@ impl<ExecC, QueryC> CachingCustomHandler<ExecC, QueryC> {
}
}

impl<ExecC, QueryC> SimpleCustomHandler<ExecC, QueryC> for CachingCustomHandler<ExecC, QueryC> {
fn execute(&self, _block: &BlockInfo, _sender: Addr, msg: ExecC) -> AnyResult<AppResponse> {
impl<ExecC, QueryC> CustomHandler<ExecC, QueryC> for CachingCustomHandler<ExecC, QueryC> {
fn execute(
&self,
_api: &dyn Api,
_storage: &mut dyn Storage,
_block: &BlockInfo,
_sender: Addr,
msg: ExecC,
) -> AnyResult<AppResponse> {
self.state.execs.borrow_mut().push(msg);
Ok(AppResponse::default())
}

fn query(&self, _block: &BlockInfo, msg: QueryC) -> AnyResult<Binary> {
fn query(
&self,
_api: &dyn Api,
_storage: &dyn Storage,
_block: &BlockInfo,
msg: QueryC,
) -> AnyResult<Binary> {
self.state.queries.borrow_mut().push(msg);
Ok(Binary::default())
}
Expand Down
2 changes: 1 addition & 1 deletion packages/multi-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ mod wasm;
pub use crate::app::{next_block, App, AppBuilder, Router};
pub use crate::bank::{Bank, BankKeeper};
pub use crate::contracts::{Contract, ContractWrapper};
pub use crate::custom_handler::{CustomHandler, PanickingCustomHandler, SimpleCustomHandler};
pub use crate::custom_handler::CustomHandler;
pub use crate::executor::{AppResponse, Executor};
pub use crate::wasm::{parse_contract_addr, Wasm, WasmKeeper};
2 changes: 1 addition & 1 deletion packages/multi-test/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ pub fn parse_contract_addr(data: &Option<Binary>) -> AnyResult<Addr> {

#[cfg(test)]
mod test {
use crate::PanickingCustomHandler;
use crate::custom_handler::PanickingCustomHandler;
use cosmwasm_std::testing::{mock_env, mock_info, MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{coin, from_slice, to_vec, BankMsg, Coin, CosmosMsg, Empty, StdError};

Expand Down

0 comments on commit a3e574b

Please sign in to comment.