|
| 1 | +// Copyright 2019-2020 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Substrate. |
| 3 | + |
| 4 | +// Substrate is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Substrate is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Substrate. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +// Tests for Utility Pallet |
| 18 | + |
| 19 | +#![cfg(test)] |
| 20 | + |
| 21 | +use super::*; |
| 22 | + |
| 23 | +use frame_support::{ |
| 24 | + assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch, |
| 25 | + weights::Weight, impl_outer_event |
| 26 | +}; |
| 27 | +use sp_core::H256; |
| 28 | +use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header}; |
| 29 | +use crate as utility; |
| 30 | + |
| 31 | +impl_outer_origin! { |
| 32 | + pub enum Origin for Test where system = frame_system {} |
| 33 | +} |
| 34 | + |
| 35 | +impl_outer_event! { |
| 36 | + pub enum TestEvent for Test { |
| 37 | + system<T>, |
| 38 | + pallet_balances<T>, |
| 39 | + utility<T>, |
| 40 | + } |
| 41 | +} |
| 42 | +impl_outer_dispatch! { |
| 43 | + pub enum Call for Test where origin: Origin { |
| 44 | + frame_system::System, |
| 45 | + pallet_balances::Balances, |
| 46 | + utility::Utility, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// For testing the pallet, we construct most of a mock runtime. This means |
| 51 | +// first constructing a configuration type (`Test`) which `impl`s each of the |
| 52 | +// configuration traits of pallets we want to use. |
| 53 | +#[derive(Clone, Eq, PartialEq)] |
| 54 | +pub struct Test; |
| 55 | +parameter_types! { |
| 56 | + pub const BlockHashCount: u64 = 250; |
| 57 | + pub const MaximumBlockWeight: Weight = 1024; |
| 58 | + pub const MaximumBlockLength: u32 = 2 * 1024; |
| 59 | + pub const AvailableBlockRatio: Perbill = Perbill::one(); |
| 60 | +} |
| 61 | +impl frame_system::Trait for Test { |
| 62 | + type Origin = Origin; |
| 63 | + type Index = u64; |
| 64 | + type BlockNumber = u64; |
| 65 | + type Hash = H256; |
| 66 | + type Call = Call; |
| 67 | + type Hashing = BlakeTwo256; |
| 68 | + type AccountId = u64; |
| 69 | + type Lookup = IdentityLookup<Self::AccountId>; |
| 70 | + type Header = Header; |
| 71 | + type Event = TestEvent; |
| 72 | + type BlockHashCount = BlockHashCount; |
| 73 | + type MaximumBlockWeight = MaximumBlockWeight; |
| 74 | + type DbWeight = (); |
| 75 | + type MaximumBlockLength = MaximumBlockLength; |
| 76 | + type AvailableBlockRatio = AvailableBlockRatio; |
| 77 | + type Version = (); |
| 78 | + type ModuleToIndex = (); |
| 79 | + type AccountData = pallet_balances::AccountData<u64>; |
| 80 | + type OnNewAccount = (); |
| 81 | + type OnKilledAccount = (); |
| 82 | +} |
| 83 | +parameter_types! { |
| 84 | + pub const ExistentialDeposit: u64 = 1; |
| 85 | +} |
| 86 | +impl pallet_balances::Trait for Test { |
| 87 | + type Balance = u64; |
| 88 | + type Event = TestEvent; |
| 89 | + type DustRemoval = (); |
| 90 | + type ExistentialDeposit = ExistentialDeposit; |
| 91 | + type AccountStore = System; |
| 92 | +} |
| 93 | +parameter_types! { |
| 94 | + pub const MultisigDepositBase: u64 = 1; |
| 95 | + pub const MultisigDepositFactor: u64 = 1; |
| 96 | + pub const MaxSignatories: u16 = 3; |
| 97 | +} |
| 98 | +impl Trait for Test { |
| 99 | + type Event = TestEvent; |
| 100 | + type Currency = Balances; |
| 101 | +} |
| 102 | +type System = frame_system::Module<Test>; |
| 103 | +type Balances = pallet_balances::Module<Test>; |
| 104 | +type Utility = Module<Test>; |
| 105 | + |
| 106 | +use pallet_balances::Call as BalancesCall; |
| 107 | +use pallet_balances::Error as BalancesError; |
| 108 | + |
| 109 | +pub fn new_test_ext() -> sp_io::TestExternalities { |
| 110 | + let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap(); |
| 111 | + pallet_balances::GenesisConfig::<Test> { |
| 112 | + balances: vec![(1, 10), (2, 10), (3, 10), (4, 10), (5, 10)], |
| 113 | + }.assimilate_storage(&mut t).unwrap(); |
| 114 | + let mut ext = sp_io::TestExternalities::new(t); |
| 115 | + ext.execute_with(|| System::set_block_number(1)); |
| 116 | + ext |
| 117 | +} |
| 118 | + |
| 119 | +fn last_event() -> TestEvent { |
| 120 | + system::Module::<Test>::events().pop().map(|e| e.event).expect("Event expected") |
| 121 | +} |
| 122 | + |
| 123 | +fn expect_event<E: Into<TestEvent>>(e: E) { |
| 124 | + assert_eq!(last_event(), e.into()); |
| 125 | +} |
| 126 | + |
| 127 | +fn now() -> Timepoint<u64> { |
| 128 | + Utility::timepoint() |
| 129 | +} |
| 130 | + |
| 131 | +#[test] |
| 132 | +fn test() { |
| 133 | + new_test_ext().execute_with(|| { |
| 134 | + |
| 135 | + }); |
| 136 | +} |
| 137 | + |
0 commit comments