EnumerableSet.test.js 1.7 KB

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