Refactor EnumerableSet generation and tests (#4762)

This commit is contained in:
Renan Souza
2023-11-28 23:51:58 +00:00
committed by GitHub
parent 74e396a967
commit c411700572
4 changed files with 60 additions and 93 deletions

View File

@ -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) => {
);
};
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 () {
// 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`,
});
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
// 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)`,
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_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`,
});
});
}
});