From 569f5ea671230b0a4a558e162d19f4ec7f1c982f Mon Sep 17 00:00:00 2001 From: Andrey Kuprianov Date: Fri, 9 Apr 2021 15:07:17 +0200 Subject: [PATCH] a unit test for ICS03 using modelator Rust API (#713) --- modules/tests/runner/ibcsystem.rs | 17 ---------- modules/tests/runner/ics03.rs | 56 +++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/modules/tests/runner/ibcsystem.rs b/modules/tests/runner/ibcsystem.rs index 88490bcb3d..adb2a2fabd 100644 --- a/modules/tests/runner/ibcsystem.rs +++ b/modules/tests/runner/ibcsystem.rs @@ -163,23 +163,6 @@ impl IBCSystem { } } -// #[macro_export] -// macro_rules! chain_action { -// ( -// $name:ident : $kind:ident -// $($arg:ident : $t:ty),* -// ) => { -// pub fn $name(chain_id: &str, $($arg: $t),*) -> ChainAction { -// ChainAction { -// chain_id: chain_id.to_string(), -// action: Action::$kind($kind { -// $($arg),* -// }), -// } -// } -// }; -// } - #[macro_export] macro_rules! chain_action { ( diff --git a/modules/tests/runner/ics03.rs b/modules/tests/runner/ics03.rs index eace596b24..2fc255a0f4 100644 --- a/modules/tests/runner/ics03.rs +++ b/modules/tests/runner/ics03.rs @@ -104,10 +104,10 @@ impl StateHandler for IBCSystem { for connection_id in 0..ctr { if let Some(connection_end) = ctx.connection_end(&self.make(connection_id)) { let connection = Connection { - client_id: self.make(connection_end.client_id().clone()), + client_id: Some(self.make(connection_end.client_id().clone())), connection_id: Some(connection_id), - counterparty_client_id: self - .make(connection_end.counterparty().client_id().clone()), + counterparty_client_id: Some(self + .make(connection_end.counterparty().client_id().clone())), counterparty_connection_id: connection_end .counterparty() .connection_id() @@ -264,3 +264,53 @@ impl ActionHandler for IBCSystem { self.decode_connection_outcome(result) } } + + +#[cfg(test)] +mod tests { + use super::*; + use super::super::ics02::{AbstractState as ClientState, ChainAction as ClientAction}; + + #[test] + pub fn test() { + let initial = AbstractState { + chains: HashMap::from_iter(IntoIter::new([ + ("chain1".into(), Chain::new(10)), + ("chain2".into(), Chain::new(20)), + ])), + }; + + let events = modelator::EventStream::new() + // initialize the state of your system from abstract state + .init(initial) + // you can inspect the current abstract state of your system + .check(|state: AbstractState| println!("{:?}", state)) + // or make assertions about it + .check(|state: AbstractState| assert_eq!(state.chains.len(), 2)) + // you can also execute abstract actions against your system + .action(ChainAction::connection_open_init("chain1", 10, "chain2".to_string(), 30)) + // should fail because no client exist yet + .outcome(ActionOutcome::ICS03MissingClient) + // let's create the client + .action(ClientAction::create_client("chain1", 10, 20)) + // and try again + .action(ChainAction::connection_open_init("chain1", 0, "chain2".to_string(), 30)) + // should pass now + .outcome(ActionOutcome::OK) + // debug-print the client state + .check(|state: ClientState| println!("{:?}", state)) + // debug-print the connection state + .check(|state: AbstractState| println!("{:?}", state)) + ; + + let mut runner = Runner::::new() + .with_state::() + .with_action::() + .with_state::() + .with_action::() + ; + let mut system = IBCSystem::new(); + let result = runner.run(&mut system, &mut events.into_iter()); + assert!(matches!(result, TestResult::Success(_))) + } +}