EnumerableSet.behavior.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. function shouldBehaveLikeSet (valueA, valueB, valueC) {
  4. async function expectMembersMatch (set, values) {
  5. await Promise.all(values.map(async value =>
  6. expect(await set.contains(value)).to.equal(true)
  7. ));
  8. expect(await set.length()).to.bignumber.equal(values.length.toString());
  9. // To compare values we convert to strings to workaround Chai
  10. // limitations when dealing with nested arrays (required for BNs)
  11. expect(await Promise.all([...Array(values.length).keys()].map(async (index) => {
  12. const entry = await set.at(index);
  13. return entry.toString();
  14. }))).to.have.same.members(values.map(v => v.toString()));
  15. }
  16. it('starts empty', async function () {
  17. expect(await this.set.contains(valueA)).to.equal(false);
  18. await expectMembersMatch(this.set, []);
  19. });
  20. it('adds a value', async function () {
  21. const receipt = await this.set.add(valueA);
  22. expectEvent(receipt, 'OperationResult', { result: true });
  23. await expectMembersMatch(this.set, [valueA]);
  24. });
  25. it('adds several values', async function () {
  26. await this.set.add(valueA);
  27. await this.set.add(valueB);
  28. await expectMembersMatch(this.set, [valueA, valueB]);
  29. expect(await this.set.contains(valueC)).to.equal(false);
  30. });
  31. it('returns false when adding values already in the set', async function () {
  32. await this.set.add(valueA);
  33. const receipt = (await this.set.add(valueA));
  34. expectEvent(receipt, 'OperationResult', { result: false });
  35. await expectMembersMatch(this.set, [valueA]);
  36. });
  37. it('reverts when retrieving non-existent elements', async function () {
  38. await expectRevert(this.set.at(0), 'EnumerableSet: index out of bounds');
  39. });
  40. it('removes added values', async function () {
  41. await this.set.add(valueA);
  42. const receipt = await this.set.remove(valueA);
  43. expectEvent(receipt, 'OperationResult', { result: true });
  44. expect(await this.set.contains(valueA)).to.equal(false);
  45. await expectMembersMatch(this.set, []);
  46. });
  47. it('returns false when removing values not in the set', async function () {
  48. const receipt = await this.set.remove(valueA);
  49. expectEvent(receipt, 'OperationResult', { result: false });
  50. expect(await this.set.contains(valueA)).to.equal(false);
  51. });
  52. it('adds and removes multiple values', async function () {
  53. // []
  54. await this.set.add(valueA);
  55. await this.set.add(valueC);
  56. // [A, C]
  57. await this.set.remove(valueA);
  58. await this.set.remove(valueB);
  59. // [C]
  60. await this.set.add(valueB);
  61. // [C, B]
  62. await this.set.add(valueA);
  63. await this.set.remove(valueC);
  64. // [A, B]
  65. await this.set.add(valueA);
  66. await this.set.add(valueB);
  67. // [A, B]
  68. await this.set.add(valueC);
  69. await this.set.remove(valueA);
  70. // [B, C]
  71. await this.set.add(valueA);
  72. await this.set.remove(valueB);
  73. // [A, C]
  74. await expectMembersMatch(this.set, [valueA, valueC]);
  75. expect(await this.set.contains(valueB)).to.equal(false);
  76. });
  77. }
  78. module.exports = {
  79. shouldBehaveLikeSet,
  80. };