Skip to content

feat(forge): table tests#10775

Merged
grandizzy merged 2 commits intofoundry-rs:masterfrom
grandizzy:table-tests
Jun 16, 2025
Merged

feat(forge): table tests#10775
grandizzy merged 2 commits intofoundry-rs:masterfrom
grandizzy:table-tests

Conversation

@grandizzy
Copy link
Copy Markdown
Collaborator

@grandizzy grandizzy commented Jun 13, 2025

Motivation

Solution

  • introduce new table* tests
  • use current fixtures to run table tests:
    • the parameters dataset (table) is created from defined parameter fixtures
    • table test table_test(uint256 amount, bool swap) fixtures are defined as
      uint256[] public fixtureAmount = [2, 5]
      bool[] public fixtureSwap = [true, false]
    • table_test is then called with the pair of args (2, true) and (5, false).
    • a table test with single param table_test(uint256 amount) will be called with values of 2 and 5

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@grandizzy grandizzy marked this pull request as ready for review June 13, 2025 08:56
Copy link
Copy Markdown
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

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

Makes sense 👍

Implementation looks good

@grandizzy grandizzy merged commit 4435b46 into foundry-rs:master Jun 16, 2025
22 checks passed
@github-project-automation github-project-automation bot moved this to Done in Foundry Jun 16, 2025
@grandizzy grandizzy deleted the table-tests branch June 16, 2025 04:27
@gakonst
Copy link
Copy Markdown
Member

gakonst commented Jun 16, 2025

How does this work for structs if at all? If not good preprocessing feature

@grandizzy
Copy link
Copy Markdown
Collaborator Author

grandizzy commented Jun 16, 2025

How does this work for structs if at all? If not good preprocessing feature

yep, that's possible by defining struct fixtures as samples below, will update docs

    struct TestCase {
        uint256 a;
        uint256 b;
        uint256 expected;
    }
    
    function fixtureSums() public returns (TestCase[] memory) {
        TestCase[] memory entries = new TestCase[](2);
        entries[0] = TestCase(1, 2, 3);
        entries[1] = TestCase(4, 5, 9);
        return entries;
    }

    function tableSumsTest(TestCase memory sums) public pure {
        require(sums.a + sums.b == sums.expected, "wrong sum");
    }

or multiple structs input

contract SwapTableTest is Test {
    struct Wallet {
        address owner;
        uint256 amount;
    }

    struct Swap {
        bool swap;
        uint256 amount;
    }
    
    Wallet[] public fixtureWallet;
    Swap[] public fixtureSwap;

    function setUp() public {
        // first table test input
        fixtureWallet.push(Wallet(address(11), 11));
        fixtureSwap.push(Swap(true, 11));

        // second table test input
        fixtureWallet.push(Wallet(address(12), 12));        
        fixtureSwap.push(Swap(false, 12));
    }

    function tableSwapTest(Wallet memory wallet, Swap memory swap) public pure {
        require(
            (wallet.owner == address(11) && swap.swap) || (wallet.owner == address(12) && !swap.swap), "not allowed"
        );
    }
}

@grandizzy grandizzy self-assigned this Jun 17, 2025
@grandizzy grandizzy moved this from Done to Completed in Foundry Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Completed

Development

Successfully merging this pull request may close these issues.

Table tests

3 participants