Skip to content

Commit 8d63235

Browse files
authored
feat: add call to internal state variables in mocks (#76)
1 parent ff8a692 commit 8d63235

File tree

6 files changed

+229
-18
lines changed

6 files changed

+229
-18
lines changed

solidity/contracts/utils/ContractG.sol

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ contract ContractG {
3737
CommonStruct _common;
3838
}
3939

40-
mapping(bytes32 _disputeId => bool _finished) public _finished;
40+
mapping(bytes32 _disputeId => bool _finished) public finished;
41+
42+
mapping(bytes32 _disputeId => bool _finished) internal _finishedInternal;
43+
44+
mapping(bytes32 _key1 => mapping(bytes32 _key2 => bool _finished)) internal _doubleFinishedInternal;
4145

4246
mapping(bytes32 _disputeId => Set _votersSet) internal _votersA;
4347

@@ -51,10 +55,14 @@ contract ContractG {
5155

5256
mapping(bytes32 _disputeId => NestedStruct _nestedStruct) public _nestedStructs;
5357

58+
mapping(bytes32 _disputeId => NestedStruct _nestedStruct) internal _nestedStructsInternal;
59+
5460
NestedStruct public nestedStruct;
5561

5662
CommonStruct[] public structArray;
5763

64+
CommonStruct[] internal _structArrayInternal;
65+
5866
CommonStruct[][] public twoDimensionalStruct;
5967

6068
CommonStruct[][][] public threeDimensionalStruct;

solidity/test/utils/ContractD.t.sol

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import {Test} from 'forge-std/Test.sol';
5+
import {MockContractD} from 'test/smock/contracts/utils/MockContractD.sol';
6+
import {SmockHelper} from 'test/smock/SmockHelper.sol';
7+
8+
contract UnitMockContractD is Test, SmockHelper {
9+
address internal _owner = makeAddr('owner');
10+
MockContractD internal _contractTest;
11+
12+
uint256 internal _initialValue = 5;
13+
uint256 internal _newValue = 10;
14+
string internal _someText = 'some text';
15+
16+
function setUp() public {
17+
vm.prank(_owner);
18+
19+
_contractTest =
20+
MockContractD(deployMock('TestContractD', type(MockContractD).creationCode, abi.encode(_initialValue)));
21+
}
22+
23+
function test_Set_InternalUintVar() public {
24+
_contractTest.set__internalUintVar(_newValue);
25+
assertEq(_contractTest.call__internalUintVar(), _newValue);
26+
}
27+
28+
function test_Call_InternalUintVar() public {
29+
_contractTest.expectCall__setInternalUintVar(_initialValue);
30+
_contractTest.mock_call__setInternalUintVar(_initialValue, true, _newValue, _someText);
31+
32+
(bool _success, uint256 _value, string memory _text) = _contractTest.call__setInternalUintVar(_initialValue);
33+
34+
assert(_success);
35+
assertEq(_value, _newValue);
36+
assertEq(_text, _someText);
37+
}
38+
39+
function test_Call_GetVariables() public {
40+
_contractTest.expectCall__getVariables(_initialValue);
41+
_contractTest.mock_call__getVariables(_initialValue, true, _newValue, _someText);
42+
43+
(bool _success, uint256 _value, string memory _text) = _contractTest.call__getVariables(_initialValue);
44+
45+
assert(_success);
46+
assertEq(_value, _newValue);
47+
assertEq(_text, _someText);
48+
}
49+
50+
function test_Call_InternalFuncNoInputNoOutput() public {
51+
_contractTest.expectCall__internalFuncNoInputNoOutput();
52+
_contractTest.mock_call__internalFuncNoInputNoOutput();
53+
54+
_contractTest.call__internalFuncNoInputNoOutput();
55+
}
56+
57+
function test_Call_InternalViewFuncNoInputNoOutput() public {
58+
_contractTest.expectCall__internalViewFuncNoInputNoOutput();
59+
_contractTest.mock_call__internalViewFuncNoInputNoOutput();
60+
61+
_contractTest.call__internalViewFuncNoInputNoOutput();
62+
}
63+
}

solidity/test/utils/ContractG.t.sol

+122
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,126 @@ contract UnitMockContractG is Test, SmockHelper {
227227
(_value) = _contractTest.twoDimensionalStringArray(1, 1);
228228
assertEq(_value, '40');
229229
}
230+
231+
function test_Set_NestedStruct() public {
232+
ContractG.CommonStruct memory _commonStruct = ContractG.CommonStruct(20);
233+
ContractG.NestedStruct memory _nestedStruct = ContractG.NestedStruct(10, _commonStruct);
234+
235+
_contractTest.set_nestedStruct(_nestedStruct);
236+
237+
(uint256 _counter, ContractG.CommonStruct memory _commonResult) = _contractTest.nestedStruct();
238+
239+
assertEq(_counter, 10);
240+
assertEq(_commonResult._value, 20);
241+
}
242+
243+
function test_Call_NestedStruct() public {
244+
ContractG.CommonStruct memory _commonStruct = ContractG.CommonStruct(20);
245+
ContractG.NestedStruct memory _nestedStruct = ContractG.NestedStruct(10, _commonStruct);
246+
247+
_contractTest.mock_call_nestedStruct(_nestedStruct);
248+
249+
(uint256 _counter, ContractG.CommonStruct memory _commonResult) = _contractTest.nestedStruct();
250+
251+
assertEq(_counter, 10);
252+
assertEq(_commonResult._value, 20);
253+
}
254+
255+
function test_Call_SetNestedStruct() public {
256+
ContractG.CommonStruct memory _commonStruct = ContractG.CommonStruct(20);
257+
ContractG.NestedStruct memory _nestedStruct = ContractG.NestedStruct(10, _commonStruct);
258+
259+
_contractTest.mock_call_setNestedStruct(_nestedStruct);
260+
_contractTest.setNestedStruct(_nestedStruct);
261+
262+
(uint256 _counter, ContractG.CommonStruct memory _commonResult) = _contractTest.nestedStruct();
263+
264+
assertEq(_counter, 10);
265+
assertEq(_commonResult._value, 20);
266+
}
267+
268+
function test_Set_StructArray() public {
269+
ContractG.CommonStruct[] memory _structArray = new ContractG.CommonStruct[](2);
270+
_structArray[0] = ContractG.CommonStruct(10);
271+
_structArray[1] = ContractG.CommonStruct(20);
272+
273+
_contractTest.set_structArray(_structArray);
274+
(uint256 _value) = _contractTest.structArray(0);
275+
assertEq(_value, 10);
276+
277+
(_value) = _contractTest.structArray(1);
278+
assertEq(_value, 20);
279+
}
280+
281+
function test_Call_StructArray() public {
282+
ContractG.CommonStruct[] memory _structArray = new ContractG.CommonStruct[](2);
283+
_structArray[0] = ContractG.CommonStruct(10);
284+
_structArray[1] = ContractG.CommonStruct(20);
285+
286+
_contractTest.mock_call_structArray(0, _structArray[0]);
287+
(uint256 _value) = _contractTest.structArray(0);
288+
assertEq(_value, 10);
289+
290+
_contractTest.mock_call_structArray(1, _structArray[1]);
291+
(_value) = _contractTest.structArray(1);
292+
assertEq(_value, 20);
293+
}
294+
295+
function test_Set_StructArrayInternal() public {
296+
ContractG.CommonStruct[] memory _structArray = new ContractG.CommonStruct[](2);
297+
_structArray[0] = ContractG.CommonStruct(10);
298+
_structArray[1] = ContractG.CommonStruct(20);
299+
300+
_contractTest.set__structArrayInternal(_structArray);
301+
302+
ContractG.CommonStruct[] memory _resultArray = _contractTest.call__structArrayInternal();
303+
assertEq(_resultArray[0]._value, 10);
304+
assertEq(_resultArray[1]._value, 20);
305+
}
306+
307+
function test_Set_Finished() public {
308+
bytes32 _key = 'key';
309+
310+
_contractTest.set_finished(_key, true);
311+
312+
bool _value = _contractTest.finished(_key);
313+
314+
assertTrue(_value);
315+
}
316+
317+
function test_Call_Finished() public {
318+
bytes32 _key = 'key';
319+
_contractTest.mock_call_finished(_key, true);
320+
321+
bool _value = _contractTest.finished(_key);
322+
assertTrue(_value);
323+
}
324+
325+
function test_Set_FinishedInternal() public {
326+
bytes32 _key = 'key';
327+
_contractTest.set__finishedInternal(_key, true);
328+
329+
bool _value = _contractTest.call__finishedInternal(_key);
330+
assertTrue(_value);
331+
}
332+
333+
function test_Set_DoubleFinishedInternal() public {
334+
bytes32 _key0 = 'key0';
335+
bytes32 _key1 = 'key1';
336+
_contractTest.set__doubleFinishedInternal(_key0, _key1, true);
337+
338+
bool _value = _contractTest.call__doubleFinishedInternal(_key0, _key1);
339+
assertTrue(_value);
340+
}
341+
342+
function test_Set_NestedStructsInternal() public {
343+
bytes32 _key = 'key';
344+
ContractG.NestedStruct memory _nestedStruct = ContractG.NestedStruct(10, ContractG.CommonStruct(20));
345+
346+
_contractTest.set__nestedStructsInternal(_key, _nestedStruct);
347+
348+
ContractG.NestedStruct memory _result = _contractTest.call__nestedStructsInternal(_key);
349+
assertEq(_result._counter, 10);
350+
assertEq(_result._common._value, 20);
351+
}
230352
}

src/templates/partials/array-state-variable.hbs

+23-17
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@
1010
}
1111

1212
{{#unless isInternal}}
13-
function mock_call_{{mockFunction.functionName}}({{#each dimensions}}uint256 _index{{@index}}, {{/each}} {{mockFunction.baseType}} _value) public {
14-
vm.mockCall(
15-
address(this),
16-
abi.encodeWithSignature(
17-
'{{mockFunction.functionName}}({{#each dimensions}}uint256{{#unless @last}},{{/unless}}{{/each}})',
18-
{{#each dimensions}}_index{{@index}}{{#unless @last}}, {{/unless}} {{/each}}),
19-
abi.encode(
20-
{{#if isStructArray}}
21-
{{#each mockFunction.structFields}}
22-
_value.{{this}}{{#unless @last}}, {{/unless}}
23-
{{/each}}
24-
{{else}}
25-
_value
26-
{{/if}}
27-
)
28-
);
29-
}
13+
function mock_call_{{mockFunction.functionName}}({{#each dimensions}}uint256 _index{{@index}}, {{/each}} {{mockFunction.baseType}} _value) public {
14+
vm.mockCall(
15+
address(this),
16+
abi.encodeWithSignature(
17+
'{{mockFunction.functionName}}({{#each dimensions}}uint256{{#unless @last}},{{/unless}}{{/each}})',
18+
{{#each dimensions}}_index{{@index}}{{#unless @last}}, {{/unless}} {{/each}}),
19+
abi.encode(
20+
{{#if isStructArray}}
21+
{{#each mockFunction.structFields}}
22+
_value.{{this}}{{#unless @last}}, {{/unless}}
23+
{{/each}}
24+
{{else}}
25+
_value
26+
{{/if}}
27+
)
28+
);
29+
}
3030
{{/unless}}
31+
32+
{{#if isInternal}}
33+
function call_{{setFunction.functionName}}() view public returns ({{setFunction.arrayType}}) {
34+
return {{setFunction.functionName}};
35+
}
36+
{{/if}}
3137
{{/unless}}

src/templates/partials/mapping-state-variable.hbs

+6
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@
3535
);
3636
}
3737
{{/unless}}
38+
39+
{{#if isInternal}}
40+
function call_{{setFunction.functionName}}({{#each setFunction.keyTypes}}{{this}} _key{{@index}}{{#unless @last}}, {{/unless}}{{/each}}) view public returns ({{setFunction.valueType}}) {
41+
return {{setFunction.functionName}}{{#each setFunction.keyTypes}}[_key{{@index}}]{{/each}};
42+
}
43+
{{/if}}
3844
{{/unless}}

src/templates/partials/state-variable.hbs

+6
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ function set_{{setFunction.functionName}}({{setFunction.paramType}} _{{setFuncti
1919
);
2020
}
2121
{{/unless}}
22+
23+
{{#if isInternal}}
24+
function call_{{setFunction.functionName}}() view public returns ({{setFunction.paramType}}) {
25+
return {{setFunction.functionName}};
26+
}
27+
{{/if}}

0 commit comments

Comments
 (0)