From a8745ef672410a9f311635333e4e9781cd4a9780 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Fri, 22 Dec 2023 13:04:48 +0100 Subject: [PATCH] Dedicated test for `composite_enum` and variant count --- .../support/test/tests/composite_enum.rs | 180 ++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 substrate/frame/support/test/tests/composite_enum.rs diff --git a/substrate/frame/support/test/tests/composite_enum.rs b/substrate/frame/support/test/tests/composite_enum.rs new file mode 100644 index 000000000000..674c1e47e34e --- /dev/null +++ b/substrate/frame/support/test/tests/composite_enum.rs @@ -0,0 +1,180 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! General tests for composite_enum macro and its handling, test for: +//! * variant_count works + +#![recursion_limit = "128"] + +use codec::Encode; +use frame_support::derive_impl; +use frame_support::traits::VariantCount; +use sp_core::sr25519; +use sp_runtime::{ + generic, + traits::{BlakeTwo256, Verify}, +}; + +#[frame_support::pallet(dev_mode)] +mod module_single_instance { + + #[pallet::composite_enum] + pub enum HoldReason { + ModuleSingleInstanceReason1, + ModuleSingleInstanceReason2, + } + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeHoldReason: From; + } +} + +#[frame_support::pallet(dev_mode)] +mod module_multi_instance { + + #[pallet::composite_enum] + pub enum HoldReason { + ModuleMultiInstanceReason1, + ModuleMultiInstanceReason2, + ModuleMultiInstanceReason3, + } + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeHoldReason: From>; + } +} + +#[frame_support::pallet(dev_mode)] +mod module_consumer { + use super::*; + + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::config] + pub trait Config: frame_system::Config { + // consume `composite_enum` + type RuntimeHoldReason: VariantCount; + } +} + +pub type BlockNumber = u64; +pub type Signature = sr25519::Signature; +pub type AccountId = ::Signer; +pub type Header = generic::Header; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; +pub type Block = generic::Block; + +frame_support::construct_runtime!( + pub struct Runtime + { + System: frame_system::{Pallet, Call, Event, Origin} = 30, + ModuleSingleInstance: module_single_instance::{HoldReason}, + ModuleMultiInstance1: module_multi_instance::::{HoldReason} = 51, + ModuleMultiInstance2: module_multi_instance::::{HoldReason} = 52, + ModuleMultiInstance3: module_multi_instance::::{HoldReason} = 53, + ModuleConsumer: module_consumer, + } +); + +#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] +impl frame_system::Config for Runtime { + type Block = Block; +} + +impl module_single_instance::Config for Runtime { + type RuntimeHoldReason = RuntimeHoldReason; +} + +impl module_multi_instance::Config for Runtime { + type RuntimeHoldReason = RuntimeHoldReason; +} +impl module_multi_instance::Config for Runtime { + type RuntimeHoldReason = RuntimeHoldReason; +} +impl module_multi_instance::Config for Runtime { + type RuntimeHoldReason = RuntimeHoldReason; +} + +impl module_consumer::Config for Runtime { + type RuntimeHoldReason = RuntimeHoldReason; +} + +fn list_all_variants() -> Vec { + let variants = vec![ + RuntimeHoldReason::ModuleSingleInstance(module_single_instance::HoldReason::ModuleSingleInstanceReason1), + RuntimeHoldReason::ModuleSingleInstance(module_single_instance::HoldReason::ModuleSingleInstanceReason2), + RuntimeHoldReason::ModuleMultiInstance1(module_multi_instance::HoldReason::::ModuleMultiInstanceReason1), + RuntimeHoldReason::ModuleMultiInstance1(module_multi_instance::HoldReason::::ModuleMultiInstanceReason2), + RuntimeHoldReason::ModuleMultiInstance1(module_multi_instance::HoldReason::::ModuleMultiInstanceReason3), + RuntimeHoldReason::ModuleMultiInstance2(module_multi_instance::HoldReason::::ModuleMultiInstanceReason1), + RuntimeHoldReason::ModuleMultiInstance2(module_multi_instance::HoldReason::::ModuleMultiInstanceReason2), + RuntimeHoldReason::ModuleMultiInstance2(module_multi_instance::HoldReason::::ModuleMultiInstanceReason3), + RuntimeHoldReason::ModuleMultiInstance3(module_multi_instance::HoldReason::::ModuleMultiInstanceReason1), + RuntimeHoldReason::ModuleMultiInstance3(module_multi_instance::HoldReason::::ModuleMultiInstanceReason2), + RuntimeHoldReason::ModuleMultiInstance3(module_multi_instance::HoldReason::::ModuleMultiInstanceReason3), + ]; + // check - just in case + for v in &variants { + assert!(match v { + RuntimeHoldReason::ModuleSingleInstance(inner) => match inner { + module_single_instance::HoldReason::ModuleSingleInstanceReason1 => true, + module_single_instance::HoldReason::ModuleSingleInstanceReason2 => true, + } + RuntimeHoldReason::ModuleMultiInstance1(inner) => match inner { + module_multi_instance::HoldReason::::ModuleMultiInstanceReason1 + | module_multi_instance::HoldReason::::ModuleMultiInstanceReason2 + | module_multi_instance::HoldReason::::ModuleMultiInstanceReason3 => true, + module_multi_instance::HoldReason::::__Ignore(_) => false, + } + RuntimeHoldReason::ModuleMultiInstance2(inner) => match inner { + module_multi_instance::HoldReason::::ModuleMultiInstanceReason1 + | module_multi_instance::HoldReason::::ModuleMultiInstanceReason2 + | module_multi_instance::HoldReason::::ModuleMultiInstanceReason3=> true, + module_multi_instance::HoldReason::::__Ignore(_)=> false, + } + RuntimeHoldReason::ModuleMultiInstance3(inner) => match inner { + module_multi_instance::HoldReason::::ModuleMultiInstanceReason1 + | module_multi_instance::HoldReason::::ModuleMultiInstanceReason2 + | module_multi_instance::HoldReason::::ModuleMultiInstanceReason3=> true, + module_multi_instance::HoldReason::::__Ignore(_)=> false, + } + }); + } + variants +} + +#[test] +fn runtime_hold_reason_variant_count_works() { + assert_eq!(RuntimeHoldReason::VARIANT_COUNT as usize, list_all_variants().len()); +} + +#[test] +fn check_unique_encodings_for_composite_enums() { + let variants = list_all_variants(); + let encoded_variants = + variants.iter().map(|v| v.encode()).collect::>(); + assert_eq!(encoded_variants.len(), variants.len()); +}