Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix module unit tests #9

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 61 additions & 83 deletions solidity/test/unit/modules/Module.t.sol
Original file line number Diff line number Diff line change
@@ -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');
}
}
11 changes: 11 additions & 0 deletions solidity/test/utils/Helpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down