Les test unitaires de la fonction deposit() de la contract Penduel sont les suivants :
const Penduel = artifacts.require("Penduel");
const { BN, expectRevert, expectEvent } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const Web3 = require('web3');
contract("Penduel", accounts => {
let penduelInstance;
const subId = 12226;
const player1 = accounts[0];
const player2 = accounts[1];
const s_owner = accounts[2];
const web3 = new Web3("http://localhost:7545");
context("FONCTION DE MISE EN PLACE DU JEU PLAYER 1 - fonction createGame", () => {
beforeEach(async function() {
penduelInstance = await Penduel.new(subId);
});
it("should allow player1 to deposit bet", async () => {
const gameStateBefore = await penduelInstance.getGameState();
const player1BalanceBefore = await web3.eth.getBalance(player1);
const player1BetAmount = web3.utils.toWei("1", "ether");
const result = await penduelInstance.deposit({ from: player1, value: player1BetAmount });
const gameStateAfter = await penduelInstance.getGameState();
const player1BalanceAfter = await web3.eth.getBalance(player1);
expect(gameStateBefore).to.be.equal(0); // Assuming 0 is the initial game state
expect(gameStateAfter).to.be.equal(1); // Assuming 1 is the state after player1's deposit
expect(player1BalanceBefore - player1BalanceAfter).to.be.equal(player1BetAmount); // Assuming player1's balance decreased by the bet amount
expectEvent(result, "BetDeposited", {
player: player1,
betAmount: player1BetAmount,
state: 1 // Assuming 1 is the state after player1's deposit
});
});
it("should revert if an invalid address tries to deposit bet", async () => {
const invalidPlayer = accounts[3];
const player1BetAmount = web3.utils.toWei("1", "ether");
await expectRevert(
penduelInstance.deposit({ from: invalidPlayer, value: player1BetAmount }),
"invalid address"
);
});
it("should revert if the game state is not valid for deposit", async () => {
const player1BetAmount = web3.utils.toWei("1", "ether");
await penduelInstance.deposit({ from: player1, value: player1BetAmount });
await penduelInstance.deposit({ from: player2, value: player1BetAmount });
await expectRevert(
penduelInstance.deposit({ from: player2, value: player1BetAmount }),
"invalid game state"
);
});
it("should allow player2 to deposit bet and match player1's bet", async () => {
const player1BetAmount = web3.utils.toWei("1", "ether");
const player2BetAmount = web3.utils.toWei("1", "ether");
await penduelInstance.deposit({ from: player1, value: player1BetAmount });
const result = await penduelInstance.deposit({ from: player2, value: player2BetAmount });
expectEvent(result, "BetDeposited", {
player: player2,
betAmount: player2BetAmount,
state: 2 // Assuming 2 is the state after player2's deposit
});
});
it("should revert if player2 tries to deposit bet multiple times", async () => {
const player1BetAmount = web3.utils.toWei("1", "ether");
const player2BetAmount = web3.utils.toWei("1", "ether");
await penduelInstance.deposit({ from: player1, value: player1BetAmount });
await penduelInstance.deposit({ from: player2, value: player2BetAmount });
await expectRevert(
penduelInstance.deposit({ from: player2, value: player2BetAmount }),
"player2 has already bet"
);
});
it("should revert if player2 tries to deposit bet with different amount than player1's bet", async () => {
const player1BetAmount = web3.utils.toWei("1", "ether");
const player2BetAmount = web3.utils.toWei("2", "ether");
await penduelInstance.deposit({ from: player1, value: player1BetAmount });
await expectRevert(
penduelInstance.deposit({ from: player2, value: player2BetAmount }),
"bets the same amount as Player1"
);
});
});
});
Ces tests couvrent les scénarios attendus pour la fonction deposit() de la contract Penduel.