|
@@ -1,118 +1,33 @@
|
|
|
const { accounts, contract } = require('@openzeppelin/test-environment');
|
|
|
-const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
|
|
-const { expect } = require('chai');
|
|
|
+const { BN } = require('@openzeppelin/test-helpers');
|
|
|
|
|
|
-const EnumerableSetMock = contract.fromArtifact('EnumerableSetMock');
|
|
|
+const EnumerableAddressSetMock = contract.fromArtifact('EnumerableAddressSetMock');
|
|
|
+const EnumerableUintSetMock = contract.fromArtifact('EnumerableUintSetMock');
|
|
|
|
|
|
-describe('EnumerableSet', function () {
|
|
|
- const [ accountA, accountB, accountC ] = accounts;
|
|
|
-
|
|
|
- beforeEach(async function () {
|
|
|
- this.set = await EnumerableSetMock.new();
|
|
|
- });
|
|
|
-
|
|
|
- async function expectMembersMatch (set, values) {
|
|
|
- await Promise.all(values.map(async account =>
|
|
|
- expect(await set.contains(account)).to.equal(true)
|
|
|
- ));
|
|
|
-
|
|
|
- expect(await set.length()).to.bignumber.equal(values.length.toString());
|
|
|
-
|
|
|
- expect(await Promise.all([...Array(values.length).keys()].map(index =>
|
|
|
- set.at(index)
|
|
|
- ))).to.have.same.members(values);
|
|
|
- }
|
|
|
-
|
|
|
- it('starts empty', async function () {
|
|
|
- expect(await this.set.contains(accountA)).to.equal(false);
|
|
|
-
|
|
|
- await expectMembersMatch(this.set, []);
|
|
|
- });
|
|
|
-
|
|
|
- it('adds a value', async function () {
|
|
|
- const receipt = await this.set.add(accountA);
|
|
|
- expectEvent(receipt, 'OperationResult', { result: true });
|
|
|
+const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
|
|
|
|
|
|
- await expectMembersMatch(this.set, [accountA]);
|
|
|
- });
|
|
|
-
|
|
|
- it('adds several values', async function () {
|
|
|
- await this.set.add(accountA);
|
|
|
- await this.set.add(accountB);
|
|
|
-
|
|
|
- await expectMembersMatch(this.set, [accountA, accountB]);
|
|
|
- expect(await this.set.contains(accountC)).to.equal(false);
|
|
|
- });
|
|
|
-
|
|
|
- it('returns false when adding values already in the set', async function () {
|
|
|
- await this.set.add(accountA);
|
|
|
-
|
|
|
- const receipt = (await this.set.add(accountA));
|
|
|
- expectEvent(receipt, 'OperationResult', { result: false });
|
|
|
-
|
|
|
- await expectMembersMatch(this.set, [accountA]);
|
|
|
- });
|
|
|
-
|
|
|
- it('reverts when retrieving non-existent elements', async function () {
|
|
|
- await expectRevert(this.set.at(0), 'EnumerableSet: index out of bounds');
|
|
|
- });
|
|
|
-
|
|
|
- it('removes added values', async function () {
|
|
|
- await this.set.add(accountA);
|
|
|
-
|
|
|
- const receipt = await this.set.remove(accountA);
|
|
|
- expectEvent(receipt, 'OperationResult', { result: true });
|
|
|
-
|
|
|
- expect(await this.set.contains(accountA)).to.equal(false);
|
|
|
- await expectMembersMatch(this.set, []);
|
|
|
- });
|
|
|
+describe('EnumerableSet', function () {
|
|
|
+ // AddressSet
|
|
|
+ describe('EnumerableAddressSet', function () {
|
|
|
+ const [ accountA, accountB, accountC ] = accounts;
|
|
|
|
|
|
- it('returns false when removing values not in the set', async function () {
|
|
|
- const receipt = await this.set.remove(accountA);
|
|
|
- expectEvent(receipt, 'OperationResult', { result: false });
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.set = await EnumerableAddressSetMock.new();
|
|
|
+ });
|
|
|
|
|
|
- expect(await this.set.contains(accountA)).to.equal(false);
|
|
|
+ shouldBehaveLikeSet(accountA, accountB, accountC);
|
|
|
});
|
|
|
|
|
|
- it('adds and removes multiple values', async function () {
|
|
|
- // []
|
|
|
-
|
|
|
- await this.set.add(accountA);
|
|
|
- await this.set.add(accountC);
|
|
|
-
|
|
|
- // [A, C]
|
|
|
-
|
|
|
- await this.set.remove(accountA);
|
|
|
- await this.set.remove(accountB);
|
|
|
-
|
|
|
- // [C]
|
|
|
-
|
|
|
- await this.set.add(accountB);
|
|
|
-
|
|
|
- // [C, B]
|
|
|
-
|
|
|
- await this.set.add(accountA);
|
|
|
- await this.set.remove(accountC);
|
|
|
-
|
|
|
- // [A, B]
|
|
|
-
|
|
|
- await this.set.add(accountA);
|
|
|
- await this.set.add(accountB);
|
|
|
-
|
|
|
- // [A, B]
|
|
|
-
|
|
|
- await this.set.add(accountC);
|
|
|
- await this.set.remove(accountA);
|
|
|
-
|
|
|
- // [B, C]
|
|
|
-
|
|
|
- await this.set.add(accountA);
|
|
|
- await this.set.remove(accountB);
|
|
|
-
|
|
|
- // [A, C]
|
|
|
+ // UintSet
|
|
|
+ describe('EnumerableUintSet', function () {
|
|
|
+ const uintA = new BN('1234');
|
|
|
+ const uintB = new BN('5678');
|
|
|
+ const uintC = new BN('9101112');
|
|
|
|
|
|
- await expectMembersMatch(this.set, [accountA, accountC]);
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.set = await EnumerableUintSetMock.new();
|
|
|
+ });
|
|
|
|
|
|
- expect(await this.set.contains(accountB)).to.equal(false);
|
|
|
+ shouldBehaveLikeSet(uintA, uintB, uintC);
|
|
|
});
|
|
|
});
|