EnumerableSet.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { expectEvent } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const EnumerableSetMock = contract.fromArtifact('EnumerableSetMock');
  5. describe('EnumerableSet', function () {
  6. const [ accountA, accountB, accountC ] = accounts;
  7. beforeEach(async function () {
  8. this.set = await EnumerableSetMock.new();
  9. });
  10. async function expectMembersMatch (set, members) {
  11. await Promise.all(members.map(async account =>
  12. expect(await set.contains(account)).to.equal(true)
  13. ));
  14. expect(await set.enumerate()).to.have.same.members(members);
  15. expect(await set.length()).to.bignumber.equal(members.length.toString());
  16. expect(await Promise.all([...Array(members.length).keys()].map(index =>
  17. set.get(index)
  18. ))).to.have.same.members(members);
  19. }
  20. it('starts empty', async function () {
  21. expect(await this.set.contains(accountA)).to.equal(false);
  22. await expectMembersMatch(this.set, []);
  23. });
  24. it('adds a value', async function () {
  25. const receipt = await this.set.add(accountA);
  26. expectEvent(receipt, 'TransactionResult', { result: true });
  27. await expectMembersMatch(this.set, [accountA]);
  28. });
  29. it('adds several values', async function () {
  30. await this.set.add(accountA);
  31. await this.set.add(accountB);
  32. await expectMembersMatch(this.set, [accountA, accountB]);
  33. expect(await this.set.contains(accountC)).to.equal(false);
  34. });
  35. it('returns false when adding elements already in the set', async function () {
  36. await this.set.add(accountA);
  37. const receipt = (await this.set.add(accountA));
  38. expectEvent(receipt, 'TransactionResult', { result: false });
  39. await expectMembersMatch(this.set, [accountA]);
  40. });
  41. it('removes added values', async function () {
  42. await this.set.add(accountA);
  43. const receipt = await this.set.remove(accountA);
  44. expectEvent(receipt, 'TransactionResult', { result: true });
  45. expect(await this.set.contains(accountA)).to.equal(false);
  46. await expectMembersMatch(this.set, []);
  47. });
  48. it('returns false when removing elements not in the set', async function () {
  49. const receipt = await this.set.remove(accountA);
  50. expectEvent(receipt, 'TransactionResult', { result: false });
  51. expect(await this.set.contains(accountA)).to.equal(false);
  52. });
  53. it('adds and removes multiple values', async function () {
  54. // []
  55. await this.set.add(accountA);
  56. await this.set.add(accountC);
  57. // [A, C]
  58. await this.set.remove(accountA);
  59. await this.set.remove(accountB);
  60. // [C]
  61. await this.set.add(accountB);
  62. // [C, B]
  63. await this.set.add(accountA);
  64. await this.set.remove(accountC);
  65. // [A, B]
  66. await this.set.add(accountA);
  67. await this.set.add(accountB);
  68. // [A, B]
  69. await this.set.add(accountC);
  70. await this.set.remove(accountA);
  71. // [B, C]
  72. await this.set.add(accountA);
  73. await this.set.remove(accountB);
  74. // [A, C]
  75. await expectMembersMatch(this.set, [accountA, accountC]);
  76. expect(await this.set.contains(accountB)).to.equal(false);
  77. });
  78. });