-
Notifications
You must be signed in to change notification settings - Fork 313
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
refactor(test): assertApproxEq{Rel,Abs} unit tests, improvements. & fixes #51
refactor(test): assertApproxEq{Rel,Abs} unit tests, improvements. & fixes #51
Conversation
CC: @ZeroEkkusu @mds1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot, looks great! Most comments are nits on style, and one where I realized we're checking relative tolerance incorrectly
BREAKING: assertApproxEq{Abs,Rel}(int256,int265,int256) -> assertApproxEq{Abs,Rel}(int256,int265,uint256), this is because a delta cannot be negative (i think :P)
Yes good call here, uint256
is the right choice 👌
src/test/StdAssertions.t.sol
Outdated
|
||
contract StdAssertionsTest is Test | ||
{ | ||
// todo: figure out how to expectEmit multiple events |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the assertEq
overloads are simple enough that we don't really need tests for them as they may add more clutter than value—even the original ones don't have tests 😅
But you can stack expect emits like this:
vm.expectEmit(true, true, true, true);
emit MyFirstEvent(val1);
vm.expectEmit(true, true, true, true);
emit MySecondEvent(val2);
// This will look for both events in the below call
myContract.myCall();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the expectEmit answer! Re testing assertEq im flexible, it was quite easy to just template all these tests and fill them in (some of it was even macro-able). If you think their value as regression guards is outweighed by the clutter (given it would be impressive to regress assertEq()), then okay for them to be dropped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, paging @ZeroEkkusu for a second opinion here since I don't feel too strongly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should follow the convention from DSTest for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DSTest's convention being to not have a corresponding test file for any assertions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @OliverNChalk, great additions! I think Forge Std should provide things that are often needed when testing but not natively available in Solidity. I'd do these manually, so a math lib is great.
Agree with Matt on following the DSTest conventions for now.
That being said, we can also reduce the amount of code if we do assertions overriding like this. Let me know what you think.
The test are pretty comprehensive, love it.
I'll leave more comments in conversations.
Edit: I don't mind the code block style! Didn't know it was intentional.
Co-authored-by: Matt Solomon <matt@mattsolomon.dev>
Do we want to follow DSTest convention here? I feel like we should, I can call the inner EDIT: I have optimistically included the change proposed by @ZeroEkkusu, feel free to dispute: 3b3e26b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OliverNChalk, I'll leave other comments in the convos.
src/Test.sol
Outdated
@@ -585,16 +565,12 @@ library stdStorage { | |||
DELTA MATHS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
STD-MATH
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Oops looks like there's a bug in my GH UI and the comments were not posted. Regarding the assertions tests, I wrote: If you don't feel to strongly about either, let's leave them and cover everything in Forge Std from now on. It's the standard testing library for Foundry, after all. |
Otherwise, lgtm; @mds1 can take a final look and merge once ready. |
LGTM, thanks a lot @OliverNChalk! |
|
||
library stdMath { | ||
function abs(int256 a) internal pure returns (uint256) { | ||
unchecked { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just letting you guys know: #53
Brief
This PR adds unit tests for the recently added methods:
In the quest to unit test these components, the following changes were also made:
deltaMaths
library (used in relative and absolute approximation)internal virtual
(2 funcs didn't havevirtual
)assertApproxEq{Abs,Rel}(int256,int265,int256)
->assertApproxEq{Abs,Rel}(int256,int265,uint256)
, this is because a delta cannot be negative (i think :P)Next Steps
Hopefully there isn't anything too controversial in here, so should just be a matter of code reviewing & merging once happy. @ZeroEkkusu I did wanna call out that for the code blocks I use the following thought process (excerpt from a personal repo), let me know if you agree/disagree (saw you re-align some of the previous blocks):
Resolves
Closes: #49