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;

/// Returns true if the left string contains the right string, otherwise returns false.
#[cheatcode(group = Testing, safety = Safe)]
leovct marked this conversation as resolved.
Show resolved Hide resolved
function assertContains(string calldata left, string calldata right) external returns (bool result);

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

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

impl Cheatcode for assertContainsCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { left, right } = self;
Ok(left.contains(right).abi_encode())
}
}

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.

8 changes: 8 additions & 0 deletions testdata/default/cheats/Assert.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -822,4 +822,12 @@ 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";
assert(vm.assertContains(str1, str2));
assert(!vm.assertContains(str1, str3));
}
}
Loading