Skip to content

Commit

Permalink
use empty vec instead of option<vec>; remove commented test
Browse files Browse the repository at this point in the history
  • Loading branch information
emo-eth committed Nov 5, 2024
1 parent 24ebd44 commit bd54dc1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
21 changes: 9 additions & 12 deletions crates/cheatcodes/src/test/assume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct AssumeNoRevert {
/// Acceptable revert parameters for the next call, to be thrown out if they are encountered;
/// reverts with parameters not specified here will count as normal reverts and not rejects
/// towards the counter.
pub reasons: Option<Vec<AcceptableRevertParameters>>,
pub reasons: Vec<AcceptableRevertParameters>,
/// Address that reverted the call.
pub reverted_by: Option<Address>,
}
Expand Down Expand Up @@ -137,26 +137,23 @@ fn assume_no_revert(

// if assume_no_revert is not set, set it
if state.assume_no_revert.is_none() {
state.assume_no_revert = Some(AssumeNoRevert { depth, reasons: None, reverted_by: None });
state.assume_no_revert = Some(AssumeNoRevert { depth, reasons: vec![], reverted_by: None });
// if reason is not none, create a new AssumeNoRevertParams vec
if let Some(reason) = reason {
state.assume_no_revert.as_mut().unwrap().reasons =
Some(vec![AcceptableRevertParameters { reason, partial_match, reverter }]);
vec![AcceptableRevertParameters { reason, partial_match, reverter }];
}
} else {
// otherwise, ensure that reasons vec is not none and new reason is also not none
let valid_assume =
state.assume_no_revert.as_ref().unwrap().reasons.is_some() && reason.is_some();
!state.assume_no_revert.as_ref().unwrap().reasons.is_empty() && reason.is_some();
ensure!(valid_assume, ASSUME_REJECT_MAGIC);
// and append the new reason
state
.assume_no_revert
.as_mut()
.unwrap()
.reasons
.as_mut()
.unwrap()
.push(AcceptableRevertParameters { reason: reason.unwrap(), partial_match, reverter });
state.assume_no_revert.as_mut().unwrap().reasons.push(AcceptableRevertParameters {
reason: reason.unwrap(),
partial_match,
reverter,
});
}

Ok(Default::default())
Expand Down
12 changes: 8 additions & 4 deletions crates/cheatcodes/src/test/revert_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ pub(crate) fn handle_assume_no_revert(
retdata: &Bytes,
known_contracts: &Option<ContractsByArtifact>,
) -> Result<()> {
// if a generic assumeNoRevert, return Ok(). Otherwise, iterate over acceptable reasons and try
// if a generic AssumeNoRevert, return Ok(). Otherwise, iterate over acceptable reasons and try
// to match against any, otherwise, return an Error with the revert data
assume_no_revert.reasons.as_ref().map_or(Ok(()), |reasons| {
reasons
if assume_no_revert.reasons.is_empty() {
Ok(())
} else {
assume_no_revert
.reasons
.iter()
.find_map(|reason| {
handle_revert(
Expand All @@ -140,8 +143,9 @@ pub(crate) fn handle_assume_no_revert(
.ok()
})
.ok_or_else(|| retdata.clone().into())
})
}
}

pub(crate) fn handle_expect_revert(
is_cheatcode: bool,
is_create: bool,
Expand Down
17 changes: 0 additions & 17 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2500,23 +2500,6 @@ forgetest_init!(test_assume_no_revert_with_data, |prj, cmd| {
_vm.assumeNoRevert(abi.encodeWithSelector(Reverter.RevertWithData.selector, 4), address(reverter));
reverter.twoPossibleReverts(x);
}
/// @dev Test that `assumeNoRevert` assumptions are cleared after the first non-cheatcode external call
function testMultipleAssumesClearAfterCall_fails(uint256 x) public view {
_vm.assumeNoRevert(Reverter.MyRevert.selector);
_vm.assumeNoPartialRevert(Reverter.RevertWithData.selector, address(reverter));
reverter.twoPossibleReverts(x);
reverter.twoPossibleReverts(2);
}
// /// @dev Test that `assumeNoRevert` correctly rejects any error selector when no selector is provided
// function testMultipleAssumes_ThrowOnGenericNoRevert_fails(bytes4 selector) public view {
// _vm.assumeNoRevert();
// _vm.assumeNoRevert(selector);
// reverter.twoPossibleReverts(2);
// }
/// @dev Test that `assumeNoRevert` correctly rejects a generic assumeNoRevert call after any specific reason is provided
function testMultipleAssumes_ThrowOnGenericNoRevert_AfterSpecific_fails(bytes4 selector) public view {
Expand Down

0 comments on commit bd54dc1

Please sign in to comment.