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

Add revert with reason functionality for mock contracts #349

Merged
merged 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions waffle-mock-contract/src/Doppelganger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ contract Doppelganger {
struct MockCall {
bool initialized;
bool reverts;
string revertReason;
bytes returnValue;
}

Expand All @@ -13,16 +14,17 @@ contract Doppelganger {
fallback() external payable {
MockCall storage mockCall = __internal__getMockCall();
if (mockCall.reverts == true) {
__internal__mockRevert();
__internal__mockRevert(mockCall.revertReason);
return;
}
__internal__mockReturn(mockCall.returnValue);
}

function __waffle__mockReverts(bytes memory data) public {
function __waffle__mockReverts(bytes memory data, string memory reason) public {
mockConfig[keccak256(data)] = MockCall({
initialized: true,
reverts: true,
revertReason: reason,
returnValue: ""
});
}
Expand All @@ -31,6 +33,7 @@ contract Doppelganger {
mockConfig[keccak256(data)] = MockCall({
initialized: true,
reverts: false,
revertReason: "",
returnValue: value
});
}
Expand Down Expand Up @@ -67,7 +70,7 @@ contract Doppelganger {
}
}

function __internal__mockRevert() pure private {
revert("Mock revert");
function __internal__mockRevert(string memory reason) pure private {
revert(reason);
}
}
3 changes: 2 additions & 1 deletion waffle-mock-contract/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function stub(mockContract: Contract, encoder: utils.AbiCoder, func: utils.Funct
const encoded = encoder.encode(func.outputs, args);
await mockContract.__waffle__mockReturns(callData, encoded);
},
reverts: async () => mockContract.__waffle__mockReverts(callData),
reverts: async () => mockContract.__waffle__mockReverts(callData, 'Mock revert'),
revertsWithReason: async (reason: string) => mockContract.__waffle__mockReverts(callData, reason),
withArgs: (...args: any[]) => stub(mockContract, encoder, func, args)
};
}
Expand Down
11 changes: 9 additions & 2 deletions waffle-mock-contract/test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,17 @@ describe('Doppelganger - Contract', () => {
expect(await pretender.testArgumentTypes(1, false, 'str', '0x0123')).to.equal('0x0123');
});

it('reverts with no message', async () => {
const {contract, pretender} = await deploy();

await contract.__waffle__mockReverts(readSignature, '');
await expect(pretender.read()).to.be.revertedWith('');
});

it('reverts with correct message', async () => {
const {contract, pretender} = await deploy();

await contract.__waffle__mockReverts(readSignature);
await contract.__waffle__mockReverts(readSignature, 'Mock revert');
await expect(pretender.read()).to.be.revertedWith('Mock revert');
});

Expand All @@ -118,7 +125,7 @@ describe('Doppelganger - Contract', () => {
const callData = `${addSignature}0000000000000000000000000000000000000000000000000000000000000005`;
const returnedValue = '0x1000000000000000000000000000000000000000000000000000000000004234';

await contract.__waffle__mockReverts(callData);
await contract.__waffle__mockReverts(callData, 'Mock revert');
await contract.__waffle__mockReturns(addSignature, returnedValue);

await expect(pretender.add(5)).to.be.revertedWith('Mock revert');
Expand Down