EnumerableSet.test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. clear: `$clear_EnumerableSet_${name}(uint256)`,
  29. contains: `$contains(uint256,${type})`,
  30. length: `$length_EnumerableSet_${name}(uint256)`,
  31. at: `$at_EnumerableSet_${name}(uint256,uint256)`,
  32. values: `$values_EnumerableSet_${name}(uint256)`,
  33. }),
  34. events: {
  35. addReturn: `return$add_EnumerableSet_${name}_${type.replace(/[[\]]/g, '_')}`,
  36. removeReturn: `return$remove_EnumerableSet_${name}_${type.replace(/[[\]]/g, '_')}`,
  37. },
  38. },
  39. ]),
  40. );
  41. return { mock, env };
  42. }
  43. describe('EnumerableSet', function () {
  44. beforeEach(async function () {
  45. Object.assign(this, await loadFixture(fixture));
  46. });
  47. for (const { type } of TYPES) {
  48. describe(type, function () {
  49. beforeEach(function () {
  50. Object.assign(this, this.env[type]);
  51. [this.valueA, this.valueB, this.valueC] = this.values;
  52. });
  53. shouldBehaveLikeSet();
  54. });
  55. }
  56. });