-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mock internal pure functions (#69)
- Loading branch information
Showing
8 changed files
with
151 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,17 @@ | ||
{{#if isView}} | ||
{{#if outputs}} | ||
struct {{functionName}}Output { | ||
{{#each outputTypes}} | ||
{{this}} {{lookup ../outputNames @index}}; | ||
{{/each}} | ||
} | ||
|
||
mapping(bytes32 => {{functionName}}Output) private {{functionName}}Outputs; | ||
{{/if}} | ||
|
||
bytes32[] private {{functionName}}InputHashes; | ||
{{/if}} | ||
|
||
function mock_call_{{functionName}}({{parameters}}) public { | ||
{{#if isView}} | ||
bytes32 _key = keccak256(abi.encode({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}})); | ||
{{#if outputs}} | ||
{{functionName}}Outputs[_key] = {{functionName}}Output({{#each outputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
{{/if}} | ||
|
||
for (uint256 _i; _i < {{functionName}}InputHashes.length; ++_i) { | ||
if (_key == {{functionName}}InputHashes[_i]) return; | ||
} | ||
|
||
{{functionName}}InputHashes.push(_key); | ||
{{else}} | ||
vm.mockCall( | ||
address(this), | ||
abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}}), | ||
abi.encode({{#each outputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}) | ||
); | ||
{{/if}} | ||
vm.mockCall( | ||
address(this), | ||
abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}}), | ||
abi.encode({{#each outputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}) | ||
); | ||
} | ||
|
||
function {{functionName}}({{inputs}}) internal {{#if isView}}view {{/if}}override {{#if outputs}}returns ({{outputs}}){{/if}} { | ||
{{#if isView}} | ||
bytes32 _key = keccak256(abi.encode({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}})); | ||
|
||
for (uint256 _i; _i < {{functionName}}InputHashes.length; ++_i) { | ||
if (_key == {{functionName}}InputHashes[_i]) { | ||
{{#if outputs}} | ||
{{functionName}}Output memory _output = {{functionName}}Outputs[_key]; | ||
{{/if}} | ||
|
||
return ({{#each outputNames}}_output.{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
} | ||
} | ||
|
||
{{#if implemented}} | ||
return super.{{functionName}}({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
{{/if}} | ||
{{else}} | ||
(bool _success, bytes memory _data) = address(this).call(abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}})); | ||
|
||
if (_success) return abi.decode(_data, ({{#each outputTypes}}{{this}}{{#unless @last}}, {{/unless}}{{/each}})); | ||
|
||
{{#if implemented}} | ||
else return super.{{functionName}}({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
{{/if}} | ||
function {{functionName}}({{inputs}}) internal override {{#if outputs}}returns ({{outputs}}){{/if}} { | ||
(bool _success, bytes memory _data) = address(this).call(abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}})); | ||
|
||
if (_success) return abi.decode(_data, ({{#each outputTypes}}{{this}}{{#unless @last}}, {{/unless}}{{/each}})); | ||
|
||
{{#if implemented}} | ||
else return super.{{functionName}}({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
{{/if}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{{#if outputs}} | ||
struct {{functionName}}Output { | ||
{{#each outputTypes}} | ||
{{this}} {{lookup ../outputNames @index}}; | ||
{{/each}} | ||
} | ||
|
||
mapping(bytes32 => {{functionName}}Output) private {{functionName}}Outputs; | ||
{{/if}} | ||
|
||
bytes32[] private {{functionName}}InputHashes; | ||
|
||
function mock_call_{{functionName}}({{parameters}}) public { | ||
bytes32 _key = keccak256(abi.encode({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}})); | ||
{{#if outputs}} | ||
{{functionName}}Outputs[_key] = {{functionName}}Output({{#each outputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
{{/if}} | ||
|
||
for (uint256 _i; _i < {{functionName}}InputHashes.length; ++_i) { | ||
if (_key == {{functionName}}InputHashes[_i]) return; | ||
} | ||
|
||
{{functionName}}InputHashes.push(_key); | ||
} | ||
|
||
function {{functionName}}{{#if isPure}}Helper{{/if}}({{inputs}}) internal view {{#unless isPure}}override{{/unless}} {{#if outputs}}returns ({{outputs}}){{/if}} { | ||
bytes32 _key = keccak256(abi.encode({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}})); | ||
|
||
for (uint256 _i; _i < {{functionName}}InputHashes.length; ++_i) { | ||
if (_key == {{functionName}}InputHashes[_i]) { | ||
{{#if outputs}} | ||
{{functionName}}Output memory _output = {{functionName}}Outputs[_key]; | ||
{{/if}} | ||
|
||
return ({{#each outputNames}}_output.{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
} | ||
} | ||
|
||
{{#if implemented}} | ||
return super.{{functionName}}({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
{{/if}} | ||
} | ||
|
||
{{#if isPure}} | ||
|
||
function _{{functionName}}CastToPure(function({{#each inputTypes}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}) internal view {{#if outputs}}returns ({{explicitOutputTypes}}){{/if}} fnIn) | ||
internal | ||
pure | ||
returns (function({{#each inputTypes}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}) internal pure {{#if outputs}}returns ({{explicitOutputTypes}}){{/if}} fnOut) | ||
{ | ||
assembly { | ||
fnOut := fnIn | ||
} | ||
} | ||
|
||
function {{functionName}}({{inputs}}) internal pure override {{#if outputs}}returns ({{outputs}}){{/if}} { | ||
return _{{functionName}}CastToPure({{functionName}}Helper)({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); | ||
} | ||
|
||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters