EnumerableSet.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const EnumerableSet = artifacts.require('$EnumerableSet');
  2. const { mapValues } = require('../../helpers/map-values');
  3. const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
  4. const getMethods = ms => {
  5. return mapValues(
  6. ms,
  7. m =>
  8. (self, ...args) =>
  9. self.methods[m](0, ...args),
  10. );
  11. };
  12. // Get the name of the library. In the transpiled code it will be EnumerableSetUpgradeable.
  13. const library = EnumerableSet._json.contractName.replace(/^\$/, '');
  14. contract('EnumerableSet', function (accounts) {
  15. beforeEach(async function () {
  16. this.set = await EnumerableSet.new();
  17. });
  18. // Bytes32Set
  19. describe('EnumerableBytes32Set', function () {
  20. shouldBehaveLikeSet(
  21. ['0xdeadbeef', '0x0123456789', '0x42424242'].map(e => e.padEnd(66, '0')),
  22. getMethods({
  23. add: '$add(uint256,bytes32)',
  24. remove: '$remove(uint256,bytes32)',
  25. contains: '$contains(uint256,bytes32)',
  26. length: `$length_${library}_Bytes32Set(uint256)`,
  27. at: `$at_${library}_Bytes32Set(uint256,uint256)`,
  28. values: `$values_${library}_Bytes32Set(uint256)`,
  29. }),
  30. {
  31. addReturn: `return$add_${library}_Bytes32Set_bytes32`,
  32. removeReturn: `return$remove_${library}_Bytes32Set_bytes32`,
  33. },
  34. );
  35. });
  36. // AddressSet
  37. describe('EnumerableAddressSet', function () {
  38. shouldBehaveLikeSet(
  39. accounts,
  40. getMethods({
  41. add: '$add(uint256,address)',
  42. remove: '$remove(uint256,address)',
  43. contains: '$contains(uint256,address)',
  44. length: `$length_${library}_AddressSet(uint256)`,
  45. at: `$at_${library}_AddressSet(uint256,uint256)`,
  46. values: `$values_${library}_AddressSet(uint256)`,
  47. }),
  48. {
  49. addReturn: `return$add_${library}_AddressSet_address`,
  50. removeReturn: `return$remove_${library}_AddressSet_address`,
  51. },
  52. );
  53. });
  54. // UintSet
  55. describe('EnumerableUintSet', function () {
  56. shouldBehaveLikeSet(
  57. [1234, 5678, 9101112].map(e => web3.utils.toBN(e)),
  58. getMethods({
  59. add: '$add(uint256,uint256)',
  60. remove: '$remove(uint256,uint256)',
  61. contains: '$contains(uint256,uint256)',
  62. length: `$length_${library}_UintSet(uint256)`,
  63. at: `$at_${library}_UintSet(uint256,uint256)`,
  64. values: `$values_${library}_UintSet(uint256)`,
  65. }),
  66. {
  67. addReturn: `return$add_${library}_UintSet_uint256`,
  68. removeReturn: `return$remove_${library}_UintSet_uint256`,
  69. },
  70. );
  71. });
  72. });