Reorganize the repo structure (#2503)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
181
test/utils/structs/EnumerableMap.test.js
Normal file
181
test/utils/structs/EnumerableMap.test.js
Normal file
@ -0,0 +1,181 @@
|
||||
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const zip = require('lodash.zip');
|
||||
|
||||
const EnumerableMapMock = artifacts.require('EnumerableMapMock');
|
||||
|
||||
contract('EnumerableMap', function (accounts) {
|
||||
const [ accountA, accountB, accountC ] = accounts;
|
||||
|
||||
const keyA = new BN('7891');
|
||||
const keyB = new BN('451');
|
||||
const keyC = new BN('9592328');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.map = await EnumerableMapMock.new();
|
||||
});
|
||||
|
||||
async function expectMembersMatch (map, keys, values) {
|
||||
expect(keys.length).to.equal(values.length);
|
||||
|
||||
await Promise.all(keys.map(async key =>
|
||||
expect(await map.contains(key)).to.equal(true),
|
||||
));
|
||||
|
||||
expect(await map.length()).to.bignumber.equal(keys.length.toString());
|
||||
|
||||
expect(await Promise.all(keys.map(key =>
|
||||
map.get(key),
|
||||
))).to.have.same.members(values);
|
||||
|
||||
// To compare key-value pairs, we zip keys and values, and convert BNs to
|
||||
// strings to workaround Chai limitations when dealing with nested arrays
|
||||
expect(await Promise.all([...Array(keys.length).keys()].map(async (index) => {
|
||||
const entry = await map.at(index);
|
||||
return [entry.key.toString(), entry.value];
|
||||
}))).to.have.same.deep.members(
|
||||
zip(keys.map(k => k.toString()), values),
|
||||
);
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await this.map.contains(keyA)).to.equal(false);
|
||||
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
});
|
||||
|
||||
describe('set', function () {
|
||||
it('adds a key', async function () {
|
||||
const receipt = await this.map.set(keyA, accountA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [accountA]);
|
||||
});
|
||||
|
||||
it('adds several keys', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.set(keyB, accountB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyB], [accountA, accountB]);
|
||||
expect(await this.map.contains(keyC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('returns false when adding keys already in the set', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
|
||||
const receipt = (await this.map.set(keyA, accountA));
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [accountA]);
|
||||
});
|
||||
|
||||
it('updates values for keys already in the set', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
|
||||
await this.map.set(keyA, accountB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [accountB]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', function () {
|
||||
it('removes added keys', async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
|
||||
const receipt = await this.map.remove(keyA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
expect(await this.map.contains(keyA)).to.equal(false);
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
});
|
||||
|
||||
it('returns false when removing keys not in the set', async function () {
|
||||
const receipt = await this.map.remove(keyA);
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
expect(await this.map.contains(keyA)).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds and removes multiple keys', async function () {
|
||||
// []
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.set(keyC, accountC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await this.map.remove(keyA);
|
||||
await this.map.remove(keyB);
|
||||
|
||||
// [C]
|
||||
|
||||
await this.map.set(keyB, accountB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.remove(keyC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.set(keyB, accountB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.map.set(keyC, accountC);
|
||||
await this.map.remove(keyA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await this.map.set(keyA, accountA);
|
||||
await this.map.remove(keyB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyC], [accountA, accountC]);
|
||||
|
||||
expect(await this.map.contains(keyB)).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('read', function () {
|
||||
beforeEach(async function () {
|
||||
await this.map.set(keyA, accountA);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await this.map.get(keyA)).to.be.equal(accountA);
|
||||
});
|
||||
it('missing value', async function () {
|
||||
await expectRevert(this.map.get(keyB), 'EnumerableMap: nonexistent key');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get with message', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await this.map.getWithMessage(keyA, 'custom error string')).to.be.equal(accountA);
|
||||
});
|
||||
it('missing value', async function () {
|
||||
await expectRevert(this.map.getWithMessage(keyB, 'custom error string'), 'custom error string');
|
||||
});
|
||||
});
|
||||
|
||||
describe('tryGet', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await this.map.tryGet(keyA)).to.be.deep.equal({
|
||||
0: true,
|
||||
1: accountA,
|
||||
});
|
||||
});
|
||||
it('missing value', async function () {
|
||||
expect(await this.map.tryGet(keyB)).to.be.deep.equal({
|
||||
0: false,
|
||||
1: constants.ZERO_ADDRESS,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
122
test/utils/structs/EnumerableSet.behavior.js
Normal file
122
test/utils/structs/EnumerableSet.behavior.js
Normal file
@ -0,0 +1,122 @@
|
||||
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
function shouldBehaveLikeSet (valueA, valueB, valueC) {
|
||||
async function expectMembersMatch (set, values) {
|
||||
await Promise.all(values.map(async value =>
|
||||
expect(await set.contains(value)).to.equal(true),
|
||||
));
|
||||
|
||||
expect(await set.length()).to.bignumber.equal(values.length.toString());
|
||||
|
||||
// To compare values we convert to strings to workaround Chai
|
||||
// limitations when dealing with nested arrays (required for BNs)
|
||||
expect(await Promise.all([...Array(values.length).keys()].map(async (index) => {
|
||||
const entry = await set.at(index);
|
||||
return entry.toString();
|
||||
}))).to.have.same.members(values.map(v => v.toString()));
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await this.set.contains(valueA)).to.equal(false);
|
||||
|
||||
await expectMembersMatch(this.set, []);
|
||||
});
|
||||
|
||||
describe('add', function () {
|
||||
it('adds a value', async function () {
|
||||
const receipt = await this.set.add(valueA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
await expectMembersMatch(this.set, [valueA]);
|
||||
});
|
||||
|
||||
it('adds several values', async function () {
|
||||
await this.set.add(valueA);
|
||||
await this.set.add(valueB);
|
||||
|
||||
await expectMembersMatch(this.set, [valueA, valueB]);
|
||||
expect(await this.set.contains(valueC)).to.equal(false);
|
||||
});
|
||||
|
||||
it('returns false when adding values already in the set', async function () {
|
||||
await this.set.add(valueA);
|
||||
|
||||
const receipt = (await this.set.add(valueA));
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
await expectMembersMatch(this.set, [valueA]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('at', function () {
|
||||
it('reverts when retrieving non-existent elements', async function () {
|
||||
await expectRevert(this.set.at(0), 'EnumerableSet: index out of bounds');
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', function () {
|
||||
it('removes added values', async function () {
|
||||
await this.set.add(valueA);
|
||||
|
||||
const receipt = await this.set.remove(valueA);
|
||||
expectEvent(receipt, 'OperationResult', { result: true });
|
||||
|
||||
expect(await this.set.contains(valueA)).to.equal(false);
|
||||
await expectMembersMatch(this.set, []);
|
||||
});
|
||||
|
||||
it('returns false when removing values not in the set', async function () {
|
||||
const receipt = await this.set.remove(valueA);
|
||||
expectEvent(receipt, 'OperationResult', { result: false });
|
||||
|
||||
expect(await this.set.contains(valueA)).to.equal(false);
|
||||
});
|
||||
|
||||
it('adds and removes multiple values', async function () {
|
||||
// []
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.add(valueC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await this.set.remove(valueA);
|
||||
await this.set.remove(valueB);
|
||||
|
||||
// [C]
|
||||
|
||||
await this.set.add(valueB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.remove(valueC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.add(valueB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await this.set.add(valueC);
|
||||
await this.set.remove(valueA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await this.set.add(valueA);
|
||||
await this.set.remove(valueB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await expectMembersMatch(this.set, [valueA, valueC]);
|
||||
|
||||
expect(await this.set.contains(valueB)).to.equal(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeSet,
|
||||
};
|
||||
46
test/utils/structs/EnumerableSet.test.js
Normal file
46
test/utils/structs/EnumerableSet.test.js
Normal file
@ -0,0 +1,46 @@
|
||||
const { BN } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const EnumerableBytes32SetMock = artifacts.require('EnumerableBytes32SetMock');
|
||||
const EnumerableAddressSetMock = artifacts.require('EnumerableAddressSetMock');
|
||||
const EnumerableUintSetMock = artifacts.require('EnumerableUintSetMock');
|
||||
|
||||
const { shouldBehaveLikeSet } = require('./EnumerableSet.behavior');
|
||||
|
||||
contract('EnumerableSet', function (accounts) {
|
||||
// Bytes32Set
|
||||
describe('EnumerableBytes32Set', function () {
|
||||
const bytesA = '0xdeadbeef'.padEnd(66, '0');
|
||||
const bytesB = '0x0123456789'.padEnd(66, '0');
|
||||
const bytesC = '0x42424242'.padEnd(66, '0');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.set = await EnumerableBytes32SetMock.new();
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet(bytesA, bytesB, bytesC);
|
||||
});
|
||||
|
||||
// AddressSet
|
||||
describe('EnumerableAddressSet', function () {
|
||||
const [accountA, accountB, accountC] = accounts;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.set = await EnumerableAddressSetMock.new();
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet(accountA, accountB, accountC);
|
||||
});
|
||||
|
||||
// UintSet
|
||||
describe('EnumerableUintSet', function () {
|
||||
const uintA = new BN('1234');
|
||||
const uintB = new BN('5678');
|
||||
const uintC = new BN('9101112');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.set = await EnumerableUintSetMock.new();
|
||||
});
|
||||
|
||||
shouldBehaveLikeSet(uintA, uintB, uintC);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user