Panic.test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  5. async function fixture() {
  6. return { mock: await ethers.deployContract('$Panic') };
  7. }
  8. describe('Panic', function () {
  9. beforeEach(async function () {
  10. Object.assign(this, await loadFixture(fixture));
  11. });
  12. for (const [name, code] of Object.entries({
  13. GENERIC: 0x0,
  14. ASSERT: PANIC_CODES.ASSERTION_ERROR,
  15. UNDER_OVERFLOW: PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
  16. DIVISION_BY_ZERO: PANIC_CODES.DIVISION_BY_ZERO,
  17. ENUM_CONVERSION_ERROR: PANIC_CODES.ENUM_CONVERSION_OUT_OF_BOUNDS,
  18. STORAGE_ENCODING_ERROR: PANIC_CODES.INCORRECTLY_ENCODED_STORAGE_BYTE_ARRAY,
  19. EMPTY_ARRAY_POP: PANIC_CODES.POP_ON_EMPTY_ARRAY,
  20. ARRAY_OUT_OF_BOUNDS: PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS,
  21. RESOURCE_ERROR: PANIC_CODES.TOO_MUCH_MEMORY_ALLOCATED,
  22. INVALID_INTERNAL_FUNCTION: PANIC_CODES.ZERO_INITIALIZED_VARIABLE,
  23. })) {
  24. describe(`${name} (${ethers.toBeHex(code)})`, function () {
  25. it('exposes panic code as constant', async function () {
  26. expect(await this.mock.getFunction(`$${name}`)()).to.equal(code);
  27. });
  28. it('reverts with panic when called', async function () {
  29. await expect(this.mock.$panic(code)).to.be.revertedWithPanic(code);
  30. });
  31. });
  32. }
  33. });