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(forge): new cheatcode for running simple tests with multiple msg.sender values #4839

Open
PaulRBerg opened this issue Apr 27, 2023 · 2 comments
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test T-feature Type: feature

Comments

@PaulRBerg
Copy link
Contributor

PaulRBerg commented Apr 27, 2023

Component

Forge

Describe the feature you would like

Scenario: a contract function allows the caller to be one of two special accounts:

function foo() external onlyAliceOrBob {
    // <--- snip --- >
}

Testing this sort of function is annoying because I have to duplicate logic in my tests, e.g.

function testFoo_Alice() external {
    assertEq(A, B);
    assertEq(C, D);
    assertEq(E, F);
}

function testFoo_Bob() external {
    assertEq(A, B);
    assertEq(C, D);
    assertEq(E, F);
}

Now, I know I could de-dup the assertions in a subroutine, like this:

function testFoo_Common() internal {
    // <--- snip --->
}

But I wouldn't say I like this approach because it (i) hurts the readability of the tests and (ii) doesn't scale super well (e.g. in realistic scenarios, Bob has specific permissions that Alice doesn't have, which requires branching, which in turn complicates the subroutine).

It would be helpful if Forge lent a hand for this use case. I imagine that a new cheatcode could be implemented, e.g. vm.consider, which would be similar to vm.assume and would work something like this:

  • Takes one, two, three, four, or a dynamic array of addresses
  • When Forge sees vm.consider in a test, it runs the test with all provided addresses, one test run for each address. Basically, vm.consider would run a batch of changePrank under the hood (related: feat(forge): cheat that starts or changes the prank #4779)
  • Forge overrides any prank started before the test (e.g. in setUp), but it lets the user change the prank during the test
  • Using this cheat in a fuzz test would result in an error

With this hypothetical vm.consider, I could write my test like this:

function testFoo() external {
    vm.consider(users.alice, users.bob); // succinct and declarative
    assertEq(A, B);
    assertEq(C, D);
    assertEq(E, F);
}

Side notes:

  • vm.consider is just a suggestion. I don't think it's a great name. Alternatives: vm.regard, vm.rollPrank, vm.testPrank.
  • What I'm referring to in this feature request is not fuzzing. I want to run the test once with Alice as the caller, and once with Bob. I don't want to run the tests hundreds of times - these are unit tests I refer to.
@PaulRBerg PaulRBerg added the T-feature Type: feature label Apr 27, 2023
@gakonst gakonst added this to Foundry Apr 27, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Apr 27, 2023
@mds1 mds1 added Cmd-forge-test Command: forge test C-forge Command: forge A-cheatcodes Area: cheatcodes labels May 1, 2023
@mds1
Copy link
Collaborator

mds1 commented May 1, 2023

Is this a duplicate of #858? It sounds like a more narrow version that issue

@PaulRBerg
Copy link
Contributor Author

PaulRBerg commented May 1, 2023

This is not quite the same as #858 (table tests).

A table test is an explicit set of input(s) + output(s).

Whereas in this issue, I suggested implementing a new mode of unit testing, a mode specifically meant for unit testing a range of callers. There's no notion of outputs in my proposal.

@zerosnacks zerosnacks added A-testing Area: testing and removed A-testing Area: testing labels Jul 2, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@grandizzy grandizzy removed this from the v1.0.0 milestone Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge Cmd-forge-test Command: forge test T-feature Type: feature
Projects
Status: Todo
Development

No branches or pull requests

4 participants