motsu_proc/
lib.rs

1#![doc = include_str!("../README.md")]
2use proc_macro::TokenStream;
3
4/// Shorthand to print nice errors.
5///
6/// Note that it's defined before the module declarations.
7macro_rules! error {
8    ($tokens:expr, $($msg:expr),+ $(,)?) => {{
9        let error = syn::Error::new(syn::spanned::Spanned::span(&$tokens), format!($($msg),+));
10        return error.to_compile_error().into();
11    }};
12    (@ $tokens:expr, $($msg:expr),+ $(,)?) => {{
13        return Err(syn::Error::new(syn::spanned::Spanned::span(&$tokens), format!($($msg),+)))
14    }};
15}
16
17mod test;
18
19/// Defines a unit test that provides access to Stylus' execution context.
20///
21/// Internally, this is a thin wrapper over `#[test]` that gives access to
22/// affordances like contract storage and `msg::sender` and deploys precompiles.
23/// If you don't need them, you can simply use `#[test]` instead of
24/// `#[motsu::test]`.
25///
26/// # Examples
27///
28/// ```rust,ignore
29/// #[cfg(test)]
30/// mod tests {
31///     #[motsu::test]
32///     fn autosetup(contract: Contract<Erc20>, alice: Address) {
33///         // contract and alice is already set up, and precompiled
34///         // contracts are deployed.
35///
36///         // ...
37///     }
38///
39///     #[motsu::test]
40///     fn no_affordances() {
41///         // only precompiled contracts are deployed, contract and alice
42///         // need to be manually instantiated.
43///         let contract = Contract::<Erc20>::from_tag("contract");
44///         let alice = Address::from_tag("alice");
45///
46///         // ...
47///     }
48///
49///     #[test]
50///     fn manual_setup() {
51///         // CAUTION: precompiles will not be deployed, use only if you're
52///         // sure you don't need them.
53///         // contract and alice still need to be manually instantiated.
54///         let contract = Contract::<Erc20>::from_tag("contract");
55///         let alice = Address::from_tag("alice");
56///
57///         // ...
58///     }
59/// }
60/// ```
61#[proc_macro_attribute]
62pub fn test(attr: TokenStream, input: TokenStream) -> TokenStream {
63    test::test(&attr, input)
64}