Skip to content

Commit 319c0f8

Browse files
leovctzerosnacksmds1
authored andcommitted
feat(cheatcodes): implement new cheatcode to check if a string contains 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]>
1 parent 896cc17 commit 319c0f8

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/spec/src/vm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,9 @@ interface Vm {
19761976
/// Returns 0 in case of an empty `key`.
19771977
#[cheatcode(group = String)]
19781978
function indexOf(string calldata input, string calldata key) external pure returns (uint256);
1979+
/// Returns true if `search` is found in `subject`, false otherwise.
1980+
#[cheatcode(group = String)]
1981+
function contains(string calldata subject, string calldata search) external returns (bool result);
19791982

19801983
// ======== JSON Parsing and Manipulation ========
19811984

crates/cheatcodes/src/string.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ impl Cheatcode for indexOfCall {
144144
}
145145
}
146146

147+
// contains
148+
impl Cheatcode for containsCall {
149+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
150+
let Self { subject, search } = self;
151+
Ok(subject.contains(search).abi_encode())
152+
}
153+
}
154+
147155
pub(super) fn parse(s: &str, ty: &DynSolType) -> Result {
148156
parse_value(s, ty).map(|v| v.abi_encode())
149157
}

testdata/cheats/Vm.sol

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/default/cheats/StringUtils.t.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,10 @@ contract StringManipulationTest is DSTest {
5151
assertEq(vm.indexOf(input, key3), 0);
5252
assertEq(vm.indexOf(input, key4), type(uint256).max);
5353
}
54+
55+
function testContains() public {
56+
string memory subject = "this is a test";
57+
assert(vm.contains(subject, "test"));
58+
assert(!vm.contains(subject, "foundry"));
59+
}
5460
}

0 commit comments

Comments
 (0)