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

Improve tests #864

Merged
merged 17 commits into from Jan 29, 2024
Merged

Improve tests #864

merged 17 commits into from Jan 29, 2024

Conversation

ericnordelo
Copy link
Member

Fixes #670 #820

See this comment for assertion guidelines.

This PR also removes the #[available_gas] attributes.

PR Checklist

  • Tests
  • Documentation
  • Added entry to CHANGELOG.md
  • Tried the feature on a public network

Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

Great work, Eric! I left a few minor comments, suggestions, and questions

src/tests/access/test_dual_accesscontrol.cairo Outdated Show resolved Hide resolved
src/tests/introspection/test_dual_src5.cairo Outdated Show resolved Hide resolved
src/tests/mocks/erc721_mocks.cairo Outdated Show resolved Hide resolved
src/tests/mocks/erc721_receiver_mocks.cairo Outdated Show resolved Hide resolved
src/tests/presets/test_account.cairo Outdated Show resolved Hide resolved
src/tests/token/test_erc20.cairo Outdated Show resolved Hide resolved
src/tests/token/test_erc20.cairo Outdated Show resolved Hide resolved
src/tests/token/test_erc20.cairo Outdated Show resolved Hide resolved
Comment on lines +1 to +16
use core::fmt::{Debug, Formatter, Error};
use starknet::{ContractAddress, ClassHash};

impl DebugContractAddress of core::fmt::Debug<ContractAddress> {
fn fmt(self: @ContractAddress, ref f: Formatter) -> Result<(), Error> {
let address: felt252 = (*self).into();
write!(f, "{address:?}")
}
}

impl DebugClassHash of core::fmt::Debug<ClassHash> {
fn fmt(self: @ClassHash, ref f: Formatter) -> Result<(), Error> {
let hash: felt252 = (*self).into();
write!(f, "{hash:?}")
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, should we document this addition in the CHANGELOG?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if we should add test stuff in the changelog. Actually, I think we shouldn't, even when I added a couple of entries already. I think changelog should be for API-related stuff of the library, not including testing, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

If you agree I will remove the test-related entries already added.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I agree with not including testing

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed offline to focus on relevant API changes, albeit with some flexibility analyzing case by case

src/tests/account/test_account.cairo Outdated Show resolved Hide resolved
ericnordelo and others added 4 commits January 10, 2024 10:54
Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

I left a final suggestion and I agree with removing the test items in the changelog. I guess we should also remove the use ComponentState in tests as well?

src/tests/access/test_dual_accesscontrol.cairo Outdated Show resolved Hide resolved
Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
Copy link
Contributor

@martriay martriay left a comment

Choose a reason for hiding this comment

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

Great improvements! The library, and therefore the world, is now a bit better thanks to this PR :)

CHANGELOG.md Outdated Show resolved Hide resolved
Comment on lines +1 to +16
use core::fmt::{Debug, Formatter, Error};
use starknet::{ContractAddress, ClassHash};

impl DebugContractAddress of core::fmt::Debug<ContractAddress> {
fn fmt(self: @ContractAddress, ref f: Formatter) -> Result<(), Error> {
let address: felt252 = (*self).into();
write!(f, "{address:?}")
}
}

impl DebugClassHash of core::fmt::Debug<ClassHash> {
fn fmt(self: @ClassHash, ref f: Formatter) -> Result<(), Error> {
let hash: felt252 = (*self).into();
write!(f, "{hash:?}")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

agreed offline to focus on relevant API changes, albeit with some flexibility analyzing case by case

Comment on lines 69 to 70
assert!(supports_isrc5);
assert!(supports_isrc6);
Copy link
Contributor

Choose a reason for hiding this comment

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

why not asserting right after each query? this is the least important optimization but we can save us a call in case isrc5 fails early -- that's actually a guideline we try to follow in our contracts: fail early and loud

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I will update it.

Comment on lines +179 to +184
let supports_isrc5 = dispatcher.supports_interface(ISRC5_ID);
let supports_isrc6 = dispatcher.supports_interface(ISRC6_ID);
let doesnt_support_0x123 = !dispatcher.supports_interface(0x123);
assert!(supports_isrc5);
assert!(supports_isrc6);
assert!(doesnt_support_0x123);
Copy link
Contributor

Choose a reason for hiding this comment

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

we should settle on a query/assert order pattern. in here it's all queries then asserts, in the previous test it's mixed, and there's also query/assert pairs

Copy link
Member Author

Choose a reason for hiding this comment

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

Asserting right after each query as you suggested looks like the best approach to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

added a comment in the style guidelines issue

src/tests/security/test_reentrancyguard.cairo Show resolved Hide resolved
@ericnordelo ericnordelo merged commit 3baa102 into OpenZeppelin:main Jan 29, 2024
5 checks passed
@ericnordelo ericnordelo deleted the feat/improve-tests branch January 29, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve test mocks
3 participants