Refactor EnumerableSet generation and tests (#4762)
This commit is contained in:
@ -1,11 +1,6 @@
|
|||||||
const format = require('../format-lines');
|
const format = require('../format-lines');
|
||||||
const { fromBytes32, toBytes32 } = require('./conversion');
|
const { fromBytes32, toBytes32 } = require('./conversion');
|
||||||
|
const { TYPES } = require('./EnumerableSet.opts');
|
||||||
const TYPES = [
|
|
||||||
{ name: 'Bytes32Set', type: 'bytes32' },
|
|
||||||
{ name: 'AddressSet', type: 'address' },
|
|
||||||
{ name: 'UintSet', type: 'uint256' },
|
|
||||||
];
|
|
||||||
|
|
||||||
/* eslint-disable max-len */
|
/* eslint-disable max-len */
|
||||||
const header = `\
|
const header = `\
|
||||||
|
|||||||
10
scripts/generate/templates/EnumerableSet.opts.js
Normal file
10
scripts/generate/templates/EnumerableSet.opts.js
Normal file
@ -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 };
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const { expect } = require('chai');
|
const { expect } = require('chai');
|
||||||
|
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
|
||||||
|
|
||||||
function shouldBehaveLikeSet(events) {
|
function shouldBehaveLikeSet() {
|
||||||
async function expectMembersMatch(methods, values) {
|
async function expectMembersMatch(methods, values) {
|
||||||
expect(await methods.length()).to.equal(values.length);
|
expect(await methods.length()).to.equal(values.length);
|
||||||
for (const value of values) expect(await methods.contains(value)).to.be.true;
|
for (const value of values) expect(await methods.contains(value)).to.be.true;
|
||||||
@ -17,7 +18,7 @@ function shouldBehaveLikeSet(events) {
|
|||||||
|
|
||||||
describe('add', function () {
|
describe('add', function () {
|
||||||
it('adds a value', async 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]);
|
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 () {
|
it('returns false when adding values already in the set', async function () {
|
||||||
await this.methods.add(this.valueA);
|
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]);
|
await expectMembersMatch(this.methods, [this.valueA]);
|
||||||
});
|
});
|
||||||
@ -41,7 +42,12 @@ function shouldBehaveLikeSet(events) {
|
|||||||
|
|
||||||
describe('at', function () {
|
describe('at', function () {
|
||||||
it('reverts when retrieving non-existent elements', async 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 () {
|
it('removes added values', async function () {
|
||||||
await this.methods.add(this.valueA);
|
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;
|
expect(await this.methods.contains(this.valueA)).to.be.false;
|
||||||
await expectMembersMatch(this.methods, []);
|
await expectMembersMatch(this.methods, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns false when removing values not in the set', async function () {
|
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;
|
expect(await this.methods.contains(this.valueA)).to.be.false;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -2,6 +2,7 @@ const { ethers } = require('hardhat');
|
|||||||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
|
||||||
const { mapValues } = require('../../helpers/iterate');
|
const { mapValues } = require('../../helpers/iterate');
|
||||||
const { randomArray, generators } = require('../../helpers/random');
|
const { randomArray, generators } = require('../../helpers/random');
|
||||||
|
const { TYPES } = require('../../../scripts/generate/templates/EnumerableSet.opts');
|
||||||
|
|
||||||
const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
|
const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
|
||||||
|
|
||||||
@ -14,91 +15,46 @@ const getMethods = (mock, fnSigs) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
return { mock, env };
|
||||||
|
}
|
||||||
|
|
||||||
describe('EnumerableSet', function () {
|
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 () {
|
beforeEach(async function () {
|
||||||
Object.assign(this, await loadFixture(fixture));
|
Object.assign(this, await loadFixture(fixture));
|
||||||
});
|
});
|
||||||
|
|
||||||
shouldBehaveLikeSet({
|
for (const { type } of TYPES) {
|
||||||
addReturn: `return$add_EnumerableSet_Bytes32Set_bytes32`,
|
describe(type, function () {
|
||||||
removeReturn: `return$remove_EnumerableSet_Bytes32Set_bytes32`,
|
beforeEach(function () {
|
||||||
});
|
Object.assign(this, this.env[type]);
|
||||||
|
[this.valueA, this.valueB, this.valueC] = this.values;
|
||||||
});
|
});
|
||||||
|
|
||||||
// AddressSet
|
shouldBehaveLikeSet();
|
||||||
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 };
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async function () {
|
|
||||||
Object.assign(this, await loadFixture(fixture));
|
|
||||||
});
|
|
||||||
|
|
||||||
shouldBehaveLikeSet({
|
|
||||||
addReturn: `return$add_EnumerableSet_AddressSet_address`,
|
|
||||||
removeReturn: `return$remove_EnumerableSet_AddressSet_address`,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// 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)`,
|
|
||||||
});
|
|
||||||
|
|
||||||
return { mock, valueA, valueB, valueC, methods };
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async function () {
|
|
||||||
Object.assign(this, await loadFixture(fixture));
|
|
||||||
});
|
|
||||||
|
|
||||||
shouldBehaveLikeSet({
|
|
||||||
addReturn: `return$add_EnumerableSet_UintSet_uint256`,
|
|
||||||
removeReturn: `return$remove_EnumerableSet_UintSet_uint256`,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user