-
Notifications
You must be signed in to change notification settings - Fork 0
/
Foo.t.sol
57 lines (48 loc) · 2.29 KB
/
Foo.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19 <0.9.0;
import { PRBTest } from "@prb/test/PRBTest.sol";
import { console2 } from "forge-std/console2.sol";
import { StdCheats } from "forge-std/StdCheats.sol";
import { Foo } from "../src/Foo.sol";
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
}
/// @dev If this is your first time with Forge, read this tutorial in the Foundry Book:
/// https://book.getfoundry.sh/forge/writing-tests
contract FooTest is PRBTest, StdCheats {
Foo internal foo;
/// @dev A function invoked before each test case is run.
function setUp() public virtual {
// Instantiate the contract-under-test.
foo = new Foo();
}
/// @dev Basic test. Run it with `forge test -vvv` to see the console log.
function test_Example() external {
console2.log("Hello World");
uint256 x = 42;
assertEq(foo.id(x), x, "value mismatch");
}
/// @dev Fuzz test that provides random values for an unsigned integer, but which rejects zero as an input.
/// If you need more sophisticated input validation, you should use the `bound` utility instead.
/// See https://twitter.com/PaulRBerg/status/1622558791685242880
function testFuzz_Example(uint256 x) external {
vm.assume(x != 0); // or x = bound(x, 1, 100)
assertEq(foo.id(x), x, "value mismatch");
}
/// @dev Fork test that runs against an Ethereum Mainnet fork. For this to work, you need to set `API_KEY_ALCHEMY`
/// in your environment You can get an API key for free at https://alchemy.com.
function testFork_Example() external {
// Silently pass this test if there is no API key.
string memory alchemyApiKey = vm.envOr("API_KEY_ALCHEMY", string(""));
if (bytes(alchemyApiKey).length == 0) {
return;
}
// Otherwise, run the test against the mainnet fork.
vm.createSelectFork({ urlOrAlias: "mainnet", blockNumber: 16_428_000 });
address usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address holder = 0x7713974908Be4BEd47172370115e8b1219F4A5f0;
uint256 actualBalance = IERC20(usdc).balanceOf(holder);
uint256 expectedBalance = 196_307_713.810457e6;
assertEq(actualBalance, expectedBalance);
}
}