From a6b3919fa4a57416d07dc71ac0f72ed047c1c972 Mon Sep 17 00:00:00 2001 From: Tarek Mohamed Abdalla Date: Fri, 17 Feb 2023 08:59:46 +0200 Subject: [PATCH] Use cfg_if create to make it more readable --- Cargo.lock | 1 + pallets/roles/Cargo.toml | 1 + pallets/roles/src/benchmarking.rs | 41 ++++++++++++++++--------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb090e51..5b2698c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5898,6 +5898,7 @@ dependencies = [ name = "pallet-roles" version = "0.1.7" dependencies = [ + "cfg-if 1.0.0", "frame-benchmarking", "frame-support", "frame-system", diff --git a/pallets/roles/Cargo.toml b/pallets/roles/Cargo.toml index cd089cd1..92ed9ffb 100644 --- a/pallets/roles/Cargo.toml +++ b/pallets/roles/Cargo.toml @@ -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' } diff --git a/pallets/roles/src/benchmarking.rs b/pallets/roles/src/benchmarking.rs index 57c63dd2..f6791d8b 100644 --- a/pallets/roles/src/benchmarking.rs +++ b/pallets/roles/src/benchmarking.rs @@ -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; @@ -17,37 +18,37 @@ use subsocial_support::{ use super::*; -#[cfg(not(test))] fn get_dummy_space_id( + #[allow(unused_variables)] origin: RawOrigin, ) -> Result { - let space_id = pallet_spaces::NextSpaceId::::get(); + cfg_if! { + if #[cfg(test)] { + Ok(crate::mock::SPACE1) + } else { + let space_id = pallet_spaces::NextSpaceId::::get(); - pallet_spaces::Pallet::::create_space(origin.into(), Content::None, None)?; + pallet_spaces::Pallet::::create_space(origin.into(), Content::None, None)?; - let space = pallet_spaces::SpaceById::::get(space_id) - .ok_or(DispatchError::Other("Space not found"))?; + let space = pallet_spaces::SpaceById::::get(space_id) + .ok_or(DispatchError::Other("Space not found"))?; - Ok(space.id) -} - -#[cfg(test)] -fn get_dummy_space_id( - _origin: RawOrigin, -) -> Result { - Ok(crate::mock::SPACE1) + Ok(space.id) + } + } } -#[cfg(not(test))] fn get_caller_account() -> T::AccountId { - account::("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::("Acc1", 1, 0) + } + } } -#[cfg(test)] -fn get_caller_account() -> 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(num_of_users: u32) -> Vec> { let mut users_to_grant = Vec::>::new();