Skip to content

Commit

Permalink
s/state/step
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorenesduarte committed Feb 3, 2021
1 parent 4167d29 commit 7755fec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
38 changes: 19 additions & 19 deletions modules/tests/model_based.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod modelator;
mod state;
mod step;

use ibc::ics02_client::client_def::AnyHeader;
use ibc::ics02_client::client_def::{AnyClientState, AnyConsensusState};
Expand All @@ -24,10 +24,10 @@ use ibc::mock::context::MockContext;
use ibc::mock::header::MockHeader;
use ibc::mock::host::HostType;
use ibc::Height;
use state::{ActionOutcome, ActionType, State};
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Debug, Display};
use step::{ActionOutcome, ActionType, Step};
use tendermint::account::Id as AccountId;

#[derive(Debug)]
Expand Down Expand Up @@ -133,31 +133,31 @@ impl ICS02TestExecutor {
}
}

impl modelator::TestExecutor<State> for ICS02TestExecutor {
fn check_initial_state(&mut self, state: State) -> bool {
impl modelator::TestExecutor<Step> for ICS02TestExecutor {
fn initial_step(&mut self, step: Step) -> bool {
assert_eq!(
state.action.action_type,
step.action.action_type,
ActionType::Null,
"unexpected action type"
);
assert_eq!(
state.action_outcome,
step.action_outcome,
ActionOutcome::Null,
"unexpected action outcome"
);
true
}

fn check_next_state(&mut self, state: State) -> bool {
match state.action.action_type {
fn next_step(&mut self, step: Step) -> bool {
match step.action.action_type {
ActionType::Null => panic!("unexpected action type"),
ActionType::ICS02CreateClient => {
// get action parameters
let chain_id = state
let chain_id = step
.action
.chain_id
.expect("create client action should have a chain identifier");
let height = state
let height = step
.action
.height
.expect("create client action should have a height");
Expand All @@ -174,7 +174,7 @@ impl modelator::TestExecutor<State> for ICS02TestExecutor {
let result = ctx.deliver(msg);

// check the expected outcome: client create always succeeds
match state.action_outcome {
match step.action_outcome {
ActionOutcome::ICS02CreateOK => {
// the implementaion matches the model if no error occurs
result.is_ok()
Expand All @@ -184,15 +184,15 @@ impl modelator::TestExecutor<State> for ICS02TestExecutor {
}
ActionType::ICS02UpdateClient => {
// get action parameters
let chain_id = state
let chain_id = step
.action
.chain_id
.expect("update client action should have a chain identifier");
let client_id = state
let client_id = step
.action
.client_id
.expect("update client action should have a client identifier");
let height = state
let height = step
.action
.height
.expect("update client action should have a height");
Expand All @@ -209,7 +209,7 @@ impl modelator::TestExecutor<State> for ICS02TestExecutor {
let result = ctx.deliver(msg);

// check the expected outcome
match state.action_outcome {
match step.action_outcome {
ActionOutcome::ICS02UpdateOK => {
// the implementaion matches the model if no error occurs
result.is_ok()
Expand All @@ -236,15 +236,15 @@ impl modelator::TestExecutor<State> for ICS02TestExecutor {
}
ActionType::ICS03ConnectionOpenInit => {
// get action parameters
let chain_id = state
let chain_id = step
.action
.chain_id
.expect("connection open init action should have a chain identifier");
let client_id = state
let client_id = step
.action
.client_id
.expect("connection open init action should have a client identifier");
let counterparty_client_id = state.action.counterparty_client_id.expect(
let counterparty_client_id = step.action.counterparty_client_id.expect(
"connection open init action should have a counterparty client identifier",
);

Expand All @@ -264,7 +264,7 @@ impl modelator::TestExecutor<State> for ICS02TestExecutor {
let result = ctx.deliver(msg);

// check the expected outcome
match state.action_outcome {
match step.action_outcome {
ActionOutcome::ICS03ConnectionOpenInitOK => {
// the implementaion matches the model if no error occurs
result.is_ok()
Expand Down
35 changes: 17 additions & 18 deletions modules/tests/modelator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use std::io::BufReader;
use std::path::Path;

pub trait TestExecutor<S> {
fn check_initial_state(&mut self, state: S) -> bool;

fn check_next_state(&mut self, state: S) -> bool;
fn initial_step(&mut self, step: S) -> bool;
fn next_step(&mut self, step: S) -> bool;
}

pub fn test_driver<E, S, P>(mut executor: E, path: P) -> Result<()>
pub fn test_driver<Executor, Step, P>(mut executor: Executor, path: P) -> Result<()>
where
E: TestExecutor<S> + Debug,
S: DeserializeOwned + Debug + Clone,
Executor: TestExecutor<Step> + Debug,
Step: DeserializeOwned + Debug + Clone,
P: AsRef<Path>,
{
// open test file
Expand All @@ -23,27 +22,27 @@ where
let reader = BufReader::new(file);

// parse test file
let states: Vec<S> = serde_json::de::from_reader(reader)
let steps: Vec<Step> = serde_json::de::from_reader(reader)
.wrap_err_with(|| format!("test file {:?} could not be deserialized", path.as_ref()))?;

let mut states = states.into_iter();
let mut steps = steps.into_iter();

// check the initial state
if let Some(state) = states.next() {
if !executor.check_initial_state(state.clone()) {
return Err(eyre!("check failed on initial state:\n{:#?}", state));
// check the initial step
if let Some(step) = steps.next() {
if !executor.initial_step(step.clone()) {
return Err(eyre!("check failed on initial step:\n{:#?}", step));
}
} else {
println!("WARNING: test file {:?} had 0 states", path.as_ref());
println!("WARNING: test file {:?} had 0 steps", path.as_ref());
return Ok(());
}

// check all the remaining states
for state in states {
if !executor.check_next_state(state.clone()) {
// check the remaining steps
for step in steps {
if !executor.next_step(step.clone()) {
return Err(eyre!(
"check failed on state:\n{:#?}\n\nexecutor:\n{:#?}",
state,
"check failed on step:\n{:#?}\n\nexecutor:\n{:#?}",
step,
executor
));
}
Expand Down
2 changes: 1 addition & 1 deletion modules/tests/state.rs → modules/tests/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Deserialize;
use std::fmt::Debug;

#[derive(Debug, Clone, Deserialize)]
pub struct State {
pub struct Step {
pub action: Action,

#[serde(alias = "actionOutcome")]
Expand Down

0 comments on commit 7755fec

Please sign in to comment.