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 + msg::sender mocks #14

Merged
merged 44 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
d6c909b
add motsu multiple contract deployment
qalisander Dec 19, 2024
feb635a
bump stylus sdk to 0.7.0-rc.1
qalisander Dec 23, 2024
270e2be
add docs
qalisander Dec 24, 2024
09e7a1a
++
qalisander Dec 24, 2024
f29671f
++
qalisander Dec 24, 2024
4f06a58
++
qalisander Dec 24, 2024
1799ae3
use thread id
qalisander Dec 24, 2024
32d59b5
++
qalisander Dec 24, 2024
6947aa6
test three_proxies works
qalisander Dec 24, 2024
7d6ddd2
++
qalisander Dec 24, 2024
08c6530
++
qalisander Dec 24, 2024
233e970
++
qalisander Dec 24, 2024
fbaeafe
++
qalisander Dec 25, 2024
fa44a57
++
qalisander Dec 25, 2024
cb5d91d
++
qalisander Dec 25, 2024
dedb194
++
qalisander Dec 25, 2024
1888d3c
use context as a key for storage
qalisander Dec 31, 2024
a4ea0ea
impl Drop for Contract
qalisander Dec 31, 2024
7bf0dd3
sender accept generic account
qalisander Dec 31, 2024
366a6f5
init accept generic account
qalisander Dec 31, 2024
7be9b86
++
qalisander Dec 31, 2024
1ffba32
update stylus sdk
qalisander Jan 7, 2025
e6b5196
fmt
qalisander Jan 15, 2025
2fe7239
Merge remote-tracking branch 'origin/main' into unit-tests/multiple-c…
qalisander Jan 15, 2025
f1cd300
++
qalisander Jan 15, 2025
8ae0ebd
make storage_flush_cache available for linking
qalisander Jan 15, 2025
50c3971
init function is capable of returning value
qalisander Jan 16, 2025
41e9882
call random function for test arguments
qalisander Jan 16, 2025
a9c402f
update example with account injection
qalisander Jan 16, 2025
01985a0
rename new(address) -> new_at(address) and add new() method
qalisander Jan 21, 2025
efa1be4
Merge branch 'main' into unit-tests/multiple-contract-deployment
bidzyyys Jan 21, 2025
6c52351
update three_proxies test
qalisander Jan 22, 2025
8ec42e6
inject contracts through arguments
qalisander Jan 22, 2025
66c962e
remove get prefix
qalisander Jan 23, 2025
bc9cf09
update docs
qalisander Jan 23, 2025
28f1d22
++
qalisander Jan 23, 2025
2766a43
++
qalisander Jan 23, 2025
8f662c8
rename `contract` field to `storage`
qalisander Jan 31, 2025
ada172f
update changelog
qalisander Feb 1, 2025
d3e60a5
prevent Contract<..> drop restricting ContractCall<..> with reference
qalisander Feb 1, 2025
74e16fa
add static call + test
qalisander Feb 1, 2025
f1a5e78
add error call test
qalisander Feb 1, 2025
efc7e73
elide lifetimes
qalisander Feb 1, 2025
7b330c3
fix fmt
qalisander Feb 1, 2025
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