Skip to content

Commit

Permalink
Use cfg_if create to make it more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
TarekkMA committed Feb 17, 2023
1 parent eec3d10 commit a6b3919
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 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.

1 change: 1 addition & 0 deletions pallets/roles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ try-runtime = ['frame-support/try-runtime']
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
cfg-if = "1.0.0"

# Local dependencies
pallet-permissions = { default-features = false, path = '../permissions' }
Expand Down
41 changes: 21 additions & 20 deletions pallets/roles/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// FIXME: refactor once SpacesInterface is added.

use cfg_if::cfg_if;
use frame_benchmarking::{account, benchmarks};
use frame_support::dispatch::DispatchError;
use frame_system::RawOrigin;
Expand All @@ -17,37 +18,37 @@ use subsocial_support::{

use super::*;

#[cfg(not(test))]
fn get_dummy_space_id<T: Config + pallet_spaces::Config>(
#[allow(unused_variables)]
origin: RawOrigin<T::AccountId>,
) -> Result<SpaceId, DispatchError> {
let space_id = pallet_spaces::NextSpaceId::<T>::get();
cfg_if! {
if #[cfg(test)] {
Ok(crate::mock::SPACE1)
} else {
let space_id = pallet_spaces::NextSpaceId::<T>::get();

pallet_spaces::Pallet::<T>::create_space(origin.into(), Content::None, None)?;
pallet_spaces::Pallet::<T>::create_space(origin.into(), Content::None, None)?;

let space = pallet_spaces::SpaceById::<T>::get(space_id)
.ok_or(DispatchError::Other("Space not found"))?;
let space = pallet_spaces::SpaceById::<T>::get(space_id)
.ok_or(DispatchError::Other("Space not found"))?;

Ok(space.id)
}

#[cfg(test)]
fn get_dummy_space_id<T: Config>(
_origin: RawOrigin<T::AccountId>,
) -> Result<SpaceId, DispatchError> {
Ok(crate::mock::SPACE1)
Ok(space.id)
}
}
}

#[cfg(not(test))]
fn get_caller_account<T: Config>() -> T::AccountId {
account::<T::AccountId>("Acc1", 1, 0)
cfg_if! {
if #[cfg(test)] {
let mut bytes: &[u8] = &crate::mock::ACCOUNT1.to_le_bytes();
T::AccountId::decode(&mut bytes).expect("failed to get caller_account")
} else {
account::<T::AccountId>("Acc1", 1, 0)
}
}
}

#[cfg(test)]
fn get_caller_account<T: Config>() -> T::AccountId {
let mut bytes: &[u8] = &crate::mock::ACCOUNT1.to_le_bytes();
T::AccountId::decode(&mut bytes).expect("failed to get caller_account")
}

fn dummy_list_of_users<T: Config>(num_of_users: u32) -> Vec<User<T::AccountId>> {
let mut users_to_grant = Vec::<User<T::AccountId>>::new();
Expand Down

0 comments on commit a6b3919

Please sign in to comment.