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

Conversation

qalisander
Copy link
Member

@qalisander qalisander commented Dec 19, 2024

Based on this pr (OpenZeppelin/rust-contracts-stylus#423)

Resolves #8 , Resolves #12, Resolves #5

PR Checklist

  • Tests
  • Documentation
  • Changelog

@qalisander qalisander changed the title feat(test): unit test external call mock POC feat: unit test external call mock POC Dec 19, 2024
@qalisander qalisander changed the title feat: unit test external call mock POC feat: unit test external call mock Dec 19, 2024
@qalisander qalisander force-pushed the unit-tests/multiple-contract-deployment branch from 3838020 to feb635a Compare December 23, 2024 18:48
…ontract-deployment

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	crates/motsu/README.md
@qalisander
Copy link
Member Author

Update documentation for the new test case structure

Copy link
Collaborator

@bidzyyys bidzyyys left a comment

Choose a reason for hiding this comment

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

I like the design, left some minor comments.

crates/motsu/src/context.rs Outdated Show resolved Hide resolved
crates/motsu/src/context.rs Outdated Show resolved Hide resolved
crates/motsu/src/context.rs Outdated Show resolved Hide resolved
crates/motsu/src/context.rs Show resolved Hide resolved
crates/motsu/src/router.rs Outdated Show resolved Hide resolved
crates/motsu/src/router.rs Outdated Show resolved Hide resolved
@bidzyyys bidzyyys mentioned this pull request Jan 21, 2025
3 tasks
Copy link
Collaborator

@0xNeshi 0xNeshi left a comment

Choose a reason for hiding this comment

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

I'm loving this refactor, it really seems to set us up for easier improvements later

crates/motsu/src/lib.rs Outdated Show resolved Hide resolved
crates/motsu/src/lib.rs Outdated Show resolved Hide resolved
crates/motsu/src/context.rs Show resolved Hide resolved
crates/motsu/src/context.rs Show resolved Hide resolved
crates/motsu/src/shims.rs Show resolved Hide resolved
Copy link
Collaborator

@bidzyyys bidzyyys left a comment

Choose a reason for hiding this comment

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

Can't wait to have it published on crates.io 🚀
Great job @qalisander!

crates/motsu/src/context.rs Show resolved Hide resolved
crates/motsu/src/context.rs Show resolved Hide resolved
crates/motsu/src/shims.rs Show resolved Hide resolved
crates/motsu/src/shims.rs Show resolved Hide resolved
.ping(pong.address(), value)
.expect("should ping successfully");

assert_eq!(ponged_value, value + uint!(1_U256));
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about having:
let one = uint!(1_U256);

You have many repetitions.

crates/motsu/src/lib.rs Show resolved Hide resolved
Comment on lines +27 to 33
//! fn reads_balance(
//! contract: Contract<Erc20>,
//! alice: Account,
//! ) {
//! let balance = contract.sender(alice).balance_of(Address::ZERO); // Access storage.
//! assert_eq!(balance, U256::ZERO);
//! }
Copy link
Collaborator

@0xNeshi 0xNeshi Jan 23, 2025

Choose a reason for hiding this comment

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

Suggested change
//! fn reads_balance(
//! contract: Contract<Erc20>,
//! alice: Account,
//! ) {
//! let balance = contract.sender(alice).balance_of(Address::ZERO); // Access storage.
//! assert_eq!(balance, U256::ZERO);
//! }
//! fn reads_balance(
//! contract: Contract<Erc20>,
//! alice: Account,
//! ) {
//! let balance = contract.sender(alice).balance_of(Address::ZERO); // Access storage.
//! assert_eq!(balance, U256::ZERO);
//! }
//!
//! #[motsu::test]
//! fn approve() {
//! let contract = Contract::<Erc20>::new();
//! let alice = Account::random();
//! let bob = Account::random();
//! let result = contract.sender(alice).approve(bob, U256::from(1)); // Change state.
//! assert!(result.is_ok());
//! }

We should add another example documenting a test case that instantiates a contract manually within the test itself, while also showing that they can change state within a test.

This new example works with regular #[test], but we can leave #[motsu::test] if you think it's better.

crates/motsu/src/context.rs Show resolved Hide resolved
crates/motsu/src/context.rs Outdated Show resolved Hide resolved
crates/motsu/src/context.rs Outdated Show resolved Hide resolved
@qalisander qalisander changed the title feat: unit test external call mock feat: unit test external call + msg::sender mocks Feb 1, 2025
@qalisander qalisander merged commit 480e012 into main Feb 3, 2025
9 checks passed
@bidzyyys bidzyyys deleted the unit-tests/multiple-contract-deployment branch February 3, 2025 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants