EnumerableSet.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { ethers } = require('hardhat');
  2. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  3. const { mapValues } = require('../../helpers/iterate');
  4. const { generators } = require('../../helpers/random');
  5. const { TYPES } = require('../../../scripts/generate/templates/EnumerableSet.opts');
  6. const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
  7. const getMethods = (mock, fnSigs) => {
  8. return mapValues(
  9. fnSigs,
  10. fnSig =>
  11. (...args) =>
  12. mock.getFunction(fnSig)(0, ...args),
  13. );
  14. };
  15. async function fixture() {
  16. const mock = await ethers.deployContract('$EnumerableSet');
  17. const env = Object.fromEntries(
  18. TYPES.map(({ name, type, base, size }) => [
  19. type,
  20. {
  21. values: Array.from(
  22. { length: 3 },
  23. size ? () => Array.from({ length: size }, generators[base]) : generators[type],
  24. ),
  25. methods: getMethods(mock, {
  26. add: `$add(uint256,${type})`,
  27. remove: `$remove(uint256,${type})`,
  28. contains: `$contains(uint256,${type})`,
  29. length: `$length_EnumerableSet_${name}(uint256)`,
  30. at: `$at_EnumerableSet_${name}(uint256,uint256)`,
  31. values: `$values_EnumerableSet_${name}(uint256)`,
  32. }),
  33. events: {
  34. addReturn: `return$add_EnumerableSet_${name}_${type.replace(/[[\]]/g, '_')}`,
  35. removeReturn: `return$remove_EnumerableSet_${name}_${type.replace(/[[\]]/g, '_')}`,
  36. },
  37. },
  38. ]),
  39. );
  40. return { mock, env };
  41. }
  42. describe('EnumerableSet', function () {
  43. beforeEach(async function () {
  44. Object.assign(this, await loadFixture(fixture));
  45. });
  46. for (const { type } of TYPES) {
  47. describe(type, function () {
  48. beforeEach(function () {
  49. Object.assign(this, this.env[type]);
  50. [this.valueA, this.valueB, this.valueC] = this.values;
  51. });
  52. shouldBehaveLikeSet();
  53. });
  54. }
  55. });