-
Notifications
You must be signed in to change notification settings - Fork 174
/
SystemHook.t.sol
52 lines (41 loc) · 1.97 KB
/
SystemHook.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;
import "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Hook, HookLib } from "@latticexyz/store/src/Hook.sol";
import { BEFORE_CALL_SYSTEM, AFTER_CALL_SYSTEM, ALL } from "../src/systemHookTypes.sol";
import { ISystemHook } from "../src/ISystemHook.sol";
contract SystemHookTest is Test, GasReporter {
function testFuzzEncode(address hookAddress, bool enableBeforeCallSystem, bool enableAfterCallSystem) public {
uint8 enabledHooksBitmap = 0;
if (enableBeforeCallSystem) enabledHooksBitmap |= BEFORE_CALL_SYSTEM;
if (enableAfterCallSystem) enabledHooksBitmap |= AFTER_CALL_SYSTEM;
assertEq(
Hook.unwrap(HookLib.encode(hookAddress, enabledHooksBitmap)),
bytes21(abi.encodePacked(hookAddress, enabledHooksBitmap))
);
}
function testFuzzIsEnabled(address hookAddress, bool enableBeforeCallSystem, bool enableAfterCallSystem) public {
uint8 enabledHooksBitmap = 0;
if (enableBeforeCallSystem) enabledHooksBitmap |= BEFORE_CALL_SYSTEM;
if (enableAfterCallSystem) enabledHooksBitmap |= AFTER_CALL_SYSTEM;
Hook systemHook = HookLib.encode(hookAddress, enabledHooksBitmap);
assertEq(systemHook.isEnabled(BEFORE_CALL_SYSTEM), enableBeforeCallSystem);
assertEq(systemHook.isEnabled(AFTER_CALL_SYSTEM), enableAfterCallSystem);
}
function testFuzzGetAddressAndBitmap(
address hookAddress,
bool enableBeforeCallSystem,
bool enableAfterCallSystem
) public {
uint8 enabledHooksBitmap = 0;
if (enableBeforeCallSystem) enabledHooksBitmap |= BEFORE_CALL_SYSTEM;
if (enableAfterCallSystem) enabledHooksBitmap |= AFTER_CALL_SYSTEM;
Hook systemHook = HookLib.encode(hookAddress, enabledHooksBitmap);
assertEq(systemHook.getAddress(), hookAddress);
assertEq(systemHook.getBitmap(), enabledHooksBitmap);
}
function testShorthand() public {
assertEq(ALL, BEFORE_CALL_SYSTEM | AFTER_CALL_SYSTEM);
}
}