EnumerableMap.behavior.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const zip = (array1, array2) => array1.map((item, index) => [item, array2[index]]);
  4. function shouldBehaveLikeMap() {
  5. async function expectMembersMatch(methods, keys, values) {
  6. expect(keys.length).to.equal(values.length);
  7. expect(await methods.length()).to.equal(keys.length);
  8. expect([...(await methods.keys())]).to.have.members(keys);
  9. for (const [key, value] of zip(keys, values)) {
  10. expect(await methods.contains(key)).to.be.true;
  11. expect(await methods.get(key)).to.equal(value);
  12. }
  13. expect(await Promise.all(keys.map((_, index) => methods.at(index)))).to.have.deep.members(zip(keys, values));
  14. }
  15. it('starts empty', async function () {
  16. expect(await this.methods.contains(this.keyA)).to.be.false;
  17. await expectMembersMatch(this.methods, [], []);
  18. });
  19. describe('set', function () {
  20. it('adds a key', async function () {
  21. await expect(this.methods.set(this.keyA, this.valueA)).to.emit(this.mock, this.events.setReturn).withArgs(true);
  22. await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
  23. });
  24. it('adds several keys', async function () {
  25. await this.methods.set(this.keyA, this.valueA);
  26. await this.methods.set(this.keyB, this.valueB);
  27. await expectMembersMatch(this.methods, [this.keyA, this.keyB], [this.valueA, this.valueB]);
  28. expect(await this.methods.contains(this.keyC)).to.be.false;
  29. });
  30. it('returns false when adding keys already in the set', async function () {
  31. await this.methods.set(this.keyA, this.valueA);
  32. await expect(this.methods.set(this.keyA, this.valueA)).to.emit(this.mock, this.events.setReturn).withArgs(false);
  33. await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
  34. });
  35. it('updates values for keys already in the set', async function () {
  36. await this.methods.set(this.keyA, this.valueA);
  37. await this.methods.set(this.keyA, this.valueB);
  38. await expectMembersMatch(this.methods, [this.keyA], [this.valueB]);
  39. });
  40. });
  41. describe('remove', function () {
  42. it('removes added keys', async function () {
  43. await this.methods.set(this.keyA, this.valueA);
  44. await expect(this.methods.remove(this.keyA)).to.emit(this.mock, this.events.removeReturn).withArgs(true);
  45. expect(await this.methods.contains(this.keyA)).to.be.false;
  46. await expectMembersMatch(this.methods, [], []);
  47. });
  48. it('returns false when removing keys not in the set', async function () {
  49. await expect(await this.methods.remove(this.keyA))
  50. .to.emit(this.mock, this.events.removeReturn)
  51. .withArgs(false);
  52. expect(await this.methods.contains(this.keyA)).to.be.false;
  53. });
  54. it('adds and removes multiple keys', async function () {
  55. // []
  56. await this.methods.set(this.keyA, this.valueA);
  57. await this.methods.set(this.keyC, this.valueC);
  58. // [A, C]
  59. await this.methods.remove(this.keyA);
  60. await this.methods.remove(this.keyB);
  61. // [C]
  62. await this.methods.set(this.keyB, this.valueB);
  63. // [C, B]
  64. await this.methods.set(this.keyA, this.valueA);
  65. await this.methods.remove(this.keyC);
  66. // [A, B]
  67. await this.methods.set(this.keyA, this.valueA);
  68. await this.methods.set(this.keyB, this.valueB);
  69. // [A, B]
  70. await this.methods.set(this.keyC, this.valueC);
  71. await this.methods.remove(this.keyA);
  72. // [B, C]
  73. await this.methods.set(this.keyA, this.valueA);
  74. await this.methods.remove(this.keyB);
  75. // [A, C]
  76. await expectMembersMatch(this.methods, [this.keyA, this.keyC], [this.valueA, this.valueC]);
  77. expect(await this.methods.contains(this.keyA)).to.be.true;
  78. expect(await this.methods.contains(this.keyB)).to.be.false;
  79. expect(await this.methods.contains(this.keyC)).to.be.true;
  80. });
  81. });
  82. describe('clear', function () {
  83. it('clears a single entry', async function () {
  84. await this.methods.set(this.keyA, this.valueA);
  85. await this.methods.clear();
  86. expect(await this.methods.contains(this.keyA)).to.be.false;
  87. await expectMembersMatch(this.methods, [], []);
  88. });
  89. it('clears multiple entries', async function () {
  90. await this.methods.set(this.keyA, this.valueA);
  91. await this.methods.set(this.keyB, this.valueB);
  92. await this.methods.set(this.keyC, this.valueC);
  93. await this.methods.clear();
  94. expect(await this.methods.contains(this.keyA)).to.be.false;
  95. expect(await this.methods.contains(this.keyB)).to.be.false;
  96. expect(await this.methods.contains(this.keyC)).to.be.false;
  97. await expectMembersMatch(this.methods, [], []);
  98. });
  99. it('does not revert on empty map', async function () {
  100. await this.methods.clear();
  101. });
  102. it('clear then add entry', async function () {
  103. await this.methods.set(this.keyA, this.valueA);
  104. await this.methods.set(this.keyB, this.valueB);
  105. await this.methods.set(this.keyC, this.valueC);
  106. await this.methods.clear();
  107. await this.methods.set(this.keyA, this.valueA);
  108. expect(await this.methods.contains(this.keyA)).to.be.true;
  109. expect(await this.methods.contains(this.keyB)).to.be.false;
  110. expect(await this.methods.contains(this.keyC)).to.be.false;
  111. await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
  112. });
  113. });
  114. describe('read', function () {
  115. beforeEach(async function () {
  116. await this.methods.set(this.keyA, this.valueA);
  117. });
  118. describe('get', function () {
  119. it('existing value', async function () {
  120. expect(await this.methods.get(this.keyA)).to.equal(this.valueA);
  121. });
  122. it('missing value', async function () {
  123. await expect(this.methods.get(this.keyB))
  124. .to.be.revertedWithCustomError(this.mock, 'EnumerableMapNonexistentKey')
  125. .withArgs(ethers.AbiCoder.defaultAbiCoder().encode([this.keyType], [this.keyB]));
  126. });
  127. });
  128. describe('tryGet', function () {
  129. it('existing value', async function () {
  130. expect(await this.methods.tryGet(this.keyA)).to.have.ordered.members([true, this.valueA]);
  131. });
  132. it('missing value', async function () {
  133. expect(await this.methods.tryGet(this.keyB)).to.have.ordered.members([false, this.zeroValue]);
  134. });
  135. });
  136. });
  137. }
  138. module.exports = {
  139. shouldBehaveLikeMap,
  140. };