Skip to content
Snippets Groups Projects
Unverified Commit 496dfedf authored by brettkolodny's avatar brettkolodny Committed by GitHub
Browse files

Migrated mock to construct_runtime macro (#369)

parent 15b38185
No related branches found
No related tags found
No related merge requests found
...@@ -43,7 +43,7 @@ impl frame_system::Config for Runtime { ...@@ -43,7 +43,7 @@ impl frame_system::Config for Runtime {
type BlockWeights = (); type BlockWeights = ();
type BlockLength = (); type BlockLength = ();
type Version = (); type Version = ();
type PalletInfo = (); type PalletInfo = PalletInfo;
type AccountData = (); type AccountData = ();
type OnNewAccount = (); type OnNewAccount = ();
type OnKilledAccount = (); type OnKilledAccount = ();
......
...@@ -8,6 +8,7 @@ authors = ["Laminar Developers <hello@laminar.one>"] ...@@ -8,6 +8,7 @@ authors = ["Laminar Developers <hello@laminar.one>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
serde = { version = "1.0.111", optional = true }
paste = "0.1.16" paste = "0.1.16"
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
sp-api = { version = "2.0.1", default-features = false } sp-api = { version = "2.0.1", default-features = false }
...@@ -25,6 +26,7 @@ hex-literal = "0.2.1" ...@@ -25,6 +26,7 @@ hex-literal = "0.2.1"
[features] [features]
default = [ "std" ] default = [ "std" ]
std = [ std = [
"serde",
"codec/std", "codec/std",
"sp-runtime-interface/std", "sp-runtime-interface/std",
"sp-runtime/std", "sp-runtime/std",
......
...@@ -4,43 +4,48 @@ ...@@ -4,43 +4,48 @@
use super::*; use super::*;
use frame_benchmarking::account; use frame_benchmarking::account;
use frame_support::{ use frame_support::{assert_err, assert_ok, construct_runtime, ensure};
assert_err, assert_ok, decl_module, decl_storage, dispatch::DispatchResult, ensure, impl_outer_origin, use frame_system::RawOrigin;
};
use frame_system::{ensure_none, ensure_signed, RawOrigin};
use sp_runtime::{ use sp_runtime::{
testing::{Header, H256}, testing::{Header, H256},
traits::{BlakeTwo256, IdentityLookup}, traits::{BlakeTwo256, IdentityLookup},
}; };
use sp_std::prelude::*; use sp_std::prelude::*;
decl_storage! { mod test {
trait Store for Module<T: Config> as Test { use frame_support::{decl_module, decl_storage, dispatch::DispatchResult};
Value get(fn value): Option<u32>; use frame_system::{ensure_none, ensure_signed};
use sp_std::prelude::*;
pub trait Config: frame_system::Config {
type Event;
type BlockNumber;
} }
}
decl_module! { decl_storage! {
pub struct Module<T: Config> for enum Call where origin: T::Origin { trait Store for Module<T: Config> as Test {
#[weight = 0] pub Value get(fn value) config(): Option<u32>;
fn set_value(origin, n: u32) -> DispatchResult {
let _sender = ensure_signed(origin)?;
Value::put(n);
Ok(())
} }
}
#[weight = 0] decl_module! {
fn dummy(origin, _n: u32) -> DispatchResult { pub struct Module<T: Config> for enum Call where origin: T::Origin {
let _sender = ensure_none(origin)?; #[weight = 0]
Ok(()) fn set_value(origin, n: u32) -> DispatchResult {
let _sender = ensure_signed(origin)?;
Value::put(n);
Ok(())
}
#[weight = 0]
fn dummy(origin, _n: u32) -> DispatchResult {
let _sender = ensure_none(origin)?;
Ok(())
}
} }
} }
} }
impl_outer_origin! {
pub enum Origin for Test {}
}
pub trait Config: frame_system::Config { pub trait Config: frame_system::Config {
type Event; type Event;
type BlockNumber; type BlockNumber;
...@@ -48,26 +53,23 @@ pub trait Config: frame_system::Config { ...@@ -48,26 +53,23 @@ pub trait Config: frame_system::Config {
type AccountId = u128; type AccountId = u128;
#[derive(Clone, Eq, PartialEq)]
pub struct Test;
impl frame_system::Config for Test { impl frame_system::Config for Test {
type Origin = Origin; type Origin = Origin;
type Index = u64; type Index = u64;
type BlockNumber = u64; type BlockNumber = u64;
type Hash = H256; type Hash = H256;
type Call = (); type Call = Call;
type Hashing = BlakeTwo256; type Hashing = BlakeTwo256;
type AccountId = AccountId; type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>; type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header; type Header = Header;
type Event = (); type Event = Event;
type BlockHashCount = (); type BlockHashCount = ();
type DbWeight = (); type DbWeight = ();
type BlockWeights = (); type BlockWeights = ();
type BlockLength = (); type BlockLength = ();
type Version = (); type Version = ();
type PalletInfo = (); type PalletInfo = PalletInfo;
type AccountData = (); type AccountData = ();
type OnNewAccount = (); type OnNewAccount = ();
type OnKilledAccount = (); type OnKilledAccount = ();
...@@ -76,11 +78,31 @@ impl frame_system::Config for Test { ...@@ -76,11 +78,31 @@ impl frame_system::Config for Test {
type SS58Prefix = (); type SS58Prefix = ();
} }
impl tests::test::Config for Test {
type Event = Event;
type BlockNumber = u32;
}
impl Config for Test { impl Config for Test {
type Event = (); type Event = Event;
type BlockNumber = u32; type BlockNumber = u32;
} }
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, u32, ()>;
construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Module, Call, Storage, Config, Event<T>},
Pallet: test::{Module, Call, Storage, Config},
}
);
// This function basically just builds a genesis storage key/value store // This function basically just builds a genesis storage key/value store
// according to our desired mockup. // according to our desired mockup.
fn new_test_ext() -> sp_io::TestExternalities { fn new_test_ext() -> sp_io::TestExternalities {
...@@ -91,7 +113,7 @@ fn new_test_ext() -> sp_io::TestExternalities { ...@@ -91,7 +113,7 @@ fn new_test_ext() -> sp_io::TestExternalities {
} }
runtime_benchmarks! { runtime_benchmarks! {
{ Test, self } { Test, test }
_ { _ {
// Define a common range for `b`. // Define a common range for `b`.
...@@ -103,7 +125,7 @@ runtime_benchmarks! { ...@@ -103,7 +125,7 @@ runtime_benchmarks! {
let caller = account::<AccountId>("caller", 0, 0); let caller = account::<AccountId>("caller", 0, 0);
}: _ (RawOrigin::Signed(caller), b.into()) }: _ (RawOrigin::Signed(caller), b.into())
verify { verify {
assert_eq!(Value::get(), Some(b)); assert_eq!(Pallet::value(), Some(b));
} }
other_name { other_name {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment