Skip to content

Commit

Permalink
feat(cheatcodes): implement new cheatcode to check if a string contai…
Browse files Browse the repository at this point in the history
…ns another string (foundry-rs#9085)

* feat: implement new cheatcode to check if a string contains another string

* chore: make clippy and rustfmt happy

* chore: vm.contains should return a boolean

* Update testdata/cheats/Vm.sol

Co-authored-by: zerosnacks <[email protected]>

* Update crates/cheatcodes/spec/src/vm.rs

Co-authored-by: zerosnacks <[email protected]>

* chore: update `cheatcodes.json`

* chore: update var names

* chore: rename to `vm.contains`

* Update crates/cheatcodes/spec/src/vm.rs

Co-authored-by: Matt Solomon <[email protected]>

* Update crates/cheatcodes/spec/src/vm.rs

Co-authored-by: Matt Solomon <[email protected]>

* chore: address PR comments

---------

Co-authored-by: zerosnacks <[email protected]>
Co-authored-by: Matt Solomon <[email protected]>
  • Loading branch information
3 people authored and rplusq committed Nov 29, 2024
1 parent 896cc17 commit 319c0f8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,9 @@ interface Vm {
/// Returns 0 in case of an empty `key`.
#[cheatcode(group = String)]
function indexOf(string calldata input, string calldata key) external pure returns (uint256);
/// Returns true if `search` is found in `subject`, false otherwise.
#[cheatcode(group = String)]
function contains(string calldata subject, string calldata search) external returns (bool result);

// ======== JSON Parsing and Manipulation ========

Expand Down
8 changes: 8 additions & 0 deletions crates/cheatcodes/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ impl Cheatcode for indexOfCall {
}
}

// contains
impl Cheatcode for containsCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { subject, search } = self;
Ok(subject.contains(search).abi_encode())
}
}

pub(super) fn parse(s: &str, ty: &DynSolType) -> Result {
parse_value(s, ty).map(|v| v.abi_encode())
}
Expand Down
1 change: 1 addition & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions testdata/default/cheats/StringUtils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ contract StringManipulationTest is DSTest {
assertEq(vm.indexOf(input, key3), 0);
assertEq(vm.indexOf(input, key4), type(uint256).max);
}

function testContains() public {
string memory subject = "this is a test";
assert(vm.contains(subject, "test"));
assert(!vm.contains(subject, "foundry"));
}
}

0 comments on commit 319c0f8

Please sign in to comment.