-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFundMe.t.sol
More file actions
138 lines (107 loc) · 4.19 KB
/
FundMe.t.sol
File metadata and controls
138 lines (107 loc) · 4.19 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {Test} from "forge-std/Test.sol";
import {FundMe} from "../../src/FundMe.sol";
import {DeployFundMe} from "../../script/DeployFundMe.s.sol";
contract FundMeTest is Test {
FundMe fundMe;
address USER = makeAddr("Abhay");
uint256 constant SEND_AMOUNT = 0.1 ether;
uint256 constant STARTING_BALANCE = 10 ether;
function setUp() external {
fundMe = new FundMe(0x694AA1769357215DE4FAC081bf1f309aDC325306);
DeployFundMe deployFundMe = new DeployFundMe();
fundMe = deployFundMe.run();
vm.deal(USER, STARTING_BALANCE);
}
function testMinimumUSDisFive() public view {
assertEq(fundMe.MINIMUM_USD(), 5e18);
}
function testOwnerIsMsgSender() public view {
assertEq(fundMe.i_owner(), msg.sender);
}
function testPriceFeedVersionIsAccurate() public {
uint256 version = fundMe.getVersion();
assertEq(version, 4);
}
function testFundAmountMoreThanRequiredEth() public {
vm.expectRevert();
fundMe.fund();
}
function testFundAmountIsMorethanEqualRequiredAmount() public funded {
// vm.prank(USER); //set msg.sender - The next trans send by the USER
// fundMe.fund{value: SEND_AMOUNT}();
uint256 getAmountFunded = fundMe.s_addressToAmountFunded(USER);
assertEq(getAmountFunded, SEND_AMOUNT);
}
modifier funded() {
vm.prank(USER);
fundMe.fund{value: SEND_AMOUNT}();
_;
}
function testAddsFundersToTheFundersArray() public funded {
// vm.prank(USER);
// fundMe.fund{value: SEND_AMOUNT}();
address funder = fundMe.s_funders(0);
assertEq(funder, USER);
}
function testFundWithraw() public funded {
// vm.prank(USER);
// fundMe.fund{value: SEND_AMOUNT}();
vm.expectRevert();
fundMe.withdraw();
}
function testWithdrawASingleFunderBalance() public funded {
//Arrange
uint256 StartingOwnerBal = fundMe.i_owner().balance;
uint256 StratingFundedBal = address(fundMe).balance;
//Act
vm.prank(fundMe.i_owner()); // 200
fundMe.withdraw();
//console.log(gasUsed); // 200
//assert
uint256 EndingOwnerBal = fundMe.i_owner().balance;
uint256 EndingFundedBal = address(fundMe).balance;
assertEq(EndingFundedBal, 0);
assertEq(StratingFundedBal + StartingOwnerBal, EndingOwnerBal);
}
function testWithdrawASingleFunderBalanceCheaper() public funded {
//Arrangec
uint256 StartingOwnerBal = fundMe.i_owner().balance;
uint256 StratingFundedBal = address(fundMe).balance;
//Act
vm.prank(fundMe.i_owner()); // 200
fundMe.cheaperWithdraw();
//console.log(gasUsed); // 200
//assert
uint256 EndingOwnerBal = fundMe.i_owner().balance;
uint256 EndingFundedBal = address(fundMe).balance;
assertEq(EndingFundedBal, 0);
assertEq(StratingFundedBal + StartingOwnerBal, EndingOwnerBal);
}
function testMultipleFundersWithdarw() public funded {
//arrange
uint160 numberoffunders = 10;
uint160 startingFunderIndex = 2;
for (uint160 i = startingFunderIndex; i < numberoffunders; i++) {
//vm.prank() new address
//vm.deal() send value to new address
//hoax ----> combination of prank and deal --- set new address with some in-build ether
hoax(address(i), SEND_AMOUNT);
fundMe.fund{value: SEND_AMOUNT}();
}
uint256 StartingOwnerBal = fundMe.i_owner().balance;
uint256 StratingFundedBal = address(fundMe).balance;
//act
vm.startPrank(fundMe.i_owner());
fundMe.withdraw();
vm.stopPrank();
//assert
assert(address(fundMe).balance == 0);
assert(startingFunderIndex + StartingOwnerBal == fundMe.i_owner().balance);
}
}
// what can we do to work with address out side from our system?
// 1. Unit test ---> test a specefic part of the code.
// 2. Integration --> test how out code works with different part of the code.
// 3. Forked --> test code in real enviroment.