sourcebridge_42-contract-lab/test/VotingContract.t.sol
sourcebridge_42 1117f62b2c
Some checks are pending
CI / build-and-test (push) Waiting to run
CI / slither (push) Waiting to run
test: add foundry tests for VotingContract.sol
2026-04-19 09:08:18 +00:00

115 lines
3.9 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "forge-std/Test.sol";
import "../contracts/VotingContract.sol";
contract VotingContractTest is Test {
VotingContract votingContract;
address proposer;
address voter1;
address voter2;
function setUp() public {
proposer = makeAddr("proposer");
voter1 = makeAddr("voter1");
voter2 = makeAddr("voter2");
vm.prank(proposer);
votingContract = new VotingContract{value: 200 ether}();
}
function test_CreateProposal() public {
vm.startPrank(proposer);
vm.deal(proposer, 1000 ether);
uint256 proposalId = votingContract.createProposal{value: 100 ether}("Test Proposal");
(address returnedProposer, string memory description, , , ) = votingContract.getProposalDetails(proposalId);
assertEq(returnedProposer, proposer, "Incorrect proposal creator");
assertEq(description, "Test Proposal", "Incorrect proposal description");
vm.stopPrank();
}
function testRevert_CreateProposalInsufficientStake() public {
vm.expectRevert("Insufficient proposal stake");
votingContract.createProposal{value: 50 ether}("Underfunded Proposal");
}
function test_CastVote() public {
vm.startPrank(proposer);
vm.deal(proposer, 1000 ether);
uint256 proposalId = votingContract.createProposal{value: 100 ether}("Voting Test");
vm.stopPrank();
vm.prank(voter1);
votingContract.castVote(proposalId, true);
(, , uint256 votesFor, , ) = votingContract.getProposalDetails(proposalId);
assertEq(votesFor, 1, "Vote not recorded correctly");
}
function testRevert_VoteOutsideVotingPeriod() public {
vm.startPrank(proposer);
vm.deal(proposer, 1000 ether);
uint256 proposalId = votingContract.createProposal{value: 100 ether}("Voting Test");
vm.stopPrank();
vm.warp(block.timestamp + 8 days);
vm.expectRevert("Voting is not active for this proposal");
vm.prank(voter1);
votingContract.castVote(proposalId, true);
}
function testRevert_DuplicateVote() public {
vm.startPrank(proposer);
vm.deal(proposer, 1000 ether);
uint256 proposalId = votingContract.createProposal{value: 100 ether}("Voting Test");
vm.stopPrank();
vm.startPrank(voter1);
votingContract.castVote(proposalId, true);
vm.expectRevert("Already voted");
votingContract.castVote(proposalId, false);
vm.stopPrank();
}
function test_FinalizeProposal() public {
vm.startPrank(proposer);
vm.deal(proposer, 1000 ether);
uint256 proposalId = votingContract.createProposal{value: 100 ether}("Voting Test");
vm.stopPrank();
vm.prank(voter1);
votingContract.castVote(proposalId, true);
vm.warp(block.timestamp + 8 days);
votingContract.finalizeProposal(proposalId);
(, , , , VotingContract.ProposalStatus status) = votingContract.getProposalDetails(proposalId);
assertEq(uint256(status), uint256(VotingContract.ProposalStatus.Passed), "Proposal not finalized correctly");
}
function testFuzz_CreateMultipleProposals(uint8 proposalCount) public {
vm.startPrank(proposer);
vm.deal(proposer, 1000 ether);
for (uint8 i = 0; i < proposalCount; i++) {
votingContract.createProposal{value: 100 ether}(string(abi.encodePacked("Proposal ", vm.toString(i))));
}
assertEq(votingContract.proposalCount(), proposalCount, "Incorrect number of proposals created");
vm.stopPrank();
}
function testGas_CreateProposal() public {
vm.startPrank(proposer);
vm.deal(proposer, 1000 ether);
votingContract.createProposal{value: 100 ether}("Gas Benchmark Proposal");
vm.stopPrank();
}
}