Skip to content

Commit f731912

Browse files
committed
Experiment with macro to reduce code duplication
1 parent a18327e commit f731912

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

crates/lang/src/env_access.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,34 @@ use ink_primitives::Key;
3131

3232
use crate::ChainExtensionInstance;
3333

34+
#[macro_export]
35+
macro_rules! mock_contract {
36+
( $($tt:tt)* ) => {{
37+
#![cfg_attr(not(feature = "std"), no_std)]
38+
use ::ink_lang as ink;
39+
use ::ink_env::{
40+
Environment,
41+
DefaultEnvironment,
42+
call::{build_create, Selector, ExecutionInput, FromAccountId}
43+
};
44+
#[ink::contract]
45+
pub mod my_contract {
46+
#[ink(storage)]
47+
pub struct MyContract { }
48+
49+
impl MyContract {
50+
#[ink(constructor)]
51+
pub fn new() -> Self {
52+
Self {}
53+
}
54+
55+
$( $tt )*
56+
57+
}
58+
}
59+
}};
60+
}
61+
3462
/// The environment of the compiled ink! smart contract.
3563
pub trait ContractEnv {
3664
/// The environment type.
@@ -114,33 +142,15 @@ where
114142
/// # Example
115143
///
116144
/// ```
117-
/// #![cfg_attr(not(feature = "std"), no_std)]
118-
/// use ink_lang as ink;
119145
/// use ink_prelude;
120-
/// # use ::ink_env::{
121-
/// # Environment,
122-
/// # DefaultEnvironment,
123-
/// # call::{build_create, Selector, ExecutionInput, FromAccountId}
124-
/// # };
125-
/// # #[ink::contract]
126-
/// # pub mod my_contract {
127-
/// # #[ink(storage)]
128-
/// # pub struct MyContract { }
129-
/// #
130-
/// # impl MyContract {
131-
/// # #[ink(constructor)]
132-
/// # pub fn new() -> Self {
133-
/// # Self {}
134-
/// # }
135-
///
146+
/// # ink_lang::mock_contract! {
136147
/// #[ink(message)]
137-
/// pub fn call_me(&self) {
148+
/// pub fn call_me2(&self) {
138149
/// let caller = self.env().caller();
139150
/// let message = ink_prelude::format!("got a call from {:?}", caller);
140151
/// ink_env::debug_println(&message);
141152
/// }
142153
/// # }
143-
/// # }
144154
/// ```
145155
///
146156
/// # Note

0 commit comments

Comments
 (0)