EnumerableMap.behavior.js 5.2 KB

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