EnumerableMap.behavior.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const zip = require('lodash.zip');
  4. const { expectRevertCustomError } = require('../../helpers/customError');
  5. function shouldBehaveLikeMap(keys, values, zeroValue, methods, events) {
  6. const [keyA, keyB, keyC] = keys;
  7. const [valueA, valueB, valueC] = values;
  8. async function expectMembersMatch(map, keys, values) {
  9. expect(keys.length).to.equal(values.length);
  10. await Promise.all(keys.map(async key => expect(await methods.contains(map, key)).to.equal(true)));
  11. expect(await methods.length(map)).to.bignumber.equal(keys.length.toString());
  12. expect((await Promise.all(keys.map(key => methods.get(map, key)))).map(k => k.toString())).to.have.same.members(
  13. values.map(value => value.toString()),
  14. );
  15. // To compare key-value pairs, we zip keys and values, and convert BNs to
  16. // strings to workaround Chai limitations when dealing with nested arrays
  17. expect(
  18. await Promise.all(
  19. [...Array(keys.length).keys()].map(async index => {
  20. const entry = await methods.at(map, index);
  21. return [entry[0].toString(), entry[1].toString()];
  22. }),
  23. ),
  24. ).to.have.same.deep.members(
  25. zip(
  26. keys.map(k => k.toString()),
  27. values.map(v => v.toString()),
  28. ),
  29. );
  30. // This also checks that both arrays have the same length
  31. expect((await methods.keys(map)).map(k => k.toString())).to.have.same.members(keys.map(key => key.toString()));
  32. }
  33. it('starts empty', async function () {
  34. expect(await methods.contains(this.map, keyA)).to.equal(false);
  35. await expectMembersMatch(this.map, [], []);
  36. });
  37. describe('set', function () {
  38. it('adds a key', async function () {
  39. const receipt = await methods.set(this.map, keyA, valueA);
  40. expectEvent(receipt, events.setReturn, { ret0: true });
  41. await expectMembersMatch(this.map, [keyA], [valueA]);
  42. });
  43. it('adds several keys', async function () {
  44. await methods.set(this.map, keyA, valueA);
  45. await methods.set(this.map, keyB, valueB);
  46. await expectMembersMatch(this.map, [keyA, keyB], [valueA, valueB]);
  47. expect(await methods.contains(this.map, keyC)).to.equal(false);
  48. });
  49. it('returns false when adding keys already in the set', async function () {
  50. await methods.set(this.map, keyA, valueA);
  51. const receipt = await methods.set(this.map, keyA, valueA);
  52. expectEvent(receipt, events.setReturn, { ret0: false });
  53. await expectMembersMatch(this.map, [keyA], [valueA]);
  54. });
  55. it('updates values for keys already in the set', async function () {
  56. await methods.set(this.map, keyA, valueA);
  57. await methods.set(this.map, keyA, valueB);
  58. await expectMembersMatch(this.map, [keyA], [valueB]);
  59. });
  60. });
  61. describe('remove', function () {
  62. it('removes added keys', async function () {
  63. await methods.set(this.map, keyA, valueA);
  64. const receipt = await methods.remove(this.map, keyA);
  65. expectEvent(receipt, events.removeReturn, { ret0: true });
  66. expect(await methods.contains(this.map, keyA)).to.equal(false);
  67. await expectMembersMatch(this.map, [], []);
  68. });
  69. it('returns false when removing keys not in the set', async function () {
  70. const receipt = await methods.remove(this.map, keyA);
  71. expectEvent(receipt, events.removeReturn, { ret0: false });
  72. expect(await methods.contains(this.map, keyA)).to.equal(false);
  73. });
  74. it('adds and removes multiple keys', async function () {
  75. // []
  76. await methods.set(this.map, keyA, valueA);
  77. await methods.set(this.map, keyC, valueC);
  78. // [A, C]
  79. await methods.remove(this.map, keyA);
  80. await methods.remove(this.map, keyB);
  81. // [C]
  82. await methods.set(this.map, keyB, valueB);
  83. // [C, B]
  84. await methods.set(this.map, keyA, valueA);
  85. await methods.remove(this.map, keyC);
  86. // [A, B]
  87. await methods.set(this.map, keyA, valueA);
  88. await methods.set(this.map, keyB, valueB);
  89. // [A, B]
  90. await methods.set(this.map, keyC, valueC);
  91. await methods.remove(this.map, keyA);
  92. // [B, C]
  93. await methods.set(this.map, keyA, valueA);
  94. await methods.remove(this.map, keyB);
  95. // [A, C]
  96. await expectMembersMatch(this.map, [keyA, keyC], [valueA, valueC]);
  97. expect(await methods.contains(this.map, keyA)).to.equal(true);
  98. expect(await methods.contains(this.map, keyB)).to.equal(false);
  99. expect(await methods.contains(this.map, keyC)).to.equal(true);
  100. });
  101. });
  102. describe('read', function () {
  103. beforeEach(async function () {
  104. await methods.set(this.map, keyA, valueA);
  105. });
  106. describe('get', function () {
  107. it('existing value', async function () {
  108. expect(await methods.get(this.map, keyA).then(r => r.toString())).to.be.equal(valueA.toString());
  109. });
  110. it('missing value', async function () {
  111. const key = web3.utils.toHex(keyB);
  112. await expectRevertCustomError(methods.get(this.map, keyB), 'EnumerableMapNonexistentKey', [
  113. key.length == 66 ? key : web3.utils.padLeft(key, 64, '0'),
  114. ]);
  115. });
  116. });
  117. describe('tryGet', function () {
  118. it('existing value', async function () {
  119. const result = await methods.tryGet(this.map, keyA);
  120. expect(result['0']).to.be.equal(true);
  121. expect(result['1'].toString()).to.be.equal(valueA.toString());
  122. });
  123. it('missing value', async function () {
  124. const result = await methods.tryGet(this.map, keyB);
  125. expect(result['0']).to.be.equal(false);
  126. expect(result['1'].toString()).to.be.equal(zeroValue.toString());
  127. });
  128. });
  129. });
  130. }
  131. module.exports = {
  132. shouldBehaveLikeMap,
  133. };