Skip to content

Commit

Permalink
unit test groundwork
Browse files Browse the repository at this point in the history
  • Loading branch information
amiecorso committed Oct 8, 2024
1 parent d648a6e commit 579ec73
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 8 deletions.
29 changes: 29 additions & 0 deletions test/base/SpendPermissionsBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Test, console2} from "forge-std/Test.sol";

import {SpendPermissions} from "../../src/SpendPermissions.sol";

import {MockSpendPermissions} from "../mocks/MockSpendPermissions.sol";

contract SpendPermissionsBase {
MockSpendPermissions mockSpendPermissions;

function _initializeSpendPermissions() internal {
mockSpendPermissions = new MockSpendPermissions();
}

function _createRecurringAllowance(uint48 start, uint48 period, uint160 allowance)
internal
pure
returns (SpendPermissions.RecurringAllowance memory)
{
return SpendPermissions.RecurringAllowance(start, period, allowance);
}

function _safeAdd(uint48 a, uint48 b) internal pure returns (uint48 c) {
bool overflow = uint256(a) + uint256(b) > type(uint48).max;
return overflow ? type(uint48).max : a + b;
}
}
21 changes: 21 additions & 0 deletions test/mocks/MockSpendPermissions.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {SpendPermissions} from "../../src/SpendPermissions.sol";

contract MockSpendPermissions is SpendPermissions {

// TODO: what other internal functions should be exposed?

// function initializeRecurringAllowance(
// address account,
// bytes32 permissionHash,
// RecurringAllowance memory recurringAlowance
// ) public {
// _initializeRecurringAllowance(account, permissionHash, recurringAlowance);
// }

function useRecurringAllowance(RecurringAllowance memory recurringAllowance, uint256 value) public {
_useRecurringAllowance(recurringAllowance, value);
}
}
16 changes: 8 additions & 8 deletions test/src/SpendPermissions/Debug.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ contract DebugTest is Test, Base {
spendPermissions.approve(recurringAllowance);
}

function test_withdraw(address recipient) public {
SpendPermissions.RecurringAllowance memory recurringAllowance = _createRecurringAllowance();
// function test_withdraw(address recipient) public {
// SpendPermissions.RecurringAllowance memory recurringAllowance = _createRecurringAllowance();

vm.prank(address(account));
spendPermissions.approve(recurringAllowance);
// vm.prank(address(account));
// spendPermissions.approve(recurringAllowance);

vm.deal(address(account), 1 ether);
vm.prank(owner);
spendPermissions.withdraw(recurringAllowance, recipient, 1 ether / 2);
}
// vm.deal(address(account), 1 ether);
// vm.prank(owner);
// spendPermissions.withdraw(recurringAllowance, recipient, 1 ether / 2);
// }

function _createRecurringAllowance() internal view returns (SpendPermissions.RecurringAllowance memory) {
return SpendPermissions.RecurringAllowance({
Expand Down
17 changes: 17 additions & 0 deletions test/src/SpendPermissions/approve.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Test, console2} from "forge-std/Test.sol";

import {SpendPermissions} from "../../../src/SpendPermissions.sol";

import {SpendPermissionsBase} from "../../base/SpendPermissionsBase.sol";

contract ApproveTest is Test, SpendPermissionsBase {
function setUp() public {
_initializeSpendPermissions();
}

function test_approve_revert_invalidSender() public {}
function test_approve_success() public {}
}
20 changes: 20 additions & 0 deletions test/src/SpendPermissions/getCurrentCycle.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Test, console2} from "forge-std/Test.sol";

import {SpendPermissions} from "../../../src/SpendPermissions.sol";

import {SpendPermissionsBase} from "../../base/SpendPermissionsBase.sol";

contract GetCurrentCycleTest is Test, SpendPermissionsBase {
function setUp() public {
_initializeSpendPermissions();
}

function test_getCurrentCycle_revert_beforeRecurringAllowanceStart() public {}
function test_getCurrentCycle_revert_afterRecurringAllowanceEnd() public {}
function test_getCurrentCycle_success_lastCycleStillActive() public {}
function test_getCurrentCycle_success_determineCurrentCycle() public {}

}
16 changes: 16 additions & 0 deletions test/src/SpendPermissions/getHash.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Test, console2} from "forge-std/Test.sol";

import {SpendPermissions} from "../../../src/SpendPermissions.sol";

import {SpendPermissionsBase} from "../../base/SpendPermissionsBase.sol";

contract GetHashTest is Test, SpendPermissionsBase {
function setUp() public {
_initializeSpendPermissions();
}

function test_getHash_success() public {}
}
Empty file.
Empty file.
Empty file.
25 changes: 25 additions & 0 deletions test/src/SpendPermissions/useRecurringAllowance.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Test, console2} from "forge-std/Test.sol";

import {SpendPermissions} from "../../../src/SpendPermissions.sol";

import {SpendPermissionsBase} from "../../base/SpendPermissionsBase.sol";

contract UseRecurringAllowanceTest is Test, SpendPermissionsBase {
function setUp() public {
_initializeSpendPermissions();
}
function test_useRecurringAllowance_success_noValue() public {}
function test_useRecurringAllowance_revert_unauthorizedRecurringAllowance()
public
{}
function test_useRecurringAllowance_revert_qithdrawValueOverflow() public {}
function test_useRecurringAllowance_revert_exceededRecurringAllowance()
public
{}
// TODO what are the various success logical cases?
function test_userRecurringAllowance_success() public {}

}
Empty file.

0 comments on commit 579ec73

Please sign in to comment.