Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor connection handler unit tests to adapt with new API #441

Merged
merged 2 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Refactor connection handler unit tests to adapt with new Validation/Execution API
([#440](https://github.com/cosmos/ibc-rs/issues/440)).
27 changes: 7 additions & 20 deletions crates/ibc/src/core/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ mod tests {
ConnectionEnd, Counterparty as ConnCounterparty, State as ConnState,
};
use crate::core::ics03_connection::msgs::{
conn_open_ack::{test_util::get_dummy_raw_msg_conn_open_ack, MsgConnectionOpenAck},
conn_open_init::{test_util::get_dummy_raw_msg_conn_open_init, MsgConnectionOpenInit},
conn_open_try::{test_util::get_dummy_raw_msg_conn_open_try, MsgConnectionOpenTry},
ConnectionMsg,
conn_open_ack::MsgConnectionOpenAck, conn_open_init::MsgConnectionOpenInit,
conn_open_try::MsgConnectionOpenTry, ConnectionMsg,
};
use crate::core::ics03_connection::version::Version as ConnVersion;
use crate::core::ics04_channel::channel::ChannelEnd;
Expand Down Expand Up @@ -162,26 +160,15 @@ mod tests {
//
// Connection handshake messages.
//
let msg_conn_init =
MsgConnectionOpenInit::try_from(get_dummy_raw_msg_conn_open_init(None)).unwrap();
let msg_conn_init = MsgConnectionOpenInit::new_dummy();

let correct_msg_conn_try = MsgConnectionOpenTry::try_from(get_dummy_raw_msg_conn_open_try(
client_height,
client_height,
))
.unwrap();
let correct_msg_conn_try = MsgConnectionOpenTry::new_dummy(client_height, client_height);

// The handler will fail to process this msg because the client height is too advanced.
let incorrect_msg_conn_try = MsgConnectionOpenTry::try_from(
get_dummy_raw_msg_conn_open_try(client_height + 1, client_height + 1),
)
.unwrap();
let incorrect_msg_conn_try =
MsgConnectionOpenTry::new_dummy(client_height + 1, client_height + 1);

let msg_conn_ack = MsgConnectionOpenAck::try_from(get_dummy_raw_msg_conn_open_ack(
client_height,
client_height,
))
.unwrap();
let msg_conn_ack = MsgConnectionOpenAck::new_dummy(client_height, client_height);

//
// Channel handshake messages.
Expand Down
37 changes: 37 additions & 0 deletions crates/ibc/src/core/ics03_connection/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,40 @@ where
ConnectionMsg::OpenConfirm(msg) => conn_open_confirm::process(ctx, msg),
}
}

#[cfg(test)]
pub mod test_util {
use core::fmt::Debug;

use crate::{core::ContextError, mock::context::MockContext, prelude::String};
use alloc::format;

pub enum Expect {
Success,
Failure(Option<ContextError>),
}

#[derive(Clone, Debug)]
pub struct Fixture<M: Debug> {
pub ctx: MockContext,
pub msg: M,
}

impl<M: Debug> Fixture<M> {
pub fn generate_error_msg(
&self,
expect: &Expect,
process: &str,
res: &Result<(), ContextError>,
) -> String {
let base_error = match expect {
Expect::Success => "step failed!",
Expect::Failure(_) => "step passed but was supposed to fail!",
};
format!(
"{process} {base_error} /n {res:?} /n {:?} /n {:?}",
&self.msg, &self.ctx
)
}
}
}
Loading