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

How to combine multiple supposed-to-fail contract calls in one test case? #3950

Closed
0xDEnYO opened this issue Nov 21, 2022 · 4 comments
Closed

Comments

@0xDEnYO
Copy link

0xDEnYO commented Nov 21, 2022

Hey Guys,

I am coding a Testhelper.sol contract which features some standard tests that should be inherited and executed by multiple other test files. We have numerous diamond facets and we want to make sure that they all pass the same tests whilst reducing boilerplate code.
Aggregating and calling positive tests works fine.
But aggregating negative tests does not work since after the first test failed, the tx is reverted already and the following tests will never be executed.
I cannot work with try/catch here since these are internal calls.

In Hardhat I could apply the following principle:
expectRevert(testFailNegative1());
expectRevert(testFailNegative2());
...

But with foundry I have not found a way to have several reverting calls in one test case.

Do you have a suggestion for me how I could implement such a pattern?

file Testhelper.sol

contract TestHelper {
   function testPositiveA() public {....}
   function testPositiveB() public {....}
   function testFailNegative1() public {....}
   function testFailNegative() public {....}

   function runAllPositiveTests() internal {      // this works fine
     testPositiveA();
     testPositiveB();
   }

  function runAllNegativeTests() internal {
     testFailNegative1();   // fails here (as expected)
     testFailNegative2();  // will never be executed, but should
  }
}

file TestFacet.t.sol

contract TestFacet is Testhelper {

   function testPositiveStandardTests() public {
       runAllPositiveTests();   // this works fine
   }

   function testFailNegativeStandardTests() public {
       runAllNegativeTests();    // this will never execute all negative standard tests but stop after the first one failed
   }
}
@mattsse
Copy link
Member

mattsse commented Nov 21, 2022

@odyslam thoughts about this?

iirc we discussed multi expectRevert at some point, so maybe you have some insight here

@0xDEnYO
Copy link
Author

0xDEnYO commented Nov 21, 2022

Actually, I just realized that by making the functions public in the Testhelper.sol contract, the test run will pick them up automatically. So I dont even need to aggregate the functions and call them explicitly. This helps me partially.

But nevertheless I see benefits of having multiple reverts in one call
(e.g. testing to fail a function with several wrong parameters - would be nice to combine that in one test case)

@odyslam
Copy link
Contributor

odyslam commented Nov 21, 2022

@mattsse I think this is another use-case for the combinators we talked about:

  • not
  • or
  • and

@onbjerg onbjerg transferred this issue from foundry-rs/book Dec 24, 2022
@zerosnacks
Copy link
Member

Closing in favor of #3723 as it has a larger body of discussion

@zerosnacks zerosnacks closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

No branches or pull requests

4 participants