Use hardhat-exposed to reduce the need for mocks (#3666)

Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
Hadrien Croubois
2023-01-03 15:38:13 +01:00
committed by GitHub
parent a81b0d0b21
commit c1d9da4052
190 changed files with 2297 additions and 4311 deletions

View File

@ -3,21 +3,27 @@ const { expect } = require('chai');
const zip = require('lodash.zip');
function shouldBehaveLikeMap (keys, values, zeroValue) {
const [keyA, keyB, keyC] = keys;
const [valueA, valueB, valueC] = values;
function shouldBehaveLikeMap (
keys,
values,
zeroValue,
methods,
events,
) {
const [ keyA, keyB, keyC ] = keys;
const [ valueA, valueB, valueC ] = values;
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 methods.contains(map, key)).to.equal(true),
));
expect(await map.length()).to.bignumber.equal(keys.length.toString());
expect(await methods.length(map)).to.bignumber.equal(keys.length.toString());
expect(
(await Promise.all(keys.map(key => map.get(key)))).map(k => k.toString()),
(await Promise.all(keys.map(key => methods.get(map, key)))).map(k => k.toString()),
).to.have.same.members(
values.map(value => value.toString()),
);
@ -25,48 +31,47 @@ function shouldBehaveLikeMap (keys, values, zeroValue) {
// 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.toString()];
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())),
);
}
it('starts empty', async function () {
expect(await this.map.contains(keyA)).to.equal(false);
expect(await methods.contains(this.map, keyA)).to.equal(false);
await expectMembersMatch(this.map, [], []);
});
describe('set', function () {
it('adds a key', async function () {
const receipt = await this.map.set(keyA, valueA);
expectEvent(receipt, 'OperationResult', { result: true });
const receipt = await methods.set(this.map, keyA, valueA);
expectEvent(receipt, events.setReturn, { ret0: true });
await expectMembersMatch(this.map, [keyA], [valueA]);
});
it('adds several keys', async function () {
await this.map.set(keyA, valueA);
await this.map.set(keyB, valueB);
await methods.set(this.map, keyA, valueA);
await methods.set(this.map, keyB, valueB);
await expectMembersMatch(this.map, [keyA, keyB], [valueA, valueB]);
expect(await this.map.contains(keyC)).to.equal(false);
expect(await methods.contains(this.map, keyC)).to.equal(false);
});
it('returns false when adding keys already in the set', async function () {
await this.map.set(keyA, valueA);
await methods.set(this.map, keyA, valueA);
const receipt = (await this.map.set(keyA, valueA));
expectEvent(receipt, 'OperationResult', { result: false });
const receipt = await methods.set(this.map, keyA, valueA);
expectEvent(receipt, events.setReturn, { ret0: false });
await expectMembersMatch(this.map, [keyA], [valueA]);
});
it('updates values for keys already in the set', async function () {
await this.map.set(keyA, valueA);
await this.map.set(keyA, valueB);
await methods.set(this.map, keyA, valueA);
await methods.set(this.map, keyA, valueB);
await expectMembersMatch(this.map, [keyA], [valueB]);
});
@ -74,101 +79,108 @@ function shouldBehaveLikeMap (keys, values, zeroValue) {
describe('remove', function () {
it('removes added keys', async function () {
await this.map.set(keyA, valueA);
await methods.set(this.map, keyA, valueA);
const receipt = await this.map.remove(keyA);
expectEvent(receipt, 'OperationResult', { result: true });
const receipt = await methods.remove(this.map, keyA);
expectEvent(receipt, events.removeReturn, { ret0: true });
expect(await this.map.contains(keyA)).to.equal(false);
expect(await methods.contains(this.map, 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 });
const receipt = await methods.remove(this.map, keyA);
expectEvent(receipt, events.removeReturn, { ret0: false });
expect(await this.map.contains(keyA)).to.equal(false);
expect(await methods.contains(this.map, keyA)).to.equal(false);
});
it('adds and removes multiple keys', async function () {
// []
await this.map.set(keyA, valueA);
await this.map.set(keyC, valueC);
await methods.set(this.map, keyA, valueA);
await methods.set(this.map, keyC, valueC);
// [A, C]
await this.map.remove(keyA);
await this.map.remove(keyB);
await methods.remove(this.map, keyA);
await methods.remove(this.map, keyB);
// [C]
await this.map.set(keyB, valueB);
await methods.set(this.map, keyB, valueB);
// [C, B]
await this.map.set(keyA, valueA);
await this.map.remove(keyC);
await methods.set(this.map, keyA, valueA);
await methods.remove(this.map, keyC);
// [A, B]
await this.map.set(keyA, valueA);
await this.map.set(keyB, valueB);
await methods.set(this.map, keyA, valueA);
await methods.set(this.map, keyB, valueB);
// [A, B]
await this.map.set(keyC, valueC);
await this.map.remove(keyA);
await methods.set(this.map, keyC, valueC);
await methods.remove(this.map, keyA);
// [B, C]
await this.map.set(keyA, valueA);
await this.map.remove(keyB);
await methods.set(this.map, keyA, valueA);
await methods.remove(this.map, keyB);
// [A, C]
await expectMembersMatch(this.map, [keyA, keyC], [valueA, valueC]);
expect(await this.map.contains(keyB)).to.equal(false);
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);
});
});
describe('read', function () {
beforeEach(async function () {
await this.map.set(keyA, valueA);
await methods.set(this.map, keyA, valueA);
});
describe('get', function () {
it('existing value', async function () {
expect(
(await this.map.get(keyA)).toString(),
await methods.get(this.map, keyA).then(r => r.toString()),
).to.be.equal(valueA.toString());
});
it('missing value', async function () {
await expectRevert(this.map.get(keyB), 'EnumerableMap: nonexistent key');
await expectRevert(
methods.get(this.map, keyB),
'EnumerableMap: nonexistent key',
);
});
});
describe('get with message', function () {
it('existing value', async function () {
expect(
(await this.map.getWithMessage(keyA, 'custom error string'))
.toString(),
await methods.getWithMessage(this.map, keyA, 'custom error string').then(r => r.toString()),
).to.be.equal(valueA.toString());
});
it('missing value', async function () {
await expectRevert(this.map.getWithMessage(keyB, 'custom error string'), 'custom error string');
await expectRevert(
methods.getWithMessage(this.map, keyB, 'custom error string'),
'custom error string',
);
});
});
describe('tryGet', function () {
it('existing value', async function () {
const result = await this.map.tryGet(keyA);
const result = await methods.tryGet(this.map, keyA);
expect(result['0']).to.be.equal(true);
expect(result['1'].toString()).to.be.equal(valueA.toString());
});
it('missing value', async function () {
const result = await this.map.tryGet(keyB);
const result = await methods.tryGet(this.map, keyB);
expect(result['0']).to.be.equal(false);
expect(result['1'].toString()).to.be.equal(zeroValue.toString());
});