EnumerableMap.test.js 2.2 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, formatType } = require('../../../scripts/generate/templates/EnumerableMap.opts');
  6. const { shouldBehaveLikeMap } = require('./EnumerableMap.behavior');
  7. // Add Bytes32ToBytes32Map that must be tested but is not part of the generated types.
  8. TYPES.unshift(formatType('bytes32', 'bytes32'));
  9. async function fixture() {
  10. const mock = await ethers.deployContract('$EnumerableMap');
  11. const env = Object.fromEntries(
  12. TYPES.map(({ name, keyType, valueType }) => [
  13. name,
  14. {
  15. keyType,
  16. keys: Array.from({ length: 3 }, generators[keyType]),
  17. values: Array.from({ length: 3 }, generators[valueType]),
  18. zeroValue: generators[valueType].zero,
  19. methods: mapValues(
  20. {
  21. set: `$set(uint256,${keyType},${valueType})`,
  22. get: `$get_EnumerableMap_${name}(uint256,${keyType})`,
  23. tryGet: `$tryGet_EnumerableMap_${name}(uint256,${keyType})`,
  24. remove: `$remove_EnumerableMap_${name}(uint256,${keyType})`,
  25. length: `$length_EnumerableMap_${name}(uint256)`,
  26. at: `$at_EnumerableMap_${name}(uint256,uint256)`,
  27. contains: `$contains_EnumerableMap_${name}(uint256,${keyType})`,
  28. keys: `$keys_EnumerableMap_${name}(uint256)`,
  29. },
  30. fnSig =>
  31. (...args) =>
  32. mock.getFunction(fnSig)(0, ...args),
  33. ),
  34. events: {
  35. setReturn: `return$set_EnumerableMap_${name}_${keyType}_${valueType}`,
  36. removeReturn: `return$remove_EnumerableMap_${name}_${keyType}`,
  37. },
  38. },
  39. ]),
  40. );
  41. return { mock, env };
  42. }
  43. describe('EnumerableMap', function () {
  44. beforeEach(async function () {
  45. Object.assign(this, await loadFixture(fixture));
  46. });
  47. for (const { name } of TYPES) {
  48. describe(name, function () {
  49. beforeEach(async function () {
  50. Object.assign(this, this.env[name]);
  51. [this.keyA, this.keyB, this.keyC] = this.keys;
  52. [this.valueA, this.valueB, this.valueC] = this.values;
  53. });
  54. shouldBehaveLikeMap();
  55. });
  56. }
  57. });