Add EnumerableMap, refactor ERC721 (#2160)
* Implement AddressSet in terms of a generic Set * Add Uint256Set * Add EnumerableMap * Fix wording on EnumerableSet docs and tests * Refactor ERC721 using EnumerableSet and EnumerableMap * Fix tests * Fix linter error * Gas optimization for EnumerableMap * Gas optimization for EnumerableSet * Remove often not-taken if from Enumerable data structures * Fix failing test * Gas optimization for EnumerableMap * Fix linter errors * Add comment for clarification * Improve test naming * Rename EnumerableMap.add to set * Add overload for EnumerableMap.get with custom error message * Improve Enumerable docs * Rename Uint256Set to UintSet * Add changelog entry
This commit is contained in:
@ -11,18 +11,16 @@ describe('EnumerableSet', function () {
|
||||
this.set = await EnumerableSetMock.new();
|
||||
});
|
||||
|
||||
async function expectMembersMatch (set, members) {
|
||||
await Promise.all(members.map(async account =>
|
||||
async function expectMembersMatch (set, values) {
|
||||
await Promise.all(values.map(async account =>
|
||||
expect(await set.contains(account)).to.equal(true)
|
||||
));
|
||||
|
||||
expect(await set.enumerate()).to.have.same.members(members);
|
||||
expect(await set.length()).to.bignumber.equal(values.length.toString());
|
||||
|
||||
expect(await set.length()).to.bignumber.equal(members.length.toString());
|
||||
|
||||
expect(await Promise.all([...Array(members.length).keys()].map(index =>
|
||||
expect(await Promise.all([...Array(values.length).keys()].map(index =>
|
||||
set.at(index)
|
||||
))).to.have.same.members(members);
|
||||
))).to.have.same.members(values);
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
@ -33,7 +31,7 @@ describe('EnumerableSet', function () {
|
||||
|
||||
it('adds a value', async function () {
|
||||
const receipt = await this.set.add(accountA);
|
||||
expectEvent(receipt, 'TransactionResult', { result: true });
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
await expectMembersMatch(this.set, [accountA]);
|
||||
});
|
||||
@ -46,11 +44,11 @@ describe('EnumerableSet', function () {
|
||||
expect(await this.set.contains(accountC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('returns false when adding elements already in the set', async function () {
|
||||
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, 'TransactionResult', { result: false });
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
await expectMembersMatch(this.set, [accountA]);
|
||||
});
|
||||
@ -63,15 +61,15 @@ describe('EnumerableSet', function () {
|
||||
await this.set.add(accountA);
|
||||
|
||||
const receipt = await this.set.remove(accountA);
|
||||
expectEvent(receipt, 'TransactionResult', { result: true });
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
expect(await this.set.contains(accountA)).to.equal(false);
|
||||
await expectMembersMatch(this.set, []);
|
||||
});
|
||||
|
||||
it('returns false when removing elements not in the set', async function () {
|
||||
it('returns false when removing values not in the set', async function () {
|
||||
const receipt = await this.set.remove(accountA);
|
||||
expectEvent(receipt, 'TransactionResult', { result: false });
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
expect(await this.set.contains(accountA)).to.equal(false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user