Skip to content

Commit

Permalink
Deduplicate test code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgparity committed Nov 21, 2022
1 parent 7afe2db commit ba46adb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 91 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 1 addition & 25 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,34 +656,10 @@ mod tests {
time::{Duration, Instant},
};
use substrate_test_runtime_client::{
runtime::{Header, H256},
runtime::{Header, TestInherentDataProvider, H256},
TestClient,
};

#[derive(Clone)]
struct TestInherentDataProvider;

const ERROR_TO_STRING: &str = "Found error!";
const TEST_INHERENT_0: sp_inherents::InherentIdentifier = *b"testinh0";

#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for TestInherentDataProvider {
async fn provide_inherent_data(
&self,
data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
data.put_data(TEST_INHERENT_0, &42)
}

async fn try_handle_error(
&self,
_: &sp_inherents::InherentIdentifier,
_: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
Some(Err(sp_inherents::Error::Application(Box::from(ERROR_TO_STRING))))
}
}

const SLOT_DURATION_MS: u64 = 1000;

type Error = sp_blockchain::Error;
Expand Down
24 changes: 1 addition & 23 deletions client/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,34 +810,12 @@ impl<N> BackoffAuthoringBlocksStrategy<N> for () {
#[cfg(test)]
mod test {
use super::*;
use sp_inherents::{Error, InherentData, InherentDataProvider, InherentIdentifier};
use sp_runtime::traits::NumberFor;
use std::time::{Duration, Instant};
use substrate_test_runtime_client::runtime::{Block, Header};
use substrate_test_runtime_client::runtime::{Block, Header, TestInherentDataProvider};

const SLOT_DURATION: Duration = Duration::from_millis(6000);

#[derive(Clone)]
struct TestInherentDataProvider;

const ERROR_TO_STRING: &str = "Found error!";
const TEST_INHERENT_0: InherentIdentifier = *b"testinh0";

#[async_trait::async_trait]
impl InherentDataProvider for TestInherentDataProvider {
async fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> {
data.put_data(TEST_INHERENT_0, &42)
}

async fn try_handle_error(
&self,
_: &InherentIdentifier,
_: &[u8],
) -> Option<Result<(), Error>> {
Some(Err(Error::Application(Box::from(ERROR_TO_STRING))))
}
}

fn slot(slot: u64) -> super::slots::SlotInfo<Block> {
super::slots::SlotInfo {
slot: slot.into(),
Expand Down
1 change: 1 addition & 0 deletions primitives/inherents/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sp-std = { version = "5.0.0", default-features = false, path = "../std" }

[dev-dependencies]
futures = "0.3.21"
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }

[features]
default = [ "std" ]
Expand Down
23 changes: 1 addition & 22 deletions primitives/inherents/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,30 +436,9 @@ mod tests {
assert!(data.put_data(TEST_INHERENT_0, &10).is_err());
}

#[derive(Clone)]
struct TestInherentDataProvider;

const ERROR_TO_STRING: &str = "Found error!";

#[async_trait::async_trait]
impl InherentDataProvider for TestInherentDataProvider {
async fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> {
data.put_data(TEST_INHERENT_0, &42)
}

async fn try_handle_error(
&self,
_: &InherentIdentifier,
_: &[u8],
) -> Option<Result<(), Error>> {
Some(Err(Error::Application(Box::from(ERROR_TO_STRING))))
}
}

#[test]
fn create_inherent_data() {
let provider = TestInherentDataProvider;

let provider = substrate_test_runtime_client::runtime::TestInherentDataProvider;
let inherent_data = futures::executor::block_on(provider.create_inherent_data()).unwrap();

assert_eq!(inherent_data.get_data::<u32>(&TEST_INHERENT_0).unwrap().unwrap(), 42u32);
Expand Down
46 changes: 25 additions & 21 deletions test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use frame_support::{
use frame_system::limits::{BlockLength, BlockWeights};
use sp_api::{decl_runtime_apis, impl_runtime_apis};
pub use sp_core::hash::H256;
use sp_inherents::{CheckInherentsResult, InherentData};
use sp_inherents::{CheckInherentsResult, InherentData, InherentIdentifier};
#[cfg(feature = "std")]
use sp_runtime::traits::NumberFor;
use sp_runtime::{
Expand Down Expand Up @@ -1402,23 +1402,27 @@ mod tests {
}
}

//#[derive(Clone)]
//struct TestInherentDataProvider;
//
//const ERROR_TO_STRING: &str = "Found error!";
//const TEST_INHERENT_0: InherentIdentifier = *b"testinh0";
//
//#[async_trait::async_trait]
//impl sp_inherents::InherentDataProvider for TestInherentDataProvider {
// async fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> {
// data.put_data(TEST_INHERENT_0, &42)
// }
//
// async fn try_handle_error(
// &self,
// _: &InherentIdentifier,
// _: &[u8],
// ) -> Option<Result<(), Error>> {
// Some(Err(Error::Application(Box::from(ERROR_TO_STRING))))
// }
//}
#[derive(Clone)]
pub struct TestInherentDataProvider;

const ERROR_TO_STRING: &str = "Found error!";
const TEST_INHERENT_0: InherentIdentifier = *b"testinh0";

#[cfg(feature = "std")]
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for TestInherentDataProvider {
async fn provide_inherent_data(
&self,
data: &mut InherentData,
) -> Result<(), sp_inherents::Error> {
data.put_data(TEST_INHERENT_0, &42)
}

async fn try_handle_error(
&self,
_: &InherentIdentifier,
_: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
Some(Err(sp_inherents::Error::Application(Box::from(ERROR_TO_STRING))))
}
}

0 comments on commit ba46adb

Please sign in to comment.