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

@ -1,24 +1,30 @@
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
function shouldBehaveLikeSet (valueA, valueB, valueC) {
function shouldBehaveLikeSet (
values,
methods,
events,
) {
const [ valueA, valueB, valueC ] = values;
async function expectMembersMatch (set, values) {
const contains = await Promise.all(values.map(value => set.contains(value)));
const contains = await Promise.all(values.map(value => methods.contains(set, value)));
expect(contains.every(Boolean)).to.be.equal(true);
const length = await set.length();
const length = await methods.length(set);
expect(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)
const indexedValues = await Promise.all(Array(values.length).fill().map((_, index) => set.at(index)));
const indexedValues = await Promise.all(Array(values.length).fill().map((_, index) => methods.at(set, index)));
expect(
indexedValues.map(v => v.toString()),
).to.have.same.members(
values.map(v => v.toString()),
);
const returnedValues = await set.values();
const returnedValues = await methods.values(set);
expect(
returnedValues.map(v => v.toString()),
).to.have.same.members(
@ -27,32 +33,32 @@ function shouldBehaveLikeSet (valueA, valueB, valueC) {
}
it('starts empty', async function () {
expect(await this.set.contains(valueA)).to.equal(false);
expect(await methods.contains(this.set, 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 });
const receipt = await methods.add(this.set, valueA);
expectEvent(receipt, events.addReturn, { ret0: true });
await expectMembersMatch(this.set, [valueA]);
});
it('adds several values', async function () {
await this.set.add(valueA);
await this.set.add(valueB);
await methods.add(this.set, valueA);
await methods.add(this.set, valueB);
await expectMembersMatch(this.set, [valueA, valueB]);
expect(await this.set.contains(valueC)).to.equal(false);
expect(await methods.contains(this.set, valueC)).to.equal(false);
});
it('returns false when adding values already in the set', async function () {
await this.set.add(valueA);
await methods.add(this.set, valueA);
const receipt = (await this.set.add(valueA));
expectEvent(receipt, 'OperationResult', { result: false });
const receipt = await methods.add(this.set, valueA);
expectEvent(receipt, events.addReturn, { ret0: false });
await expectMembersMatch(this.set, [valueA]);
});
@ -60,68 +66,68 @@ function shouldBehaveLikeSet (valueA, valueB, valueC) {
describe('at', function () {
it('reverts when retrieving non-existent elements', async function () {
await expectRevert.unspecified(this.set.at(0));
await expectRevert.unspecified(methods.at(this.set, 0));
});
});
describe('remove', function () {
it('removes added values', async function () {
await this.set.add(valueA);
await methods.add(this.set, valueA);
const receipt = await this.set.remove(valueA);
expectEvent(receipt, 'OperationResult', { result: true });
const receipt = await methods.remove(this.set, valueA);
expectEvent(receipt, events.removeReturn, { ret0: true });
expect(await this.set.contains(valueA)).to.equal(false);
expect(await methods.contains(this.set, 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 });
const receipt = await methods.remove(this.set, valueA);
expectEvent(receipt, events.removeReturn, { ret0: false });
expect(await this.set.contains(valueA)).to.equal(false);
expect(await methods.contains(this.set, valueA)).to.equal(false);
});
it('adds and removes multiple values', async function () {
// []
await this.set.add(valueA);
await this.set.add(valueC);
await methods.add(this.set, valueA);
await methods.add(this.set, valueC);
// [A, C]
await this.set.remove(valueA);
await this.set.remove(valueB);
await methods.remove(this.set, valueA);
await methods.remove(this.set, valueB);
// [C]
await this.set.add(valueB);
await methods.add(this.set, valueB);
// [C, B]
await this.set.add(valueA);
await this.set.remove(valueC);
await methods.add(this.set, valueA);
await methods.remove(this.set, valueC);
// [A, B]
await this.set.add(valueA);
await this.set.add(valueB);
await methods.add(this.set, valueA);
await methods.add(this.set, valueB);
// [A, B]
await this.set.add(valueC);
await this.set.remove(valueA);
await methods.add(this.set, valueC);
await methods.remove(this.set, valueA);
// [B, C]
await this.set.add(valueA);
await this.set.remove(valueB);
await methods.add(this.set, valueA);
await methods.remove(this.set, valueB);
// [A, C]
await expectMembersMatch(this.set, [valueA, valueC]);
expect(await this.set.contains(valueB)).to.equal(false);
expect(await methods.contains(this.set, valueB)).to.equal(false);
});
});
}