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`. If you don't need
23/// them, you can pass no arguments to the test function or simply use
24/// `#[test]` instead of `#[motsu::test]`.
25///
26/// # Examples
27///
28/// ```rust
29/// #[cfg(test)]
30/// mod tests {
31/// #[motsu::test]
32/// fn reads_balance(contract: Erc20) {
33/// let balance = contract.balance_of(Address::ZERO);
34/// assert_eq!(U256::ZERO, balance);
35///
36/// let owner = msg::sender();
37/// let one = U256::from(1);
38/// contract._balances.setter(owner).set(one);
39/// let balance = contract.balance_of(owner);
40/// assert_eq!(one, balance);
41/// }
42/// }
43/// ```
44///
45/// ```rust,ignore
46/// #[cfg(test)]
47/// mod tests {
48/// #[motsu::test]
49/// fn t() { // If no params, it expands to a `#[test]`.
50/// ...
51/// }
52/// }
53/// ```
54#[proc_macro_attribute]
55pub fn test(attr: TokenStream, input: TokenStream) -> TokenStream {
56 test::test(&attr, input)
57}