Verify than secondsPerPeriod cannot be zero (#211)

* Assert than secondsPerPeriod cannot be zero
* Apply custom error
This commit is contained in:
Arnaud 2025-01-24 09:22:21 +01:00 committed by GitHub
parent 02e3b8d22b
commit bfa5a78b4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View File

@ -2,11 +2,16 @@
pragma solidity 0.8.23;
contract Periods {
error Periods_InvalidSecondsPerPeriod();
type Period is uint256;
uint256 internal immutable _secondsPerPeriod;
constructor(uint256 secondsPerPeriod) {
if (secondsPerPeriod == 0) {
revert Periods_InvalidSecondsPerPeriod();
}
_secondsPerPeriod = secondsPerPeriod;
}

16
test/Periods.test.js Normal file
View File

@ -0,0 +1,16 @@
const { expect } = require("chai")
const { ethers } = require("hardhat")
describe("Periods", function () {
it("should revert when secondsPerPeriod is 0", async function () {
const PeriodsContract = await ethers.getContractFactory("Periods")
await expect(PeriodsContract.deploy(0)).to.be.revertedWith(
"Periods_InvalidSecondsPerPeriod"
)
})
it("should not revert when secondsPerPeriod more than 0", async function () {
const PeriodsContract = await ethers.getContractFactory("Periods")
await expect(PeriodsContract.deploy(10)).not.to.be.reverted
})
})