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 all 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
7 changes: 0 additions & 7 deletions frame/benchmarking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,21 @@ mod pallet_test {
}

#[pallet::storage]
#[pallet::getter(fn value)]
pub(crate) type Value<T: Config> = StorageValue<_, u32, OptionQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn set_value(origin: OriginFor<T>, n: u32) -> DispatchResult {
let _sender = ensure_signed(origin)?;
Value::<T>::put(n);
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(0)]
pub fn dummy(origin: OriginFor<T>, _n: u32) -> DispatchResult {
let _sender = ensure_none(origin)?;
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(0)]
pub fn always_error(_origin: OriginFor<T>) -> DispatchResult {
return Err("I always fail".into())
}
Expand Down
1 change: 0 additions & 1 deletion frame/benchmarking/src/tests_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ mod pallet_test {
}

#[pallet::storage]
#[pallet::getter(fn value)]
pub(crate) type Value<T: Config<I>, I: 'static = ()> = StorageValue<_, u32, OptionQuery>;

#[pallet::event]
Expand Down
2 changes: 0 additions & 2 deletions frame/collective/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ mod mock_democracy {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn external_propose_majority(origin: OriginFor<T>) -> DispatchResult {
T::ExternalMajorityOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::<T>::ExternalProposed);
Expand Down
12 changes: 0 additions & 12 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,51 +735,39 @@ mod tests {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(100)]
pub fn some_function(origin: OriginFor<T>) -> DispatchResult {
// NOTE: does not make any different.
frame_system::ensure_signed(origin)?;
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight((200, DispatchClass::Operational))]
pub fn some_root_operation(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_root(origin)?;
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(0)]
pub fn some_unsigned_message(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_none(origin)?;
Ok(())
}

#[pallet::call_index(3)]
#[pallet::weight(0)]
pub fn allowed_unsigned(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_root(origin)?;
Ok(())
}

#[pallet::call_index(4)]
#[pallet::weight(0)]
pub fn unallowed_unsigned(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_root(origin)?;
Ok(())
}

#[pallet::call_index(5)]
#[pallet::weight((0, DispatchClass::Mandatory))]
pub fn inherent_call(origin: OriginFor<T>) -> DispatchResult {
frame_system::ensure_none(origin)?;
Ok(())
}

#[pallet::call_index(6)]
#[pallet::weight(0)]
pub fn calculate_storage_root(_origin: OriginFor<T>) -> DispatchResult {
let root = sp_io::storage::root(sp_runtime::StateVersion::V1);
sp_io::storage::set("storage_root".as_bytes(), &root);
Expand Down
4 changes: 2 additions & 2 deletions frame/support/test/compile_pass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../" }
renamed-frame-support = { package = "frame-support", version = "4.0.0-dev", default-features = false, path = "../../" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../../system" }
sp-core = { version = "7.0.0", default-features = false, path = "../../../../primitives/core" }
sp-runtime = { version = "7.0.0", default-features = false, path = "../../../../primitives/runtime" }
Expand All @@ -24,7 +24,7 @@ sp-version = { version = "5.0.0", default-features = false, path = "../../../../
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"renamed-frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
Expand Down
12 changes: 5 additions & 7 deletions frame/support/test/compile_pass/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Test that `construct_runtime!` also works when `frame-support` is renamed in the `Cargo.toml`.

#![cfg_attr(not(feature = "std"), no_std)]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]
//! This crate tests that `construct_runtime!` expands the pallet parts
//! correctly even when frame-support is renamed in Cargo.toml

use frame_support::{
use renamed_frame_support::{
construct_runtime, parameter_types,
traits::{ConstU16, ConstU32, ConstU64},
traits::{ConstU16, ConstU32, ConstU64, Everything},
};
use sp_core::{sr25519, H256};
use sp_runtime::{
Expand Down Expand Up @@ -54,7 +52,7 @@ parameter_types! {
}

impl frame_system::Config for Runtime {
type BaseCallFilter = frame_support::traits::Everything;
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type Index = u128;
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 @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Testing pallet macro
//! A basic pallet that can be used to test `construct_runtime!`.

// Ensure docs are propagated properly by the macros.
#![warn(missing_docs)]
Expand All @@ -24,10 +24,7 @@ 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>(_);
Expand All @@ -37,7 +34,7 @@ pub mod pallet {

/// 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
121 changes: 93 additions & 28 deletions frame/support/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,113 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Test crate for frame_support. Allow to make use of `frame_support::decl_storage`.
//! See tests directory.
//! Minimal pallet without `frame_system::Config`-super trait.

// Make sure we fail compilation on warnings
#![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>(_);

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")
}

/// Same semantic as [`frame_system`].
// Note: we cannot use [`frame_system`] here since the pallet does not depend on
// [`frame_system::Config`].
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.

2 changes: 1 addition & 1 deletion frame/support/test/tests/benchmark_ui/invalid_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod benches {
#[benchmark]
fn bench() {
#[extrinsic_call]
thing(1);
noop(1);
}
}

Expand Down
8 changes: 0 additions & 8 deletions frame/support/test/tests/benchmark_ui/invalid_origin.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
error[E0599]: no variant or associated item named `new_call_variant_thing` found for enum `frame_support_test::Call` in the current scope
--> tests/benchmark_ui/invalid_origin.rs:6:1
|
6 | #[benchmarks]
| ^^^^^^^^^^^^^ variant or associated item not found in `Call<T>`
|
= note: this error originates in the attribute macro `benchmarks` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `<T as frame_support_test::Config>::RuntimeOrigin: From<{integer}>` is not satisfied
--> tests/benchmark_ui/invalid_origin.rs:6:1
|
Expand Down
Loading