Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}
Loading