Skip to content

Commit

Permalink
fix: merge internal functions templates
Browse files Browse the repository at this point in the history
  • Loading branch information
agusduha committed Apr 29, 2024
1 parent 2476e3e commit d6365e5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 20 additions & 2 deletions src/templates/partials/internal-function.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}));

Expand All @@ -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}}
43 changes: 0 additions & 43 deletions src/templates/partials/internal-view-function.hbs

This file was deleted.

7 changes: 1 addition & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ export async function renderNodeMock(node: ASTNode): Promise<string> {
constructor: constructorContext,
'external-or-public-function': externalOrPublicFunctionContext,
'internal-function': internalFunctionContext,
'internal-view-function': internalFunctionContext,
import: importContext,
};

Expand Down Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion test/unit/context/internalFunctionContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down

0 comments on commit d6365e5

Please sign in to comment.