Skip to content

Commit

Permalink
potential direction for refactoring differential tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-carroll committed Nov 17, 2023
1 parent 180be2a commit a9ab358
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 5 additions & 13 deletions test/foundry/offerers/ContractOffersNativeTokenOfferItems.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,20 @@ contract ContractOffersNativeTokenOfferItems is

TestERC721 erc721;

function setUp() public override {
function setUp() public override snapshotted {
super.setUp();
erc721 = new TestERC721();
}

function test(
function(Context memory) external fn,
Context memory context
) internal {
try fn(context) {} catch (bytes memory reason) {
assertPass(reason);
}
}

function testEthForErc721(
FuzzArgs memory args
) public validateInputs(args) {
test(this.ethForErc721, Context(consideration,args));
test(this.ethForErc721, Context(referenceConsideration,args));
ethForErc721(Context(consideration,args));
revertToSetup();
ethForErc721(Context(referenceConsideration,args));
}

function ethForErc721(Context memory context) public stateless {
function ethForErc721(Context memory context) public {
TestContractOffererNativeToken contractOfferer = new TestContractOffererNativeToken(
address(context.seaport)
);
Expand Down
15 changes: 15 additions & 0 deletions test/foundry/utils/DifferentialTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pragma solidity ^0.8.17;
import { Test } from "forge-std/Test.sol";

contract DifferentialTest is Test {
// identifier for a snapshot taken at the end of a setUp function for a differential test
uint256 snapshot;

///@dev error to supply
error RevertWithFailureStatus(bool status);
error DifferentialTestAssertionFailed();
Expand All @@ -24,6 +27,18 @@ contract DifferentialTest is Test {
revert RevertWithFailureStatus(readHevmFailureSlot());
}

/// @notice setup the snapshot at the end of the setUp function
/// @dev apply modifier to `setUp` function, then run differential tests using `revertToSetup`
modifier snapshotted() {
_;
snapshot = vm.snapshot();
}

/// @notice revert to the state at the end of setUp
function revertToSetup() public {
vm.revertTo(snapshot);
}

///@dev revert if the supplied bytes do not match the expected "passing" revert bytes
function assertPass(bytes memory reason) internal view {
// hash the reason and compare to the hash of the passing revert bytes
Expand Down

0 comments on commit a9ab358

Please sign in to comment.