EnumerableSet.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 }) => [
  19. type,
  20. {
  21. values: Array.from({ length: 3 }, generators[type]),
  22. methods: getMethods(mock, {
  23. add: `$add(uint256,${type})`,
  24. remove: `$remove(uint256,${type})`,
  25. clear: `$clear_EnumerableSet_${name}(uint256)`,
  26. contains: `$contains(uint256,${type})`,
  27. length: `$length_EnumerableSet_${name}(uint256)`,
  28. at: `$at_EnumerableSet_${name}(uint256,uint256)`,
  29. values: `$values_EnumerableSet_${name}(uint256)`,
  30. }),
  31. events: {
  32. addReturn: `return$add_EnumerableSet_${name}_${type}`,
  33. removeReturn: `return$remove_EnumerableSet_${name}_${type}`,
  34. },
  35. },
  36. ]),
  37. );
  38. return { mock, env };
  39. }
  40. describe('EnumerableSet', function () {
  41. beforeEach(async function () {
  42. Object.assign(this, await loadFixture(fixture));
  43. });
  44. for (const { type } of TYPES) {
  45. describe(type, function () {
  46. beforeEach(function () {
  47. Object.assign(this, this.env[type]);
  48. [this.valueA, this.valueB, this.valueC] = this.values;
  49. });
  50. shouldBehaveLikeSet();
  51. });
  52. }
  53. });