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,14 +1,12 @@
const { expectEvent } = require('@openzeppelin/test-helpers');
const { expectRevertCustomError } = require('../../helpers/customError');
const Bytes32DequeMock = artifacts.require('Bytes32DequeMock');
const DoubleEndedQueue = artifacts.require('$DoubleEndedQueue');
/** Rebuild the content of the deque as a JS array. */
async function getContent (deque) {
const length = await deque.length().then(bn => bn.toNumber());
const values = await Promise.all(Array(length).fill().map((_, i) => deque.at(i)));
return values;
}
const getContent = (deque) => deque.$length(0).then(bn =>
Promise.all(Array(bn.toNumber()).fill().map((_, i) => deque.$at(0, i))),
);
contract('DoubleEndedQueue', function () {
const bytesA = '0xdeadbeef'.padEnd(66, '0');
@ -17,53 +15,53 @@ contract('DoubleEndedQueue', function () {
const bytesD = '0x171717'.padEnd(66, '0');
beforeEach(async function () {
this.deque = await Bytes32DequeMock.new();
this.deque = await DoubleEndedQueue.new();
});
describe('when empty', function () {
it('getters', async function () {
expect(await this.deque.empty()).to.be.equal(true);
expect(await this.deque.$empty(0)).to.be.equal(true);
expect(await getContent(this.deque)).to.have.ordered.members([]);
});
it('reverts on accesses', async function () {
await expectRevertCustomError(this.deque.popBack(), 'Empty()');
await expectRevertCustomError(this.deque.popFront(), 'Empty()');
await expectRevertCustomError(this.deque.back(), 'Empty()');
await expectRevertCustomError(this.deque.front(), 'Empty()');
await expectRevertCustomError(this.deque.$popBack(0), 'Empty()');
await expectRevertCustomError(this.deque.$popFront(0), 'Empty()');
await expectRevertCustomError(this.deque.$back(0), 'Empty()');
await expectRevertCustomError(this.deque.$front(0), 'Empty()');
});
});
describe('when not empty', function () {
beforeEach(async function () {
await this.deque.pushBack(bytesB);
await this.deque.pushFront(bytesA);
await this.deque.pushBack(bytesC);
await this.deque.$pushBack(0, bytesB);
await this.deque.$pushFront(0, bytesA);
await this.deque.$pushBack(0, bytesC);
this.content = [ bytesA, bytesB, bytesC ];
});
it('getters', async function () {
expect(await this.deque.empty()).to.be.equal(false);
expect(await this.deque.length()).to.be.bignumber.equal(this.content.length.toString());
expect(await this.deque.front()).to.be.equal(this.content[0]);
expect(await this.deque.back()).to.be.equal(this.content[this.content.length - 1]);
expect(await this.deque.$empty(0)).to.be.equal(false);
expect(await this.deque.$length(0)).to.be.bignumber.equal(this.content.length.toString());
expect(await this.deque.$front(0)).to.be.equal(this.content[0]);
expect(await this.deque.$back(0)).to.be.equal(this.content[this.content.length - 1]);
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
});
it('out of bounds access', async function () {
await expectRevertCustomError(this.deque.at(this.content.length), 'OutOfBounds()');
await expectRevertCustomError(this.deque.$at(0, this.content.length), 'OutOfBounds()');
});
describe('push', function () {
it('front', async function () {
await this.deque.pushFront(bytesD);
await this.deque.$pushFront(0, bytesD);
this.content.unshift(bytesD); // add element at the beginning
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
});
it('back', async function () {
await this.deque.pushBack(bytesD);
await this.deque.$pushBack(0, bytesD);
this.content.push(bytesD); // add element at the end
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
@ -73,23 +71,23 @@ contract('DoubleEndedQueue', function () {
describe('pop', function () {
it('front', async function () {
const value = this.content.shift(); // remove first element
expectEvent(await this.deque.popFront(), 'OperationResult', { value });
expectEvent(await this.deque.$popFront(0), 'return$popFront', { value });
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
});
it('back', async function () {
const value = this.content.pop(); // remove last element
expectEvent(await this.deque.popBack(), 'OperationResult', { value });
expectEvent(await this.deque.$popBack(0), 'return$popBack', { value });
expect(await getContent(this.deque)).to.have.ordered.members(this.content);
});
});
it('clear', async function () {
await this.deque.clear();
await this.deque.$clear(0);
expect(await this.deque.empty()).to.be.equal(true);
expect(await this.deque.$empty(0)).to.be.equal(true);
expect(await getContent(this.deque)).to.have.ordered.members([]);
});
});