diff --git a/solidity/test/unit/modules/Module.t.sol b/solidity/test/unit/modules/Module.t.sol index 4aed1c4..343d724 100644 --- a/solidity/test/unit/modules/Module.t.sol +++ b/solidity/test/unit/modules/Module.t.sol @@ -1,83 +1,61 @@ -// // SPDX-License-Identifier: MIT -// pragma solidity ^0.8.19; - -// import 'forge-std/Test.sol'; - -// import {Module, IModule} from '../../../contracts/Module.sol'; -// import {IOracle} from '../../../interfaces/IOracle.sol'; - -// /** -// * @dev Harness to deploy the abstract contract -// */ -// contract ForTest_Module is Module { -// constructor(IOracle _oracle) Module(_oracle) {} - -// function moduleName() external pure returns (string memory _moduleName) { -// return 'abstractModule'; -// } -// } - -// /** -// * @title Module Abstract Unit tests -// */ -// contract Module_UnitTest is Test { -// // The target contract -// Module public module; - -// // A mock oracle -// IOracle public oracle; - -// /** -// * @notice Deploy the target and mock oracle extension -// */ -// function setUp() public { -// oracle = IOracle(makeAddr('Oracle')); -// vm.etch(address(oracle), hex'069420'); - -// module = new ForTest_Module(oracle); -// } - -// /** -// * @notice Test that setupRequestData stores the data -// */ -// function test_decodeRequestData(bytes32 _requestId, bytes calldata _data) public { -// // Set the request data -// vm.prank(address(oracle)); -// module.setupRequest(_requestId, _data); - -// // Check: decoded values match original values? -// assertEq(module.requestData(_requestId), _data); -// } - -// /** -// * @notice Test that setupRequestData reverts if the oracle is not the caller -// */ -// function test_setupRequestRevertsWhenCalledByNonOracle(bytes32 _requestId, bytes calldata _data) public { -// vm.expectRevert(abi.encodeWithSelector(IModule.Module_OnlyOracle.selector)); -// // Set the request data -// module.setupRequest(_requestId, _data); -// } - -// /** -// * @notice Test if finalizeRequest can only be called by the oracle -// */ -// function test_finalizeRequestOnlyOracle(bytes32 _requestId, address _caller) public { -// vm.assume(_caller != address(oracle)); - -// // Check: reverts if not called by oracle? -// vm.expectRevert(abi.encodeWithSelector(IModule.Module_OnlyOracle.selector)); -// vm.prank(_caller); -// module.finalizeRequest(_requestId, address(_caller)); - -// // Check: does not revert if called by oracle -// vm.prank(address(oracle)); -// module.finalizeRequest(_requestId, address(oracle)); -// } - -// /** -// * @notice Test that the moduleName function returns the correct name -// */ -// function test_moduleNameReturnsName() public { -// assertEq(module.moduleName(), 'abstractModule'); -// } -// } +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.19; + +import 'forge-std/Test.sol'; + +import {Module, IModule} from '../../../contracts/Module.sol'; +import {IOracle} from '../../../interfaces/IOracle.sol'; +import {Helpers} from '../../utils/Helpers.sol'; + +/** + * @dev Harness to deploy the abstract contract + */ +contract ForTest_Module is Module { + constructor(IOracle _oracle) Module(_oracle) {} + + function moduleName() external pure returns (string memory _moduleName) { + return 'AbstractModule'; + } +} + +/** + * @title Module Abstract Unit tests + */ +contract Module_UnitTest is Test, Helpers { + // The target contract + Module public module; + + // A mock oracle + IOracle public oracle; + + /** + * @notice Deploy the target and mock oracle extension + */ + function setUp() public { + oracle = IOracle(_mockContract('Oracle')); + module = new ForTest_Module(oracle); + } + + /** + * @notice Test if finalizeRequest can only be called by the oracle + */ + function test_finalizeRequest_onlyOracle(address _caller) public { + vm.assume(_caller != address(oracle)); + + // Check: reverts if not called by oracle? + vm.expectRevert(abi.encodeWithSelector(IModule.Module_OnlyOracle.selector)); + vm.prank(_caller); + module.finalizeRequest(mockRequest, mockResponse, _caller); + + // Check: does not revert if called by oracle + vm.prank(address(oracle)); + module.finalizeRequest(mockRequest, mockResponse, address(oracle)); + } + + /** + * @notice Test that the moduleName function returns the correct name + */ + function test_moduleName_returnsName() public { + assertEq(module.moduleName(), 'AbstractModule'); + } +} diff --git a/solidity/test/utils/Helpers.sol b/solidity/test/utils/Helpers.sol index d2d42a8..c7d58bf 100644 --- a/solidity/test/utils/Helpers.sol +++ b/solidity/test/utils/Helpers.sol @@ -61,11 +61,22 @@ contract Helpers is DSTestPlus { _id = keccak256(abi.encode(_dispute)); } + /** + * @notice Creates a mock contract, labels it and erases the bytecode + * + * @param _label The label to use for the mock contract + * @return _contract The address of the mock contract + */ function _mockContract(string memory _label) internal returns (address _contract) { _contract = makeAddr(_label); vm.etch(_contract, hex'69'); } + /** + * @notice Sets an expectation for an event to be emitted + * + * @param _contract The contract to expect the event on + */ function _expectEmit(address _contract) internal { vm.expectEmit(true, true, true, true, _contract); }