EnumerableSet.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { ethers } = require('hardhat');
  2. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  3. const { mapValues } = require('../../helpers/iterate');
  4. const { randomArray, generators } = require('../../helpers/random');
  5. const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
  6. const getMethods = (mock, fnSigs) => {
  7. return mapValues(
  8. fnSigs,
  9. fnSig =>
  10. (...args) =>
  11. mock.getFunction(fnSig)(0, ...args),
  12. );
  13. };
  14. describe('EnumerableSet', function () {
  15. // Bytes32Set
  16. describe('EnumerableBytes32Set', function () {
  17. const fixture = async () => {
  18. const mock = await ethers.deployContract('$EnumerableSet');
  19. const [valueA, valueB, valueC] = randomArray(generators.bytes32);
  20. const methods = getMethods(mock, {
  21. add: '$add(uint256,bytes32)',
  22. remove: '$remove(uint256,bytes32)',
  23. contains: '$contains(uint256,bytes32)',
  24. length: `$length_EnumerableSet_Bytes32Set(uint256)`,
  25. at: `$at_EnumerableSet_Bytes32Set(uint256,uint256)`,
  26. values: `$values_EnumerableSet_Bytes32Set(uint256)`,
  27. });
  28. return { mock, valueA, valueB, valueC, methods };
  29. };
  30. beforeEach(async function () {
  31. Object.assign(this, await loadFixture(fixture));
  32. });
  33. shouldBehaveLikeSet({
  34. addReturn: `return$add_EnumerableSet_Bytes32Set_bytes32`,
  35. removeReturn: `return$remove_EnumerableSet_Bytes32Set_bytes32`,
  36. });
  37. });
  38. // AddressSet
  39. describe('EnumerableAddressSet', function () {
  40. const fixture = async () => {
  41. const mock = await ethers.deployContract('$EnumerableSet');
  42. const [valueA, valueB, valueC] = randomArray(generators.address);
  43. const methods = getMethods(mock, {
  44. add: '$add(uint256,address)',
  45. remove: '$remove(uint256,address)',
  46. contains: '$contains(uint256,address)',
  47. length: `$length_EnumerableSet_AddressSet(uint256)`,
  48. at: `$at_EnumerableSet_AddressSet(uint256,uint256)`,
  49. values: `$values_EnumerableSet_AddressSet(uint256)`,
  50. });
  51. return { mock, valueA, valueB, valueC, methods };
  52. };
  53. beforeEach(async function () {
  54. Object.assign(this, await loadFixture(fixture));
  55. });
  56. shouldBehaveLikeSet({
  57. addReturn: `return$add_EnumerableSet_AddressSet_address`,
  58. removeReturn: `return$remove_EnumerableSet_AddressSet_address`,
  59. });
  60. });
  61. // UintSet
  62. describe('EnumerableUintSet', function () {
  63. const fixture = async () => {
  64. const mock = await ethers.deployContract('$EnumerableSet');
  65. const [valueA, valueB, valueC] = randomArray(generators.uint256);
  66. const methods = getMethods(mock, {
  67. add: '$add(uint256,uint256)',
  68. remove: '$remove(uint256,uint256)',
  69. contains: '$contains(uint256,uint256)',
  70. length: `$length_EnumerableSet_UintSet(uint256)`,
  71. at: `$at_EnumerableSet_UintSet(uint256,uint256)`,
  72. values: `$values_EnumerableSet_UintSet(uint256)`,
  73. });
  74. return { mock, valueA, valueB, valueC, methods };
  75. };
  76. beforeEach(async function () {
  77. Object.assign(this, await loadFixture(fixture));
  78. });
  79. shouldBehaveLikeSet({
  80. addReturn: `return$add_EnumerableSet_UintSet_uint256`,
  81. removeReturn: `return$remove_EnumerableSet_UintSet_uint256`,
  82. });
  83. });
  84. });