Skip to content

Commit

Permalink
refactoring test and renaming addVoter to addVoters
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehammond committed Feb 8, 2025
1 parent 06e8c3e commit dda34ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions blockchain/contracts/Election.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract Election {
string image;
}
struct Voter {
bool registered;
bool voted;
uint candidateId;
}
Expand Down Expand Up @@ -86,14 +87,14 @@ contract Election {
return allCandidates;
}

function addVoter(address[] memory _voterAddresses) public onlyWhileOpen {
function addVoters(address[] memory _voterAddresses) public onlyWhileOpen {
uint length = _voterAddresses.length;
for (uint i = 0; i < length; i++) {
require(
!voters[_voterAddresses[i]].voted,
"Voter is already registered"
);
voters[_voterAddresses[i]] = Voter(false, 0);
voters[_voterAddresses[i]] = Voter(true, false, 0);
}
}

Expand Down
12 changes: 6 additions & 6 deletions blockchain/test/Election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("Election Contract", function () {
await hre.ethers.provider.send("evm_mine", []);

await expect(
election.addCandidate("Invalid", "Team X", "invalid.jpg")
election.addCandidate("Invalid", "Team X", "invalid.jpg")
).to.be.revertedWithCustomError(election, "ElectionEnded");
});

Expand Down Expand Up @@ -107,7 +107,7 @@ describe("Election Contract", function () {
await hre.ethers.provider.send("evm_mine", []);

await election.addCandidate("Alice", "Team A", "alice.jpg");
await election.addVoter(voter1.address);
await election.addVoters([voter1.address]);

await expect(election.connect(voter1).castVote(999))
.to.be.revertedWith("Invalid candidate. Please enter a valid candidate ID");
Expand All @@ -124,7 +124,7 @@ describe("Election Contract", function () {
await hre.ethers.provider.send("evm_mine", []);

await election.addCandidate("Alice", "Team A", "alice.jpg");
await election.addVoter(voter1.address);
await election.addVoters([voter1.address]);

// Cast the first vote
await election.connect(voter1).castVote(1);
Expand All @@ -144,15 +144,15 @@ describe("Election Contract", function () {
await hre.ethers.provider.send("evm_increaseTime", [timeToAdvance]);
await hre.ethers.provider.send("evm_mine", []);

await expect(election.addVoter(voter1.address))
await expect(election.addVoters([voter1.address]))
.to.be.revertedWithCustomError(election, "ElectionEnded");
});

it("should prevent adding a voter before election starts", async function () {
const { election, startDate, voter1 } = await loadFixture(deployElectionFixture);

// Attempt to add a voter before the election starts
await expect(election.addVoter(voter1.address))
await expect(election.addVoters([voter1.address]))
.to.be.revertedWithCustomError(election, "ElectionNotStarted");
});

Expand All @@ -168,7 +168,7 @@ describe("Election Contract", function () {

// Add a candidate and a voter
await election.addCandidate("Alice", "Team A", "alice.jpg");
await election.addVoter(voter1.address);
await election.addVoters([voter1.address]);

// Cast a vote
await election.connect(voter1).castVote(1);
Expand Down

0 comments on commit dda34ba

Please sign in to comment.