Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
feat[smock]: add support for overloaded functions (ethereum-optimism#966
Browse files Browse the repository at this point in the history
)

* feat[smock]: add support for overloaded functions

* chore: add changeset
  • Loading branch information
smartcontracts authored May 27, 2021
1 parent c84d345 commit e6e87ae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/neat-melons-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/smock': patch
---

Fix a bug where overloaded functions would not be handled correctly
2 changes: 1 addition & 1 deletion packages/smock/src/smockit/smockit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const smockit = async (
let mockFn: any
if (fn !== null) {
params = this.interface.decodeFunctionData(fn, toHexString(data))
mockFn = this.smocked[fn.name]
mockFn = this.smocked[fn.name] || this.smocked[fn.format()]
} else {
params = toHexString(data)
mockFn = this.smocked.fallback
Expand Down
19 changes: 19 additions & 0 deletions packages/smock/test/contracts/TestHelpers_BasicReturnContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,23 @@ contract TestHelpers_BasicReturnContract {
uint256[] memory _out
)
{}

function overloadedFunction(
uint256 _paramA,
uint256 _paramB
)
public
returns (
uint256
)
{}

function overloadedFunction(
uint256
)
public
returns (
uint256
)
{}
}
17 changes: 17 additions & 0 deletions packages/smock/test/smockit/function-manipulation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ describe('[smock]: function manipulation tests', () => {
// TODO
})

describe('overloaded functions', () => {
it('should be able to modify both versions of an overloaded function', async () => {
const expected1 = 1234
const expected2 = 5678
mock.smocked['overloadedFunction(uint256)'].will.return.with(expected1)
mock.smocked['overloadedFunction(uint256,uint256)'].will.return.with(
expected2
)
expect(
await mock.callStatic['overloadedFunction(uint256)'](0)
).to.equal(expected1)
expect(
await mock.callStatic['overloadedFunction(uint256,uint256)'](0, 0)
).to.equal(expected2)
})
})

describe('returning with data', () => {
describe('fixed data types', () => {
describe('default behaviors', () => {
Expand Down

0 comments on commit e6e87ae

Please sign in to comment.