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

feat(cheatcodes): implement new cheatcode to check if a string contains another string #9085

Merged
Merged
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.

4 changes: 4 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,10 @@ interface Vm {
string calldata error
) external pure;

/// Asserts that a string contains another string.
#[cheatcode(group = Testing, safety = Safe)]
leovct marked this conversation as resolved.
Show resolved Hide resolved
function assertContains(string calldata haystack, string calldata needle) external pure;

// ======== OS and Filesystem ========

// -------- Metadata --------
Expand Down
13 changes: 12 additions & 1 deletion crates/cheatcodes/src/test/assert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{CheatcodesExecutor, CheatsCtxt, Result, Vm::*};
use crate::{Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, Result, Vm::*};
use alloy_primitives::{hex, I256, U256};
use foundry_evm_core::{
abi::{format_units_int, format_units_uint},
Expand Down Expand Up @@ -447,6 +447,17 @@ impl_assertions! {
(assertApproxEqRelDecimal_2Call, assertApproxEqRelDecimal_3Call),
}

impl Cheatcode for assertContainsCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { haystack, needle } = self;
if haystack.contains(needle) {
Ok(Default::default())
} else {
Err(format!("String '{haystack}' does not contain '{needle}'").into())
}
}
}

fn assert_true(condition: bool) -> Result<Vec<u8>, SimpleAssertionError> {
if condition {
Ok(Default::default())
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.

9 changes: 9 additions & 0 deletions testdata/default/cheats/Assert.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -822,4 +822,13 @@ contract AssertionsTest is DSTest {

vm.assertApproxEqRel(uint256(0), uint256(0), uint256(0));
}

function testAssertContains() public {
string memory str1 = "this is a test";
string memory str2 = "test";
string memory str3 = "foundry";
vm.assertContains(str1, str2);
vm._expectCheatcodeRevert(bytes(string.concat("String '", str1, "' does not contain '", str3, "'")));
vm.assertContains(str1, str3);
}
}
Loading