AddressToUintMap.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { expectMembersMatch } = require('./helpers');
  4. const AddressToUintMapMock = artifacts.require('AddressToUintMapMock');
  5. contract('AddressToUintMap', function (accounts) {
  6. const [accountA, accountB, accountC] = accounts;
  7. const valueA = new BN('7891');
  8. const valueB = new BN('451');
  9. const valueC = new BN('9592328');
  10. beforeEach(async function () {
  11. this.map = await AddressToUintMapMock.new();
  12. });
  13. it('starts empty', async function () {
  14. expect(await this.map.contains(accountA)).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(accountA, valueA);
  20. expectEvent(receipt, 'OperationResult', { result: true });
  21. await expectMembersMatch(this.map, [accountA], [valueA]);
  22. });
  23. it('adds several keys', async function () {
  24. await this.map.set(accountA, valueA);
  25. await this.map.set(accountB, valueB);
  26. await expectMembersMatch(this.map, [accountA, accountB], [valueA, valueB]);
  27. expect(await this.map.contains(accountC)).to.equal(false);
  28. });
  29. it('returns false when adding keys already in the set', async function () {
  30. await this.map.set(accountA, valueA);
  31. const receipt = await this.map.set(accountA, valueA);
  32. expectEvent(receipt, 'OperationResult', { result: false });
  33. await expectMembersMatch(this.map, [accountA], [valueA]);
  34. });
  35. it('updates values for keys already in the set', async function () {
  36. await this.map.set(accountA, valueA);
  37. await this.map.set(accountA, valueB);
  38. await expectMembersMatch(this.map, [accountA], [valueB]);
  39. });
  40. });
  41. describe('remove', function () {
  42. it('removes added keys', async function () {
  43. await this.map.set(accountA, valueA);
  44. const receipt = await this.map.remove(accountA);
  45. expectEvent(receipt, 'OperationResult', { result: true });
  46. expect(await this.map.contains(accountA)).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(accountA);
  51. expectEvent(receipt, 'OperationResult', { result: false });
  52. expect(await this.map.contains(accountA)).to.equal(false);
  53. });
  54. it('adds and removes multiple keys', async function () {
  55. // []
  56. await this.map.set(accountA, valueA);
  57. await this.map.set(accountC, valueC);
  58. // [A, C]
  59. await this.map.remove(accountA);
  60. await this.map.remove(accountB);
  61. // [C]
  62. await this.map.set(accountB, valueB);
  63. // [C, B]
  64. await this.map.set(accountA, valueA);
  65. await this.map.remove(accountC);
  66. // [A, B]
  67. await this.map.set(accountA, valueA);
  68. await this.map.set(accountB, valueB);
  69. // [A, B]
  70. await this.map.set(accountC, valueC);
  71. await this.map.remove(accountA);
  72. // [B, C]
  73. await this.map.set(accountA, valueA);
  74. await this.map.remove(accountB);
  75. // [A, C]
  76. await expectMembersMatch(this.map, [accountA, accountC], [valueA, valueC]);
  77. expect(await this.map.contains(accountB)).to.equal(false);
  78. });
  79. });
  80. describe('read', function () {
  81. beforeEach(async function () {
  82. await this.map.set(accountA, valueA);
  83. });
  84. describe('get', function () {
  85. it('existing value', async function () {
  86. expect(await this.map.get(accountA)).to.bignumber.equal(valueA);
  87. });
  88. it('missing value', async function () {
  89. await expectRevert(this.map.get(accountB), 'EnumerableMap: nonexistent key');
  90. });
  91. });
  92. describe('tryGet', function () {
  93. const stringifyTryGetValue = ({ 0: result, 1: value }) => ({ 0: result, 1: value.toString() });
  94. it('existing value', async function () {
  95. const actual = stringifyTryGetValue(await this.map.tryGet(accountA));
  96. const expected = stringifyTryGetValue({ 0: true, 1: valueA });
  97. expect(actual).to.deep.equal(expected);
  98. });
  99. it('missing value', async function () {
  100. const actual = stringifyTryGetValue(await this.map.tryGet(accountB));
  101. const expected = stringifyTryGetValue({ 0: false, 1: new BN('0') });
  102. expect(actual).to.deep.equal(expected);
  103. });
  104. });
  105. });
  106. });