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

fix[smock]: fix broken call assertions for overloaded functions #996

Merged
merged 4 commits into from
Jun 2, 2021
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
5 changes: 5 additions & 0 deletions .changeset/cool-baboons-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/smock': patch
---

Fixes a bug that would break call assertions for overloaded smocked functions
11 changes: 9 additions & 2 deletions packages/smock/src/smockit/smockit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,25 @@ const smockifyFunction = (

let data: any = toHexString(calldataBuf)
try {
data = contract.interface.decodeFunctionData(fragment.name, data)
data = contract.interface.decodeFunctionData(
fragment.format(),
data
)
} catch (e) {
console.error(e)
}

return {
functionName: fragment.name,
functionSignature: fragment.format(),
data,
}
})
.filter((functionResult: any) => {
return functionResult.functionName === functionName
return (
functionResult.functionName === functionName ||
functionResult.functionSignature === functionName
)
})
.map((functionResult: any) => {
return functionResult.data
Expand Down
8 changes: 8 additions & 0 deletions packages/smock/test/contracts/TestHelpers_MockCaller.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;

contract TestHelpers_MockCaller {
function callMock(address _target, bytes memory _data) public {
_target.call(_data);
}
}
72 changes: 72 additions & 0 deletions packages/smock/test/smockit/call-assertions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Imports: External */
import hre from 'hardhat'
import { expect } from 'chai'
import { Contract } from 'ethers'

/* Imports: Internal */
import { MockContract, smockit } from '../../src'

describe('[smock]: call assertion tests', () => {
const ethers = (hre as any).ethers

let mock: MockContract
beforeEach(async () => {
mock = await smockit('TestHelpers_BasicReturnContract')
})

let mockCaller: Contract
before(async () => {
const mockCallerFactory = await ethers.getContractFactory(
'TestHelpers_MockCaller'
)
mockCaller = await mockCallerFactory.deploy()
})

describe('call assertions for functions', () => {
it('should be able to make assertions about a non-overloaded function', async () => {
mock.smocked.getInputtedUint256.will.return.with(0)

const expected1 = ethers.BigNumber.from(1234)
await mockCaller.callMock(
mock.address,
mock.interface.encodeFunctionData('getInputtedUint256(uint256)', [
expected1,
])
)

expect(mock.smocked.getInputtedUint256.calls[0]).to.deep.equal([
expected1,
])
})

it('should be able to make assertions about both versions of an overloaded function', async () => {
mock.smocked['overloadedFunction(uint256)'].will.return.with(0)
mock.smocked['overloadedFunction(uint256,uint256)'].will.return.with(0)

const expected1 = ethers.BigNumber.from(1234)
await mockCaller.callMock(
mock.address,
mock.interface.encodeFunctionData('overloadedFunction(uint256)', [
expected1,
])
)

expect(
mock.smocked['overloadedFunction(uint256)'].calls[0]
).to.deep.equal([expected1])

const expected2 = ethers.BigNumber.from(5678)
await mockCaller.callMock(
mock.address,
mock.interface.encodeFunctionData(
'overloadedFunction(uint256,uint256)',
[expected2, expected2]
)
)

expect(
mock.smocked['overloadedFunction(uint256,uint256)'].calls[0]
).to.deep.equal([expected2, expected2])
})
})
})