-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWhitelisted.t.sol
71 lines (53 loc) · 1.86 KB
/
Whitelisted.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.10;
import "forge-std/Test.sol";
import {WhitelistedMock} from "./utils/mocks/WhitelistedMock.sol";
/**
* Errors library for Whitelisted's custom errors.
* Enables checking for errors with vm.expectRevert(Errors.<Error>).
*/
library Errors {
bytes internal constant OnlyCallableByWhitelistedAddress
= abi.encodeWithSignature("OnlyCallableByWhitelistedAddress()");
}
contract WhitelistedTest is Test {
// SuT
WhitelistedMock sut;
// Events copied from SuT.
// Note that the Event declarations are needed to test for emission.
event AddressAddedToWhitelist(address indexed who);
event AddressRemovedFromWhitelist(address indexed who);
function setUp() public {
sut = new WhitelistedMock();
}
function testAddToWhitelist(address who) public {
vm.expectEmit(true, true, true, true);
emit AddressAddedToWhitelist(who);
sut.addToWhitelist(who);
// Function should be idempotent.
sut.addToWhitelist(who);
assertTrue(sut.whitelist(who));
}
function testRemoveFromWhitelist(address who) public {
sut.addToWhitelist(who);
vm.expectEmit(true, true, true, true);
emit AddressRemovedFromWhitelist(who);
sut.removeFromWhitelist(who);
// Function should be idempotent.
sut.removeFromWhitelist(who);
assertTrue(!sut.whitelist(who));
}
function testModifierOnlyWhitelisted(address who, bool addToWhitelist)
public
{
if (addToWhitelist) {
sut.addToWhitelist(who);
vm.prank(who);
sut.onlyCallableByWhitelistedAddress();
} else {
vm.prank(who);
vm.expectRevert(Errors.OnlyCallableByWhitelistedAddress);
sut.onlyCallableByWhitelistedAddress();
}
}
}