浏览代码

Refactor EnumerableSet generation and tests (#4762)

Renan Souza 1 年之前
父节点
当前提交
c411700572

+ 1 - 6
scripts/generate/templates/EnumerableSet.js

@@ -1,11 +1,6 @@
 const format = require('../format-lines');
 const { fromBytes32, toBytes32 } = require('./conversion');
-
-const TYPES = [
-  { name: 'Bytes32Set', type: 'bytes32' },
-  { name: 'AddressSet', type: 'address' },
-  { name: 'UintSet', type: 'uint256' },
-];
+const { TYPES } = require('./EnumerableSet.opts');
 
 /* eslint-disable max-len */
 const header = `\

+ 10 - 0
scripts/generate/templates/EnumerableSet.opts.js

@@ -0,0 +1,10 @@
+const mapType = str => (str == 'uint256' ? 'Uint' : `${str.charAt(0).toUpperCase()}${str.slice(1)}`);
+
+const formatType = type => ({
+  name: `${mapType(type)}Set`,
+  type,
+});
+
+const TYPES = ['bytes32', 'address', 'uint256'].map(formatType);
+
+module.exports = { TYPES, formatType };

+ 12 - 6
test/utils/structs/EnumerableSet.behavior.js

@@ -1,6 +1,7 @@
 const { expect } = require('chai');
+const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
 
-function shouldBehaveLikeSet(events) {
+function shouldBehaveLikeSet() {
   async function expectMembersMatch(methods, values) {
     expect(await methods.length()).to.equal(values.length);
     for (const value of values) expect(await methods.contains(value)).to.be.true;
@@ -17,7 +18,7 @@ function shouldBehaveLikeSet(events) {
 
   describe('add', function () {
     it('adds a value', async function () {
-      await expect(this.methods.add(this.valueA)).to.emit(this.mock, events.addReturn).withArgs(true);
+      await expect(this.methods.add(this.valueA)).to.emit(this.mock, this.events.addReturn).withArgs(true);
 
       await expectMembersMatch(this.methods, [this.valueA]);
     });
@@ -33,7 +34,7 @@ function shouldBehaveLikeSet(events) {
     it('returns false when adding values already in the set', async function () {
       await this.methods.add(this.valueA);
 
-      await expect(this.methods.add(this.valueA)).to.emit(this.mock, events.addReturn).withArgs(false);
+      await expect(this.methods.add(this.valueA)).to.emit(this.mock, this.events.addReturn).withArgs(false);
 
       await expectMembersMatch(this.methods, [this.valueA]);
     });
@@ -41,7 +42,12 @@ function shouldBehaveLikeSet(events) {
 
   describe('at', function () {
     it('reverts when retrieving non-existent elements', async function () {
-      await expect(this.methods.at(0)).to.be.reverted;
+      await expect(this.methods.at(0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
+    });
+
+    it('retrieves existing element', async function () {
+      await this.methods.add(this.valueA);
+      expect(await this.methods.at(0)).to.equal(this.valueA);
     });
   });
 
@@ -49,14 +55,14 @@ function shouldBehaveLikeSet(events) {
     it('removes added values', async function () {
       await this.methods.add(this.valueA);
 
-      await expect(this.methods.remove(this.valueA)).to.emit(this.mock, events.removeReturn).withArgs(true);
+      await expect(this.methods.remove(this.valueA)).to.emit(this.mock, this.events.removeReturn).withArgs(true);
 
       expect(await this.methods.contains(this.valueA)).to.be.false;
       await expectMembersMatch(this.methods, []);
     });
 
     it('returns false when removing values not in the set', async function () {
-      await expect(this.methods.remove(this.valueA)).to.emit(this.mock, events.removeReturn).withArgs(false);
+      await expect(this.methods.remove(this.valueA)).to.emit(this.mock, this.events.removeReturn).withArgs(false);
 
       expect(await this.methods.contains(this.valueA)).to.be.false;
     });

+ 36 - 80
test/utils/structs/EnumerableSet.test.js

@@ -2,6 +2,7 @@ const { ethers } = require('hardhat');
 const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
 const { mapValues } = require('../../helpers/iterate');
 const { randomArray, generators } = require('../../helpers/random');
+const { TYPES } = require('../../../scripts/generate/templates/EnumerableSet.opts');
 
 const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
 
@@ -14,91 +15,46 @@ const getMethods = (mock, fnSigs) => {
   );
 };
 
-describe('EnumerableSet', function () {
-  // Bytes32Set
-  describe('EnumerableBytes32Set', function () {
-    const fixture = async () => {
-      const mock = await ethers.deployContract('$EnumerableSet');
-
-      const [valueA, valueB, valueC] = randomArray(generators.bytes32);
-
-      const methods = getMethods(mock, {
-        add: '$add(uint256,bytes32)',
-        remove: '$remove(uint256,bytes32)',
-        contains: '$contains(uint256,bytes32)',
-        length: `$length_EnumerableSet_Bytes32Set(uint256)`,
-        at: `$at_EnumerableSet_Bytes32Set(uint256,uint256)`,
-        values: `$values_EnumerableSet_Bytes32Set(uint256)`,
-      });
-
-      return { mock, valueA, valueB, valueC, methods };
-    };
-
-    beforeEach(async function () {
-      Object.assign(this, await loadFixture(fixture));
-    });
-
-    shouldBehaveLikeSet({
-      addReturn: `return$add_EnumerableSet_Bytes32Set_bytes32`,
-      removeReturn: `return$remove_EnumerableSet_Bytes32Set_bytes32`,
-    });
-  });
-
-  // AddressSet
-  describe('EnumerableAddressSet', function () {
-    const fixture = async () => {
-      const mock = await ethers.deployContract('$EnumerableSet');
-
-      const [valueA, valueB, valueC] = randomArray(generators.address);
-
-      const methods = getMethods(mock, {
-        add: '$add(uint256,address)',
-        remove: '$remove(uint256,address)',
-        contains: '$contains(uint256,address)',
-        length: `$length_EnumerableSet_AddressSet(uint256)`,
-        at: `$at_EnumerableSet_AddressSet(uint256,uint256)`,
-        values: `$values_EnumerableSet_AddressSet(uint256)`,
-      });
-
-      return { mock, valueA, valueB, valueC, methods };
-    };
+async function fixture() {
+  const mock = await ethers.deployContract('$EnumerableSet');
+
+  const env = Object.fromEntries(
+    TYPES.map(({ name, type }) => [
+      type,
+      {
+        values: randomArray(generators[type]),
+        methods: getMethods(mock, {
+          add: `$add(uint256,${type})`,
+          remove: `$remove(uint256,${type})`,
+          contains: `$contains(uint256,${type})`,
+          length: `$length_EnumerableSet_${name}(uint256)`,
+          at: `$at_EnumerableSet_${name}(uint256,uint256)`,
+          values: `$values_EnumerableSet_${name}(uint256)`,
+        }),
+        events: {
+          addReturn: `return$add_EnumerableSet_${name}_${type}`,
+          removeReturn: `return$remove_EnumerableSet_${name}_${type}`,
+        },
+      },
+    ]),
+  );
 
-    beforeEach(async function () {
-      Object.assign(this, await loadFixture(fixture));
-    });
+  return { mock, env };
+}
 
-    shouldBehaveLikeSet({
-      addReturn: `return$add_EnumerableSet_AddressSet_address`,
-      removeReturn: `return$remove_EnumerableSet_AddressSet_address`,
-    });
+describe('EnumerableSet', function () {
+  beforeEach(async function () {
+    Object.assign(this, await loadFixture(fixture));
   });
 
-  // UintSet
-  describe('EnumerableUintSet', function () {
-    const fixture = async () => {
-      const mock = await ethers.deployContract('$EnumerableSet');
-
-      const [valueA, valueB, valueC] = randomArray(generators.uint256);
-
-      const methods = getMethods(mock, {
-        add: '$add(uint256,uint256)',
-        remove: '$remove(uint256,uint256)',
-        contains: '$contains(uint256,uint256)',
-        length: `$length_EnumerableSet_UintSet(uint256)`,
-        at: `$at_EnumerableSet_UintSet(uint256,uint256)`,
-        values: `$values_EnumerableSet_UintSet(uint256)`,
+  for (const { type } of TYPES) {
+    describe(type, function () {
+      beforeEach(function () {
+        Object.assign(this, this.env[type]);
+        [this.valueA, this.valueB, this.valueC] = this.values;
       });
 
-      return { mock, valueA, valueB, valueC, methods };
-    };
-
-    beforeEach(async function () {
-      Object.assign(this, await loadFixture(fixture));
+      shouldBehaveLikeSet();
     });
-
-    shouldBehaveLikeSet({
-      addReturn: `return$add_EnumerableSet_UintSet_uint256`,
-      removeReturn: `return$remove_EnumerableSet_UintSet_uint256`,
-    });
-  });
+  }
 });