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

Add vm.assertContains with overloads (String, etc..) #4859

Closed
johnnyshankman opened this issue May 1, 2023 · 4 comments · Fixed by #9085
Closed

Add vm.assertContains with overloads (String, etc..) #4859

johnnyshankman opened this issue May 1, 2023 · 4 comments · Fixed by #9085
Assignees
Labels
A-cheatcodes Area: cheatcodes first issue A good way to start contributing T-feature Type: feature

Comments

@johnnyshankman
Copy link

johnnyshankman commented May 1, 2023

Component

Forge

Describe the feature you would like

In the DS Test assertions there should be a assertStringContains(string, string) method for checking strings interpolation.

Additional context

Since we are not deploying these contracts to the network, the gas costs of doing string contains is negligible. It's extremely useful in testing on-chain NFT data etc and straight forward enough to do with two for loops that convert to bytes.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract StringContains {

    function assertStringContains(string memory _base, string memory _value) public pure returns (bool) {
        bytes memory baseBytes = bytes(_base);
        bytes memory valueBytes = bytes(_value);

        if (valueBytes.length == 0) {
            return true;
        }

        if (baseBytes.length < valueBytes.length) {
            return false;
        }

        for (uint i = 0; i <= baseBytes.length - valueBytes.length; i++) {
            bool found = true;
            for (uint j = 0; j < valueBytes.length; j++) {
                if (baseBytes[i + j] != valueBytes[j]) {
                    found = false;
                    break;
                }
            }
            if (found) {
                return true;
            }
        }
        return false;
    }
}

I use this in my tests. It works for every situation I've thrown at it but I have not unit tested this method itself.

@johnnyshankman
Copy link
Author

johnnyshankman commented May 1, 2023

Btw let me know if anything here is unclear. Hopefully pretty straight forward request 🙏 thanks y'all!

Would make a PR myself but I don't have the time this week to dive into the Foundry repo. Hopefully soon.

Been just shimming this thing into any test file that needs it via solidity import.

@johnnyshankman
Copy link
Author

@mds1 any updates on this feature request? been quite a while so i just figured I'd bump this.

@mattsse
Copy link
Member

mattsse commented Jan 8, 2024

I think this would be a nice assert to have.

though we probably want this as native cheatcode impl and not implemented in solidity itself.

forge-std/Test.sol then could simply call vm.assertStringContains?

@zerosnacks zerosnacks changed the title Assertion: String Contains Add vm.assertStringContains Jul 2, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@zerosnacks zerosnacks changed the title Add vm.assertStringContains Add vm.assertContains with overloads (String, etc..) Oct 8, 2024
@zerosnacks zerosnacks added the first issue A good way to start contributing label Oct 8, 2024
@grandizzy grandizzy removed this from the v1.0.0 milestone Oct 8, 2024
@leovct
Copy link
Contributor

leovct commented Oct 9, 2024

Hi @zerosnacks, happy to tackle this issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes first issue A good way to start contributing T-feature Type: feature
Projects
Status: Done
6 participants