Skip to content
Snippets Groups Projects
mock.rs 3.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • //! Mocks for the tokens module.
    
    #![cfg(test)]
    
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
    use frame_system as system;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    use sp_core::H256;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    use sp_runtime::{testing::Header, traits::IdentityLookup, Perbill};
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    use sp_std::cell::RefCell;
    
    use std::collections::HashMap;
    
    
    use super::*;
    
    impl_outer_origin! {
    	pub enum Origin for Runtime {}
    }
    
    mod tokens {
    	pub use crate::Event;
    }
    
    impl_outer_event! {
    	pub enum TestEvent for Runtime {
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    		frame_system<T>,
    
    		tokens<T>,
    	}
    }
    
    // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
    #[derive(Clone, PartialEq, Eq, Debug)]
    pub struct Runtime;
    parameter_types! {
    	pub const BlockHashCount: u64 = 250;
    	pub const MaximumBlockWeight: u32 = 1024;
    	pub const MaximumBlockLength: u32 = 2 * 1024;
    	pub const AvailableBlockRatio: Perbill = Perbill::one();
    }
    
    
    type AccountId = u128;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    impl frame_system::Trait for Runtime {
    
    	type Origin = Origin;
    
    	type Index = u64;
    	type BlockNumber = u64;
    	type Hash = H256;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type Hashing = ::sp_runtime::traits::BlakeTwo256;
    
    	type AccountId = AccountId;
    	type Lookup = IdentityLookup<Self::AccountId>;
    	type Header = Header;
    	type Event = TestEvent;
    	type BlockHashCount = BlockHashCount;
    	type MaximumBlockWeight = MaximumBlockWeight;
    	type MaximumBlockLength = MaximumBlockLength;
    	type AvailableBlockRatio = AvailableBlockRatio;
    	type Version = ();
    
    	type ModuleToIndex = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type AccountData = ();
    	type OnNewAccount = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type OnKilledAccount = ();
    
    	type DbWeight = ();
    	type BlockExecutionWeight = ();
    	type ExtrinsicBaseWeight = ();
    
    	type MaximumExtrinsicWeight = ();
    
    	type BaseCallFilter = ();
    
    }
    pub type System = system::Module<Runtime>;
    
    type CurrencyId = u32;
    pub type Balance = u64;
    
    thread_local! {
    	pub static ACCUMULATED_RECEIVED: RefCell<HashMap<(AccountId, CurrencyId), Balance>> = RefCell::new(HashMap::new());
    }
    
    pub struct MockOnReceived;
    impl OnReceived<AccountId, CurrencyId, Balance> for MockOnReceived {
    
    	fn on_received(who: &AccountId, currency_id: CurrencyId, amount: Balance) {
    
    		ACCUMULATED_RECEIVED.with(|v| {
    			let mut old_map = v.borrow().clone();
    
    			if let Some(before) = old_map.get_mut(&(*who, currency_id)) {
    
    				*before += amount;
    			} else {
    
    				old_map.insert((*who, currency_id), amount);
    
    			};
    
    			*v.borrow_mut() = old_map;
    		});
    	}
    }
    
    
    impl Trait for Runtime {
    	type Event = TestEvent;
    	type Balance = Balance;
    
    	type CurrencyId = CurrencyId;
    
    	type OnReceived = MockOnReceived;
    
    }
    
    pub type Tokens = Module<Runtime>;
    
    pub const TEST_TOKEN_ID: CurrencyId = 1;
    pub const ALICE: AccountId = 1;
    pub const BOB: AccountId = 2;
    
    pub const ID_1: LockIdentifier = *b"1       ";
    pub const ID_2: LockIdentifier = *b"2       ";
    
    
    pub struct ExtBuilder {
    
    	endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
    
    }
    
    impl Default for ExtBuilder {
    	fn default() -> Self {
    		Self {
    
    	pub fn balances(mut self, endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>) -> Self {
    		self.endowed_accounts = endowed_accounts;
    
    		self
    	}
    
    	pub fn one_hundred_for_alice_n_bob(self) -> Self {
    
    		self.balances(vec![(ALICE, TEST_TOKEN_ID, 100), (BOB, TEST_TOKEN_ID, 100)])
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	pub fn build(self) -> sp_io::TestExternalities {
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    		let mut t = frame_system::GenesisConfig::default()
    
    			.build_storage::<Runtime>()
    			.unwrap();
    
    
    		GenesisConfig::<Runtime> {
    			endowed_accounts: self.endowed_accounts,
    		}
    		.assimilate_storage(&mut t)
    		.unwrap();
    
    		t.into()
    	}
    }