Migrate utils-structs tests to ethersjs (#4748)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
@ -1,173 +1,146 @@
|
||||
const { expectEvent } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { ethers } = require('hardhat');
|
||||
|
||||
const zip = require('lodash.zip');
|
||||
const { expectRevertCustomError } = require('../../helpers/customError');
|
||||
const zip = (array1, array2) => array1.map((item, index) => [item, array2[index]]);
|
||||
|
||||
function shouldBehaveLikeMap(keys, values, zeroValue, methods, events) {
|
||||
const [keyA, keyB, keyC] = keys;
|
||||
const [valueA, valueB, valueC] = values;
|
||||
|
||||
async function expectMembersMatch(map, keys, values) {
|
||||
function shouldBehaveLikeMap(zeroValue, keyType, events) {
|
||||
async function expectMembersMatch(methods, keys, values) {
|
||||
expect(keys.length).to.equal(values.length);
|
||||
expect(await methods.length()).to.equal(keys.length);
|
||||
expect([...(await methods.keys())]).to.have.members(keys);
|
||||
|
||||
await Promise.all(keys.map(async key => expect(await methods.contains(map, key)).to.equal(true)));
|
||||
for (const [key, value] of zip(keys, values)) {
|
||||
expect(await methods.contains(key)).to.be.true;
|
||||
expect(await methods.get(key)).to.equal(value);
|
||||
}
|
||||
|
||||
expect(await methods.length(map)).to.bignumber.equal(keys.length.toString());
|
||||
|
||||
expect((await Promise.all(keys.map(key => methods.get(map, key)))).map(k => k.toString())).to.have.same.members(
|
||||
values.map(value => value.toString()),
|
||||
);
|
||||
|
||||
// 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 methods.at(map, index);
|
||||
return [entry[0].toString(), entry[1].toString()];
|
||||
}),
|
||||
),
|
||||
).to.have.same.deep.members(
|
||||
zip(
|
||||
keys.map(k => k.toString()),
|
||||
values.map(v => v.toString()),
|
||||
),
|
||||
);
|
||||
|
||||
// This also checks that both arrays have the same length
|
||||
expect((await methods.keys(map)).map(k => k.toString())).to.have.same.members(keys.map(key => key.toString()));
|
||||
expect(await Promise.all(keys.map((_, index) => methods.at(index)))).to.have.deep.members(zip(keys, values));
|
||||
}
|
||||
|
||||
it('starts empty', async function () {
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(false);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
await expectMembersMatch(this.methods, [], []);
|
||||
});
|
||||
|
||||
describe('set', function () {
|
||||
it('adds a key', async function () {
|
||||
const receipt = await methods.set(this.map, keyA, valueA);
|
||||
expectEvent(receipt, events.setReturn, { ret0: true });
|
||||
await expect(this.methods.set(this.keyA, this.valueA)).to.emit(this.mock, events.setReturn).withArgs(true);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [valueA]);
|
||||
await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
|
||||
});
|
||||
|
||||
it('adds several keys', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyB, valueB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyB], [valueA, valueB]);
|
||||
expect(await methods.contains(this.map, keyC)).to.equal(false);
|
||||
await expectMembersMatch(this.methods, [this.keyA, this.keyB], [this.valueA, this.valueB]);
|
||||
expect(await this.methods.contains(this.keyC)).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false when adding keys already in the set', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
|
||||
const receipt = await methods.set(this.map, keyA, valueA);
|
||||
expectEvent(receipt, events.setReturn, { ret0: false });
|
||||
await expect(this.methods.set(this.keyA, this.valueA)).to.emit(this.mock, events.setReturn).withArgs(false);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [valueA]);
|
||||
await expectMembersMatch(this.methods, [this.keyA], [this.valueA]);
|
||||
});
|
||||
|
||||
it('updates values for keys already in the set', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyA, valueB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyA, this.valueB);
|
||||
|
||||
await expectMembersMatch(this.map, [keyA], [valueB]);
|
||||
await expectMembersMatch(this.methods, [this.keyA], [this.valueB]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', function () {
|
||||
it('removes added keys', async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
|
||||
const receipt = await methods.remove(this.map, keyA);
|
||||
expectEvent(receipt, events.removeReturn, { ret0: true });
|
||||
await expect(this.methods.remove(this.keyA)).to.emit(this.mock, events.removeReturn).withArgs(true);
|
||||
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(false);
|
||||
await expectMembersMatch(this.map, [], []);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
await expectMembersMatch(this.methods, [], []);
|
||||
});
|
||||
|
||||
it('returns false when removing keys not in the set', async function () {
|
||||
const receipt = await methods.remove(this.map, keyA);
|
||||
expectEvent(receipt, events.removeReturn, { ret0: false });
|
||||
await expect(await this.methods.remove(this.keyA))
|
||||
.to.emit(this.mock, events.removeReturn)
|
||||
.withArgs(false);
|
||||
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(false);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.false;
|
||||
});
|
||||
|
||||
it('adds and removes multiple keys', async function () {
|
||||
// []
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyC, valueC);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyC, this.valueC);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await methods.remove(this.map, keyA);
|
||||
await methods.remove(this.map, keyB);
|
||||
await this.methods.remove(this.keyA);
|
||||
await this.methods.remove(this.keyB);
|
||||
|
||||
// [C]
|
||||
|
||||
await methods.set(this.map, keyB, valueB);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
|
||||
// [C, B]
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.remove(this.map, keyC);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.remove(this.keyC);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.set(this.map, keyB, valueB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.set(this.keyB, this.valueB);
|
||||
|
||||
// [A, B]
|
||||
|
||||
await methods.set(this.map, keyC, valueC);
|
||||
await methods.remove(this.map, keyA);
|
||||
await this.methods.set(this.keyC, this.valueC);
|
||||
await this.methods.remove(this.keyA);
|
||||
|
||||
// [B, C]
|
||||
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await methods.remove(this.map, keyB);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
await this.methods.remove(this.keyB);
|
||||
|
||||
// [A, C]
|
||||
|
||||
await expectMembersMatch(this.map, [keyA, keyC], [valueA, valueC]);
|
||||
await expectMembersMatch(this.methods, [this.keyA, this.keyC], [this.valueA, this.valueC]);
|
||||
|
||||
expect(await methods.contains(this.map, keyA)).to.equal(true);
|
||||
expect(await methods.contains(this.map, keyB)).to.equal(false);
|
||||
expect(await methods.contains(this.map, keyC)).to.equal(true);
|
||||
expect(await this.methods.contains(this.keyA)).to.be.true;
|
||||
expect(await this.methods.contains(this.keyB)).to.be.false;
|
||||
expect(await this.methods.contains(this.keyC)).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('read', function () {
|
||||
beforeEach(async function () {
|
||||
await methods.set(this.map, keyA, valueA);
|
||||
await this.methods.set(this.keyA, this.valueA);
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
it('existing value', async function () {
|
||||
expect(await methods.get(this.map, keyA).then(r => r.toString())).to.be.equal(valueA.toString());
|
||||
expect(await this.methods.get(this.keyA)).to.be.equal(this.valueA);
|
||||
});
|
||||
|
||||
it('missing value', async function () {
|
||||
const key = web3.utils.toHex(keyB);
|
||||
await expectRevertCustomError(methods.get(this.map, keyB), 'EnumerableMapNonexistentKey', [
|
||||
key.length == 66 ? key : web3.utils.padLeft(key, 64, '0'),
|
||||
]);
|
||||
await expect(this.methods.get(this.keyB))
|
||||
.to.be.revertedWithCustomError(this.mock, 'EnumerableMapNonexistentKey')
|
||||
.withArgs(ethers.AbiCoder.defaultAbiCoder().encode([keyType], [this.keyB]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('tryGet', function () {
|
||||
it('existing value', async function () {
|
||||
const result = await methods.tryGet(this.map, keyA);
|
||||
expect(result['0']).to.be.equal(true);
|
||||
expect(result['1'].toString()).to.be.equal(valueA.toString());
|
||||
expect(await this.methods.tryGet(this.keyA)).to.have.ordered.members([true, this.valueA]);
|
||||
});
|
||||
|
||||
it('missing value', async function () {
|
||||
const result = await methods.tryGet(this.map, keyB);
|
||||
expect(result['0']).to.be.equal(false);
|
||||
expect(result['1'].toString()).to.be.equal(zeroValue.toString());
|
||||
expect(await this.methods.tryGet(this.keyB)).to.have.ordered.members([false, zeroValue]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user