1234567891011use support::{decl_module, decl_storage, decl_event, dispatch::Result};12use system::ensure_signed;131415pub trait Trait: system::Trait {16 1718 19 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;20}212223decl_storage! {24 trait Store for Module<T: Trait> as TemplateModule {25 26 27 28 Something get(something): Option<u32>;29 }30}313233decl_module! {34 35 pub struct Module<T: Trait> for enum Call where origin: T::Origin {36 37 38 fn deposit_event() = default;3940 41 42 43 pub fn do_something(origin, something: u32) -> Result {44 45 let who = ensure_signed(origin)?;4647 48 49 Something::put(something);5051 52 Self::deposit_event(RawEvent::SomethingStored(something, who));53 Ok(())54 }55 }56}5758decl_event!(59 pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {60 61 62 63 SomethingStored(u32, AccountId),64 }65);666768#[cfg(test)]69mod tests {70 use super::*;7172 use primitives::H256;73 use support::{impl_outer_origin, assert_ok, parameter_types};74 use sr_primitives::{75 traits::{BlakeTwo256, IdentityLookup}, testing::Header, weights::Weight, Perbill,76 };7778 impl_outer_origin! {79 pub enum Origin for Test {}80 }8182 83 84 85 #[derive(Clone, Eq, PartialEq)]86 pub struct Test;87 parameter_types! {88 pub const BlockHashCount: u64 = 250;89 pub const MaximumBlockWeight: Weight = 1024;90 pub const MaximumBlockLength: u32 = 2 * 1024;91 pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);92 }93 impl system::Trait for Test {94 type Origin = Origin;95 type Call = ();96 type Index = u64;97 type BlockNumber = u64;98 type Hash = H256;99 type Hashing = BlakeTwo256;100 type AccountId = u64;101 type Lookup = IdentityLookup<Self::AccountId>;102 type Header = Header;103 type Event = ();104 type BlockHashCount = BlockHashCount;105 type MaximumBlockWeight = MaximumBlockWeight;106 type MaximumBlockLength = MaximumBlockLength;107 type AvailableBlockRatio = AvailableBlockRatio;108 type Version = ();109 }110 impl Trait for Test {111 type Event = ();112 }113 type TemplateModule = Module<Test>;114115 116 117 fn new_test_ext() -> runtime_io::TestExternalities {118 system::GenesisConfig::default().build_storage::<Test>().unwrap().into()119 }120121 #[test]122 fn it_works_for_default_value() {123 new_test_ext().execute_with(|| {124 125 126 assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));127 128 assert_eq!(TemplateModule::something(), Some(42));129 });130 }131}