Skip to content

Commit

Permalink
test: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agusduha committed May 1, 2024
1 parent 5fb3b6b commit b7d7f01
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 0 deletions.
38 changes: 38 additions & 0 deletions solidity/test/utils/ContractA.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from 'forge-std/Test.sol';
import {MockContractA} from 'test/smock/contracts/utils/MockContractA.sol';
import {SmockHelper} from 'test/smock/SmockHelper.sol';

contract UnitMockContractA is Test, SmockHelper {
address internal _owner = makeAddr('owner');
MockContractA internal _contractTest;

uint256 internal _initialValue = 5;
uint256 internal _newValue = 10;

function setUp() public {
vm.prank(_owner);

_contractTest =
MockContractA(deployMock('TestContractA', type(MockContractA).creationCode, abi.encode(_initialValue)));
}

function test_Set_UintVariable() public {
_contractTest.set_uintVariable(_newValue);
assertEq(_contractTest.uintVariable(), _newValue);
}

function test_Call_UintVariable() public {
_contractTest.mock_call_uintVariable(_newValue);
assertEq(_contractTest.uintVariable(), _newValue);
}

function test_Call_SetVariablesA() public {
bool _result = true;

_contractTest.mock_call_setVariablesA(_newValue, _result);
assertEq(_contractTest.setVariablesA(_newValue), _result);
}
}
91 changes: 91 additions & 0 deletions solidity/test/utils/ContractAbstract.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from 'forge-std/Test.sol';
import {MockContractAbstract} from 'test/smock/contracts/utils/MockContractAbstract.sol';
import {SmockHelper} from 'test/smock/SmockHelper.sol';

contract UnitMockContractAbstract is Test, SmockHelper {
address internal _owner = makeAddr('owner');
MockContractAbstract internal _contractTest;

uint256 internal _initialValue = 5;
uint256 internal _newValue = 10;
string internal _someText = 'some text';
bool internal _result = true;

function setUp() public {
vm.prank(_owner);

_contractTest = MockContractAbstract(
deployMock('TestContractAbstract', type(MockContractAbstract).creationCode, abi.encode(_initialValue))
);
}

function test_Set_UintVariable() public {
_contractTest.set_uintVariable(_newValue);
assertEq(_contractTest.uintVariable(), _newValue);
}

function test_Call_UintVariable() public {
_contractTest.mock_call_uintVariable(_newValue);
assertEq(_contractTest.uintVariable(), _newValue);
}

function test_Call_SetVariablesA() public {
_contractTest.mock_call_setVariablesA(_newValue, _result);
assertEq(_contractTest.setVariablesA(_newValue), _result);
}

function test_Call_UndefinedFunc() public {
_contractTest.mock_call_undefinedFunc(_someText, _result);
assertEq(_contractTest.undefinedFunc(_someText), _result);
}

function test_Call_UndefinedFuncNoInputNoOutput() public {
vm.expectCall(address(_contractTest), abi.encodeCall(MockContractAbstract.undefinedFuncNoInputNoOutput, ()), 1);

_contractTest.mock_call_undefinedFuncNoInputNoOutput();
_contractTest.undefinedFuncNoInputNoOutput();
}

function test_Call_UndefinedViewFunc() public {
_contractTest.mock_call_undefinedViewFunc(_someText, _result);
assertEq(_contractTest.undefinedViewFunc(_someText), _result);
}

function test_Call_UndefinedViewFuncNoInputNoOutput() public {
vm.expectCall(address(_contractTest), abi.encodeCall(MockContractAbstract.undefinedViewFuncNoInputNoOutput, ()), 1);

_contractTest.mock_call_undefinedViewFuncNoInputNoOutput();
_contractTest.undefinedViewFuncNoInputNoOutput();
}

function test_Call_UndefinedInternalFunc() public {
_contractTest.expectCall__undefinedInternalFunc(_someText);

_contractTest.mock_call__undefinedInternalFunc(_someText, _result);
assertEq(_contractTest.call__undefinedInternalFunc(_someText), _result);
}

function test_Call_UndefinedInternalFuncNoInputNoOutput() public {
_contractTest.expectCall__undefinedInternalFuncNoInputNoOutput();

_contractTest.mock_call__undefinedInternalFuncNoInputNoOutput();
_contractTest.call__undefinedInternalFuncNoInputNoOutput();
}

function test_Call_UndefinedInternalViewFunc() public {
_contractTest.expectCall__undefinedInternalViewFunc(_someText);

_contractTest.mock_call__undefinedInternalViewFunc(_someText, _result);
assertEq(_contractTest.call__undefinedInternalViewFunc(_someText), _result);
}

function test_Call_UndefinedInternalViewFuncNoInputNoOutput() public {
_contractTest.expectCall__undefinedInternalViewFuncNoInputNoOutput();

_contractTest.mock_call__undefinedInternalViewFuncNoInputNoOutput();
_contractTest.call__undefinedInternalViewFuncNoInputNoOutput();
}
}
36 changes: 36 additions & 0 deletions solidity/test/utils/ContractB.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from 'forge-std/Test.sol';
import {MockContractB} from 'test/smock/contracts/utils/MockContractB.sol';
import {SmockHelper} from 'test/smock/SmockHelper.sol';

contract UnitMockContractB is Test, SmockHelper {
address internal _owner = makeAddr('owner');
MockContractB internal _contractTest;

string internal _newValue = 'new value';

function setUp() public {
vm.prank(_owner);

_contractTest = MockContractB(deployMock('TestContractB', type(MockContractB).creationCode, abi.encode()));
}

function test_Set_StringVariable() public {
_contractTest.set_stringVariable(_newValue);
assertEq(_contractTest.stringVariable(), _newValue);
}

function test_Call_StringVariable() public {
_contractTest.mock_call_stringVariable(_newValue);
assertEq(_contractTest.stringVariable(), _newValue);
}

function test_Call_SetVariablesB() public {
bool _result = true;

_contractTest.mock_call_setVariablesB(_newValue, _result);
assertEq(_contractTest.setVariablesB(_newValue), _result);
}
}
52 changes: 52 additions & 0 deletions solidity/test/utils/ContractBAbstract.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from 'forge-std/Test.sol';
import {MockContractBAbstract} from 'test/smock/contracts/utils/MockContractBAbstract.sol';
import {SmockHelper} from 'test/smock/SmockHelper.sol';

contract UnitMockContractBAbstract is Test, SmockHelper {
address internal _owner = makeAddr('owner');
MockContractBAbstract internal _contractTest;

uint256 internal _initialValue = 5;
uint256 internal _newValue = 10;
bool internal _result = true;

function setUp() public {
vm.prank(_owner);

_contractTest = MockContractBAbstract(
deployMock('TestContractBAbstract', type(MockContractBAbstract).creationCode, abi.encode(_initialValue))
);
}

function test_Set_UintVariable() public {
_contractTest.set_uintVariable(_newValue);
assertEq(_contractTest.uintVariable(), _newValue);
}

function test_Call_UintVariable() public {
_contractTest.mock_call_uintVariable(_newValue);
assertEq(_contractTest.uintVariable(), _newValue);
}

function test_Call_SetVariablesA() public {
_contractTest.mock_call_setVariablesA(_newValue, _result);
assertEq(_contractTest.setVariablesA(_newValue), _result);
}

function test_Call_UndefinedInterfaceFunc() public {
vm.expectCall(address(_contractTest), abi.encodeCall(MockContractBAbstract.undefinedInterfaceFunc, (_newValue)), 1);

_contractTest.mock_call_undefinedInterfaceFunc(_newValue, _result);
assertEq(_contractTest.undefinedInterfaceFunc(_newValue), _result);
}

function test_Call_UndefinedInterfaceFunc2() public {
vm.expectCall(address(_contractTest), abi.encodeCall(MockContractBAbstract.undefinedInterfaceFunc2, (_newValue)), 1);

_contractTest.mock_call_undefinedInterfaceFunc2(_newValue, _result);
assertEq(_contractTest.undefinedInterfaceFunc2(_newValue), _result);
}
}
38 changes: 38 additions & 0 deletions solidity/test/utils/ContractCA.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from 'forge-std/Test.sol';
import {MockContractCA} from 'test/smock/contracts/utils/MockContractCA.sol';
import {SmockHelper} from 'test/smock/SmockHelper.sol';

contract UnitMockContractCA is Test, SmockHelper {
address internal _owner = makeAddr('owner');
MockContractCA internal _contractTest;

uint256 internal _initialValue = 5;
uint256 internal _newValue = 10;

function setUp() public {
vm.prank(_owner);

_contractTest =
MockContractCA(deployMock('TestContractCA', type(MockContractCA).creationCode, abi.encode(_initialValue)));
}

function test_Set_Uint256Variable() public {
_contractTest.set_uint256Variable(_newValue);
assertEq(_contractTest.uint256Variable(), _newValue);
}

function test_Call_Uint256Variable() public {
_contractTest.mock_call_uint256Variable(_newValue);
assertEq(_contractTest.uint256Variable(), _newValue);
}

function test_Call_SetVariablesC() public {
bool _result = true;

_contractTest.mock_call_setVariablesC(_newValue, _result);
assertEq(_contractTest.setVariablesC(_newValue), _result);
}
}
28 changes: 28 additions & 0 deletions solidity/test/utils/ContractE.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from 'forge-std/Test.sol';
import {MockContractE} from 'test/smock/contracts/utils/MockContractE.sol';
import {SmockHelper} from 'test/smock/SmockHelper.sol';

contract UnitMockContractE is Test, SmockHelper {
address internal _owner = makeAddr('owner');
MockContractE internal _contractTest;

uint256 internal _initialValue = 5;
uint256 internal _initialValue2 = 10;
uint256 internal _newValue = 15;

function setUp() public {
vm.prank(_owner);

_contractTest = MockContractE(
deployMock('TestContractE', type(MockContractE).creationCode, abi.encode(_initialValue, _initialValue2))
);
}

function test_Set_InternalUintVar2() public {
_contractTest.set__internalUintVar2(_newValue);
assertEq(_contractTest.call__internalUintVar2(), _newValue);
}
}
34 changes: 34 additions & 0 deletions solidity/test/utils/ContractF.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {Test} from 'forge-std/Test.sol';
import {MockContractF} from 'test/smock/contracts/utils/MockContractF.sol';
import {SmockHelper} from 'test/smock/SmockHelper.sol';

contract UnitMockContractF is Test, SmockHelper {
address internal _owner = makeAddr('owner');
MockContractF internal _contractTest;

uint256 internal _initialValue = 5;
uint256 internal _newValue = 15;

function setUp() public {
vm.prank(_owner);

_contractTest = MockContractF(deployMock('TestContractF', type(MockContractF).creationCode, abi.encode(_owner)));
}

function test_Call_SetVariablesA() public {
vm.expectCall(address(_contractTest), abi.encodeCall(MockContractF.setVariablesA, _newValue));
_contractTest.mock_call_setVariablesA(_newValue, true);

assert(_contractTest.setVariablesA(_newValue));
}

function test_Call_SetVariablesB() public {
vm.expectCall(address(_contractTest), abi.encodeCall(MockContractF.setVariablesB, _newValue));
_contractTest.mock_call_setVariablesB(_newValue, true);

assert(_contractTest.setVariablesB(_newValue));
}
}

0 comments on commit b7d7f01

Please sign in to comment.