Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unit test external call mock #14

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
911 changes: 893 additions & 18 deletions Cargo.lock

Large diffs are not rendered by default.

48 changes: 18 additions & 30 deletions crates/motsu-proc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,45 @@ pub(crate) fn test(_attr: &TokenStream, input: TokenStream) -> TokenStream {
let fn_block = &item_fn.block;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left my feedback on motsu::test macro in our group chat, but the gist of it is that with the new context design in this PR, contracts/EOAs can be safely manually instantiated with little effort, and fuzzing crates like proptest work with motsu out-of-the-box (no need for special macro wrappers).

However, there are arguments to be made that we need to keep this attribute:

  1. Backward compatibility.
  2. Enables us to easily add additional features in the future.
  3. A/B testing - we document ways for devs to instantiate contracts/EOAs (injected as is now, and manual), and after some time we investigate how the devs actually prefer to use the crate - if either of the approaches gains drastically more adoption, we can always drop the other.

let fn_args = &sig.inputs;

// Currently, more than one contract per unit test is not supported.
if fn_args.len() > 1 {
error!(fn_args, "expected at most one contract in test signature");
}

// Whether 1 or none contracts will be declared.
let arg_binding_and_ty = match fn_args
.into_iter()
.map(|arg| {
let FnArg::Typed(arg) = arg else {
error!(@arg, "unexpected receiver argument in test signature");
};
let contract_arg_binding = &arg.pat;
let contract_ty = &arg.ty;
Ok((contract_arg_binding, contract_ty))
let arg_binding = &arg.pat;
let arg_ty = &arg.ty;
Ok((arg_binding, arg_ty))
})
.collect::<Result<Vec<_>, _>>()
{
Ok(res) => res,
Err(err) => return err.to_compile_error().into(),
};

let contract_arg_defs =
arg_binding_and_ty.iter().map(|(arg_binding, contract_ty)| {
// Test case assumes, that contract's variable has `&mut` reference
// to contract's type.
quote! {
#arg_binding: &mut #contract_ty
}
});
// Collect argument definitions.
let arg_defs = arg_binding_and_ty.iter().map(|(arg_binding, arg_ty)| {
quote! {
#arg_binding: #arg_ty
}
});

let contract_args =
arg_binding_and_ty.iter().map(|(_arg_binding, contract_ty)| {
// Pass mutable reference to the contract.
quote! {
&mut <#contract_ty>::default()
}
});
// Collect argument initializations.
let arg_inits = arg_binding_and_ty.iter().map(|(_arg_binding, arg_ty)| {
quote! {
<#arg_ty>::random()
}
});

// Declare test case closure.
// Pass mut ref to the test closure and call it.
// Reset storage for the test context and return test's output.
// Pass arguments to the test closure and call it.
quote! {
#( #attrs )*
#[test]
fn #fn_name() #fn_return_type {
use ::motsu::prelude::DefaultStorage;
let test = | #( #contract_arg_defs ),* | #fn_block;
let res = test( #( #contract_args ),* );
::motsu::prelude::Context::current().reset_storage();
res
let test = | #( #arg_defs ),* | #fn_block;
test( #( #arg_inits ),* )
}
}
.into()
Expand Down
2 changes: 2 additions & 0 deletions crates/motsu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ tiny-keccak.workspace = true
stylus-sdk.workspace = true
motsu-proc.workspace = true
dashmap.workspace = true
alloy-primitives = { workspace = true, features = ["arbitrary", "rand"] }
alloy-sol-types.workspace = true

[lints]
workspace = true
Loading
Loading