Commit 5e3c5d1c authored by smartcontracts's avatar smartcontracts Committed by GitHub

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

* fix[smock]: fix broken call assertions for overloaded functions

* chore: add changeset

* minor correction and add a test

* add a test for non-overloaded functions
parent 1293825c
---
'@eth-optimism/smock': patch
---
Fixes a bug that would break call assertions for overloaded smocked functions
...@@ -79,18 +79,25 @@ const smockifyFunction = ( ...@@ -79,18 +79,25 @@ const smockifyFunction = (
let data: any = toHexString(calldataBuf) let data: any = toHexString(calldataBuf)
try { try {
data = contract.interface.decodeFunctionData(fragment.name, data) data = contract.interface.decodeFunctionData(
fragment.format(),
data
)
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
return { return {
functionName: fragment.name, functionName: fragment.name,
functionSignature: fragment.format(),
data, data,
} }
}) })
.filter((functionResult: any) => { .filter((functionResult: any) => {
return functionResult.functionName === functionName return (
functionResult.functionName === functionName ||
functionResult.functionSignature === functionName
)
}) })
.map((functionResult: any) => { .map((functionResult: any) => {
return functionResult.data return functionResult.data
......
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract TestHelpers_MockCaller {
function callMock(address _target, bytes memory _data) public {
_target.call(_data);
}
}
/* 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])
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment