From d6365e5bd5aa71b8a8db0fd4e5012064f71c931e Mon Sep 17 00:00:00 2001 From: agusduha Date: Mon, 29 Apr 2024 14:58:34 -0300 Subject: [PATCH] fix: merge internal functions templates --- src/context.ts | 2 +- src/templates/partials/internal-function.hbs | 22 +++++++++- .../partials/internal-view-function.hbs | 43 ------------------- src/utils.ts | 7 +-- .../context/internalFunctionContext.spec.ts | 3 +- 5 files changed, 24 insertions(+), 53 deletions(-) delete mode 100644 src/templates/partials/internal-view-function.hbs diff --git a/src/context.ts b/src/context.ts index eb45ff3..5e1fc04 100644 --- a/src/context.ts +++ b/src/context.ts @@ -46,8 +46,8 @@ export function internalFunctionContext(node: FunctionDefinition): InternalFunct } // Check if the function is view or pure - const isView = node.stateMutability === FunctionStateMutability.View; const isPure = node.stateMutability === FunctionStateMutability.Pure; + const isView = node.stateMutability === FunctionStateMutability.View || isPure; // Save the internal function information return { diff --git a/src/templates/partials/internal-function.hbs b/src/templates/partials/internal-function.hbs index 22e2d90..f231804 100644 --- a/src/templates/partials/internal-function.hbs +++ b/src/templates/partials/internal-function.hbs @@ -6,8 +6,8 @@ function mock_call_{{functionName}}({{parameters}}) public { ); } -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}})); +function {{functionName}}{{#if isPure}}Helper{{/if}}({{inputs}}) internal {{#if isView}}view{{/if}} {{#unless isPure}}override{{/unless}} {{#if outputs}}returns ({{outputs}}){{/if}} { + (bool _success, bytes memory _data) = address(this).{{#if isView}}static{{/if}}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}})); @@ -23,3 +23,21 @@ function call_{{functionName}}({{inputs}}) public {{#if outputs}}returns ({{outp function expectCall_{{functionName}}({{#if inputs}}{{inputs}}{{/if}}) public { vm.expectCall(address(this), abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#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}} \ No newline at end of file diff --git a/src/templates/partials/internal-view-function.hbs b/src/templates/partials/internal-view-function.hbs deleted file mode 100644 index 110ff83..0000000 --- a/src/templates/partials/internal-view-function.hbs +++ /dev/null @@ -1,43 +0,0 @@ -function mock_call_{{functionName}}({{parameters}}) public { - 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}}{{#if isPure}}Helper{{/if}}({{inputs}}) internal view {{#unless isPure}}override{{/unless}} {{#if outputs}}returns ({{outputs}}){{/if}} { - (bool _success, bytes memory _data) = address(this).staticcall(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}} -} - -{{#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}} - -function call_{{functionName}}({{inputs}}) public {{#if isView}}view{{else}}pure{{/if}} {{#if outputs}}returns ({{outputs}}){{/if}} { - return {{functionName}}({{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}); -} - -function expectCall_{{functionName}}({{#if inputs}}{{inputs}}{{/if}}) public { - vm.expectCall(address(this), abi.encodeWithSignature('{{signature}}'{{#if inputs}}, {{#each inputNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}})); -} diff --git a/src/utils.ts b/src/utils.ts index 1081115..a2335f8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -192,7 +192,6 @@ export async function renderNodeMock(node: ASTNode): Promise { constructor: constructorContext, 'external-or-public-function': externalOrPublicFunctionContext, 'internal-function': internalFunctionContext, - 'internal-view-function': internalFunctionContext, import: importContext, }; @@ -222,11 +221,7 @@ export function partialName(node: ASTNode): string { } else if (node.visibility === 'external' || node.visibility === 'public') { return 'external-or-public-function'; } else if (node.visibility === 'internal' && node.virtual) { - if (node.stateMutability === 'view' || node.stateMutability === 'pure') { - return 'internal-view-function'; - } else { - return 'internal-function'; - } + return 'internal-function'; } } else if (node instanceof ImportDirective) { return 'import'; diff --git a/test/unit/context/internalFunctionContext.spec.ts b/test/unit/context/internalFunctionContext.spec.ts index 5191273..ffc8182 100644 --- a/test/unit/context/internalFunctionContext.spec.ts +++ b/test/unit/context/internalFunctionContext.spec.ts @@ -198,9 +198,10 @@ describe('internalFunctionContext', () => { it('determines whether the function is view or not', () => { for (const stateMutability in FunctionStateMutability) { const isView = FunctionStateMutability[stateMutability] === FunctionStateMutability.View; + const isPure = FunctionStateMutability[stateMutability] === FunctionStateMutability.Pure; const node = mockFunctionDefinition({ ...defaultAttributes, stateMutability: FunctionStateMutability[stateMutability] }); const context = internalFunctionContext(node); - expect(context.isView).to.be.equal(isView); + expect(context.isView).to.be.equal(isView || isPure); } });