Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

frame-support-test: migrate tests from decl_* macros to the new pallet macros #12445

Merged
merged 21 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ std = [
"log/std",
"environmental/std",
]
runtime-benchmarks = []
runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks"
]
try-runtime = []
# By default some types have documentation, `no-metadata-docs` allows to reduce the documentation
# in the metadata.
Expand Down
7 changes: 2 additions & 5 deletions frame/support/test/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
#[allow(unused_imports)]
use frame_support::pallet_prelude::*;
#[allow(unused_imports)]
use frame_system::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(_);
pub struct Pallet<T>(PhantomData<T>);

#[pallet::config]
pub trait Config: frame_system::Config {}

/// I'm the documentation
#[pallet::storage]
pub type Value<T> = StorageValue<Value = u32>;
pub type Value<T> = StorageValue<_, u32>;

#[pallet::genesis_config]
#[cfg_attr(feature = "std", derive(Default))]
Expand Down
116 changes: 90 additions & 26 deletions frame/support/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,105 @@
#![warn(missing_docs)]
#![deny(warnings)]

/// The configuration trait
pub trait Config: 'static {
/// The runtime origin type.
type RuntimeOrigin: codec::Codec + codec::EncodeLike + Default + scale_info::TypeInfo;
/// The block number type.
type BlockNumber: codec::Codec + codec::EncodeLike + Default + scale_info::TypeInfo;
/// The information about the pallet setup in the runtime.
type PalletInfo: frame_support::traits::PalletInfo;
/// The db weights.
type DbWeight: frame_support::traits::Get<frame_support::weights::RuntimeDbWeight>;
}
pub use frame_support::dispatch::RawOrigin;

frame_support::decl_module! {
/// Some test module
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {}
}
pub use self::pallet::*;

#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
use crate::{self as frame_system, pallet_prelude::*};
use frame_support::pallet_prelude::*;

/// A PalletInfo implementation which just panics.
pub struct PanicPalletInfo;
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);

impl frame_support::traits::PalletInfo for PanicPalletInfo {
fn index<P: 'static>() -> Option<usize> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
/// The configuration trait
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static + Eq + Clone {
/// The block number type.
type BlockNumber: Parameter + Member + Default + MaybeSerializeDeserialize + MaxEncodedLen;
/// The account type.
type AccountId: Parameter + Member + MaxEncodedLen;
/// The basic call filter to use in Origin.
type BaseCallFilter: frame_support::traits::Contains<Self::RuntimeCall>;
/// The runtime origin type.
type RuntimeOrigin: Into<Result<RawOrigin<Self::AccountId>, Self::RuntimeOrigin>>
+ From<RawOrigin<Self::AccountId>>;
/// The runtime call type.
type RuntimeCall;
/// The runtime event type.
type RuntimeEvent: Parameter
+ Member
+ IsType<<Self as frame_system::Config>::RuntimeEvent>
+ From<Event<Self>>;
/// The information about the pallet setup in the runtime.
type PalletInfo: frame_support::traits::PalletInfo;
/// The db weights.
type DbWeight: Get<frame_support::weights::RuntimeDbWeight>;
}
fn name<P: 'static>() -> Option<&'static str> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");

#[pallet::call]
impl<T: Config> Pallet<T> {
/// A noop call.
pub fn noop(_origin: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
fn module_name<P: 'static>() -> Option<&'static str> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");

impl<T: Config> Pallet<T> {
/// A empty method.
pub fn deposit_event(_event: impl Into<T::RuntimeEvent>) {}
}
fn crate_version<P: 'static>() -> Option<frame_support::traits::CrateVersion> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");

/// The origin type.
#[pallet::origin]
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;

/// The error type.
#[pallet::error]
pub enum Error<T> {
/// Test error documentation
TestError,
/// Error documentation
/// with multiple lines
AnotherError,
/// Required by construct_runtime
CallFiltered,
}

/// The event type.
#[pallet::event]
pub enum Event<T: Config> {
/// The extrinsic is successful
ExtrinsicSuccess,
/// The extrinsic is failed
ExtrinsicFailed,
/// The ignored error
Ignore(<T as Config>::BlockNumber),
}
}

/// Ensure that the origin `o` represents the root. Returns `Ok` or an `Err` otherwise.
pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'static str>
where
OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
{
o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
}

/// Prelude to be used alongside pallet macro, for ease of use.
pub mod pallet_prelude {
pub use crate::ensure_root;

/// Type alias for the `Origin` associated type of system config.
pub type OriginFor<T> = <T as crate::Config>::RuntimeOrigin;

/// Type alias for the `BlockNumber` associated type of system config.
pub type BlockNumberFor<T> = <T as super::Config>::BlockNumber;
}

/// Provides an implementation of [`frame_support::traits::Randomness`] that should only be used in
/// tests!
pub struct TestRandomness<T>(sp_std::marker::PhantomData<T>);
Expand Down
29 changes: 0 additions & 29 deletions frame/support/test/src/pallet_version.rs

This file was deleted.

5 changes: 4 additions & 1 deletion frame/support/test/tests/benchmark_ui/invalid_origin.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0599]: no variant or associated item named `new_call_variant_thing` found
--> tests/benchmark_ui/invalid_origin.rs:6:1
|
6 | #[benchmarks]
| ^^^^^^^^^^^^^ variant or associated item not found in `Call<T>`
| ^^^^^^^^^^^^^
| |
| variant or associated item not found in `Call<T>`
| help: there is an associated function with a similar name: `new_call_variant_noop`
|
= note: this error originates in the attribute macro `benchmarks` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
Loading