1234567891011use support::{decl_module, decl_storage, decl_event, StorageValue, 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 runtime_io::with_externalities;73 use primitives::{H256, Blake2Hasher};74 use support::{impl_outer_origin, assert_ok, parameter_types};75 use sr_primitives::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};76 use sr_primitives::weights::Weight;77 use sr_primitives::Perbill;7879 impl_outer_origin! {80 pub enum Origin for Test {}81 }8283 84 85 86 #[derive(Clone, Eq, PartialEq)]87 pub struct Test;88 parameter_types! {89 pub const BlockHashCount: u64 = 250;90 pub const MaximumBlockWeight: Weight = 1024;91 pub const MaximumBlockLength: u32 = 2 * 1024;92 pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);93 }94 impl system::Trait for Test {95 type Origin = Origin;96 type Call = ();97 type Index = u64;98 type BlockNumber = u64;99 type Hash = H256;100 type Hashing = BlakeTwo256;101 type AccountId = u64;102 type Lookup = IdentityLookup<Self::AccountId>;103 type Header = Header;104 type WeightMultiplierUpdate = ();105 type Event = ();106 type BlockHashCount = BlockHashCount;107 type MaximumBlockWeight = MaximumBlockWeight;108 type MaximumBlockLength = MaximumBlockLength;109 type AvailableBlockRatio = AvailableBlockRatio;110 type Version = ();111 }112 impl Trait for Test {113 type Event = ();114 }115 type TemplateModule = Module<Test>;116117 118 119 fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {120 system::GenesisConfig::default().build_storage::<Test>().unwrap().into()121 }122123 #[test]124 fn it_works_for_default_value() {125 with_externalities(&mut new_test_ext(), || {126 127 128 assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));129 130 assert_eq!(TemplateModule::something(), Some(42));131 });132 }133}