UintToAddressMap.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { expectMembersMatch } = require('./helpers');
  4. const UintToAddressMapMock = artifacts.require('UintToAddressMapMock');
  5. contract('UintToAddressMap', function (accounts) {
  6. const [accountA, accountB, accountC] = accounts;
  7. const keyA = new BN('7891');
  8. const keyB = new BN('451');
  9. const keyC = new BN('9592328');
  10. beforeEach(async function () {
  11. this.map = await UintToAddressMapMock.new();
  12. });
  13. it('starts empty', async function () {
  14. expect(await this.map.contains(keyA)).to.equal(false);
  15. await expectMembersMatch(this.map, [], []);
  16. });
  17. describe('set', function () {
  18. it('adds a key', async function () {
  19. const receipt = await this.map.set(keyA, accountA);
  20. expectEvent(receipt, 'OperationResult', { result: true });
  21. await expectMembersMatch(this.map, [keyA], [accountA]);
  22. });
  23. it('adds several keys', async function () {
  24. await this.map.set(keyA, accountA);
  25. await this.map.set(keyB, accountB);
  26. await expectMembersMatch(this.map, [keyA, keyB], [accountA, accountB]);
  27. expect(await this.map.contains(keyC)).to.equal(false);
  28. });
  29. it('returns false when adding keys already in the set', async function () {
  30. await this.map.set(keyA, accountA);
  31. const receipt = await this.map.set(keyA, accountA);
  32. expectEvent(receipt, 'OperationResult', { result: false });
  33. await expectMembersMatch(this.map, [keyA], [accountA]);
  34. });
  35. it('updates values for keys already in the set', async function () {
  36. await this.map.set(keyA, accountA);
  37. await this.map.set(keyA, accountB);
  38. await expectMembersMatch(this.map, [keyA], [accountB]);
  39. });
  40. });
  41. describe('remove', function () {
  42. it('removes added keys', async function () {
  43. await this.map.set(keyA, accountA);
  44. const receipt = await this.map.remove(keyA);
  45. expectEvent(receipt, 'OperationResult', { result: true });
  46. expect(await this.map.contains(keyA)).to.equal(false);
  47. await expectMembersMatch(this.map, [], []);
  48. });
  49. it('returns false when removing keys not in the set', async function () {
  50. const receipt = await this.map.remove(keyA);
  51. expectEvent(receipt, 'OperationResult', { result: false });
  52. expect(await this.map.contains(keyA)).to.equal(false);
  53. });
  54. it('adds and removes multiple keys', async function () {
  55. // []
  56. await this.map.set(keyA, accountA);
  57. await this.map.set(keyC, accountC);
  58. // [A, C]
  59. await this.map.remove(keyA);
  60. await this.map.remove(keyB);
  61. // [C]
  62. await this.map.set(keyB, accountB);
  63. // [C, B]
  64. await this.map.set(keyA, accountA);
  65. await this.map.remove(keyC);
  66. // [A, B]
  67. await this.map.set(keyA, accountA);
  68. await this.map.set(keyB, accountB);
  69. // [A, B]
  70. await this.map.set(keyC, accountC);
  71. await this.map.remove(keyA);
  72. // [B, C]
  73. await this.map.set(keyA, accountA);
  74. await this.map.remove(keyB);
  75. // [A, C]
  76. await expectMembersMatch(this.map, [keyA, keyC], [accountA, accountC]);
  77. expect(await this.map.contains(keyB)).to.equal(false);
  78. });
  79. });
  80. describe('read', function () {
  81. beforeEach(async function () {
  82. await this.map.set(keyA, accountA);
  83. });
  84. describe('get', function () {
  85. it('existing value', async function () {
  86. expect(await this.map.get(keyA)).to.be.equal(accountA);
  87. });
  88. it('missing value', async function () {
  89. await expectRevert(this.map.get(keyB), 'EnumerableMap: nonexistent key');
  90. });
  91. });
  92. describe('get with message', function () {
  93. it('existing value', async function () {
  94. expect(await this.map.getWithMessage(keyA, 'custom error string')).to.be.equal(accountA);
  95. });
  96. it('missing value', async function () {
  97. await expectRevert(this.map.getWithMessage(keyB, 'custom error string'), 'custom error string');
  98. });
  99. });
  100. describe('tryGet', function () {
  101. it('existing value', async function () {
  102. expect(await this.map.tryGet(keyA)).to.be.deep.equal({
  103. 0: true,
  104. 1: accountA,
  105. });
  106. });
  107. it('missing value', async function () {
  108. expect(await this.map.tryGet(keyB)).to.be.deep.equal({
  109. 0: false,
  110. 1: constants.ZERO_ADDRESS,
  111. });
  112. });
  113. });
  114. });
  115. });