Skip to content

Commit

Permalink
test: add diamond cut initializer tests
Browse files Browse the repository at this point in the history
Both added tests verify that revert happens when facet initializer function reverts,
with and without error message.

Resolves: ubiquity#838
  • Loading branch information
gitcoindev committed Dec 27, 2023
1 parent c8bedfe commit 5bc784c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
4 changes: 1 addition & 3 deletions packages/contracts/src/dollar/mocks/MockFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ contract MockFacetInitializer {
revert();
}

error MockCustomError(string);

function initializeRevertWithMessage() external pure returns (uint256) {
revert MockCustomError("MockFacetError");
revert("MockFacetError");
}
}

Expand Down
42 changes: 42 additions & 0 deletions packages/contracts/test/diamond/DiamondTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ contract TestDiamond is DiamondTestSetup {
diamondCutFacet.diamondCut(facetCut, address(0x0), "");
}

function testCutFacetShouldRevertWhenFacetInitializerReverts() public {
FacetCut[] memory facetCut = new FacetCut[](1);
facetCut[0] = FacetCut({
facetAddress: address(pureFacet),
action: FacetCutAction.Replace,
functionSelectors: selectorsOfCollectableDustFacet
});

vm.expectRevert();

vm.prank(owner);
diamondCutFacet.diamondCut(
facetCut,
facetInitializer,
abi.encodeWithSelector(
MockFacetInitializer.initializeRevert.selector
)
);
}

function testCutFacetShouldRevertWithMessageWhenFacetInitializerWithMessageReverts()
public
{
FacetCut[] memory facetCut = new FacetCut[](1);
facetCut[0] = FacetCut({
facetAddress: address(pureFacet),
action: FacetCutAction.Replace,
functionSelectors: selectorsOfCollectableDustFacet
});

vm.expectRevert("MockFacetError");

vm.prank(owner);
diamondCutFacet.diamondCut(
facetCut,
facetInitializer,
abi.encodeWithSelector(
MockFacetInitializer.initializeRevertWithMessage.selector
)
);
}

function testSelectors_ShouldBeAssociatedWithCorrectFacet() public {
for (uint256 i; i < facetAddressList.length; i++) {
if (compareStrings(facetNames[i], "DiamondCutFacet")) {
Expand Down

0 comments on commit 5bc784c

Please sign in to comment.