EnumerableSet.behavior.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. const contains = await Promise.all(values.map(value => set.contains(value)));
  6. expect(contains.every(Boolean)).to.be.equal(true);
  7. const length = await set.length();
  8. expect(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. const indexedValues = await Promise.all(Array(values.length).fill().map((_, index) => set.at(index)));
  12. expect(
  13. indexedValues.map(v => v.toString()),
  14. ).to.have.same.members(
  15. values.map(v => v.toString()),
  16. );
  17. const returnedValues = await set.values();
  18. expect(
  19. returnedValues.map(v => v.toString()),
  20. ).to.have.same.members(
  21. values.map(v => v.toString()),
  22. );
  23. }
  24. it('starts empty', async function () {
  25. expect(await this.set.contains(valueA)).to.equal(false);
  26. await expectMembersMatch(this.set, []);
  27. });
  28. describe('add', function () {
  29. it('adds a value', async function () {
  30. const receipt = await this.set.add(valueA);
  31. expectEvent(receipt, 'OperationResult', { result: true });
  32. await expectMembersMatch(this.set, [valueA]);
  33. });
  34. it('adds several values', async function () {
  35. await this.set.add(valueA);
  36. await this.set.add(valueB);
  37. await expectMembersMatch(this.set, [valueA, valueB]);
  38. expect(await this.set.contains(valueC)).to.equal(false);
  39. });
  40. it('returns false when adding values already in the set', async function () {
  41. await this.set.add(valueA);
  42. const receipt = (await this.set.add(valueA));
  43. expectEvent(receipt, 'OperationResult', { result: false });
  44. await expectMembersMatch(this.set, [valueA]);
  45. });
  46. });
  47. describe('at', function () {
  48. it('reverts when retrieving non-existent elements', async function () {
  49. await expectRevert.unspecified(this.set.at(0));
  50. });
  51. });
  52. describe('remove', function () {
  53. it('removes added values', async function () {
  54. await this.set.add(valueA);
  55. const receipt = await this.set.remove(valueA);
  56. expectEvent(receipt, 'OperationResult', { result: true });
  57. expect(await this.set.contains(valueA)).to.equal(false);
  58. await expectMembersMatch(this.set, []);
  59. });
  60. it('returns false when removing values not in the set', async function () {
  61. const receipt = await this.set.remove(valueA);
  62. expectEvent(receipt, 'OperationResult', { result: false });
  63. expect(await this.set.contains(valueA)).to.equal(false);
  64. });
  65. it('adds and removes multiple values', async function () {
  66. // []
  67. await this.set.add(valueA);
  68. await this.set.add(valueC);
  69. // [A, C]
  70. await this.set.remove(valueA);
  71. await this.set.remove(valueB);
  72. // [C]
  73. await this.set.add(valueB);
  74. // [C, B]
  75. await this.set.add(valueA);
  76. await this.set.remove(valueC);
  77. // [A, B]
  78. await this.set.add(valueA);
  79. await this.set.add(valueB);
  80. // [A, B]
  81. await this.set.add(valueC);
  82. await this.set.remove(valueA);
  83. // [B, C]
  84. await this.set.add(valueA);
  85. await this.set.remove(valueB);
  86. // [A, C]
  87. await expectMembersMatch(this.set, [valueA, valueC]);
  88. expect(await this.set.contains(valueB)).to.equal(false);
  89. });
  90. });
  91. }
  92. module.exports = {
  93. shouldBehaveLikeSet,
  94. };