Add tests for improved coverage (#3448)

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
Hadrien Croubois
2022-06-03 01:01:55 +02:00
committed by GitHub
parent 04204b8fb9
commit 35090c1bf1
9 changed files with 81 additions and 69 deletions

View File

@ -20,4 +20,8 @@ contract CheckpointsImpl {
function push(uint256 value) public returns (uint256, uint256) {
return _totalCheckpoints.push(value);
}
function length() public view returns (uint256) {
return _totalCheckpoints._checkpoints.length;
}
}

View File

@ -17,6 +17,11 @@ function shouldBehaveLikeReceiver (sender = randomAddress()) {
this.receiver.crossChainRestricted(),
'NotCrossChainCall()',
);
await expectRevertCustomError(
this.receiver.crossChainOwnerRestricted(),
'NotCrossChainCall()',
);
});
it('should restrict to cross-chain call from a invalid sender', async function () {

View File

@ -168,6 +168,16 @@ contract('GovernorCompatibilityBravo', function (accounts) {
);
});
it('double voting is forbiden', async function () {
await this.helper.propose({ from: proposer });
await this.helper.waitForSnapshot();
await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 });
await expectRevert(
this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }),
'GovernorCompatibilityBravo: vote already cast',
);
});
it('with function selector and arguments', async function () {
const target = this.receiver.address;
this.helper.setProposal([

40
test/helpers/txpool.js Normal file
View File

@ -0,0 +1,40 @@
const { network } = require('hardhat');
const { promisify } = require('util');
const queue = promisify(setImmediate);
async function countPendingTransactions () {
return parseInt(
await network.provider.send('eth_getBlockTransactionCountByNumber', ['pending']),
);
}
async function batchInBlock (txs) {
try {
// disable auto-mining
await network.provider.send('evm_setAutomine', [false]);
// send all transactions
const promises = txs.map(fn => fn());
// wait for node to have all pending transactions
while (txs.length > await countPendingTransactions()) {
await queue();
}
// mine one block
await network.provider.send('evm_mine');
// fetch receipts
const receipts = await Promise.all(promises);
// Sanity check, all tx should be in the same block
const minedBlocks = new Set(receipts.map(({ receipt }) => receipt.blockNumber));
expect(minedBlocks.size).to.equal(1);
return receipts;
} finally {
// enable auto-mining
await network.provider.send('evm_setAutomine', [true]);
}
}
module.exports = {
countPendingTransactions,
batchInBlock,
};

View File

@ -8,11 +8,9 @@ const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;
const { promisify } = require('util');
const queue = promisify(setImmediate);
const ERC20VotesMock = artifacts.require('ERC20VotesMock');
const { batchInBlock } = require('../../../helpers/txpool');
const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712');
const Delegation = [
@ -21,37 +19,6 @@ const Delegation = [
{ name: 'expiry', type: 'uint256' },
];
async function countPendingTransactions() {
return parseInt(
await network.provider.send('eth_getBlockTransactionCountByNumber', ['pending'])
);
}
async function batchInBlock (txs) {
try {
// disable auto-mining
await network.provider.send('evm_setAutomine', [false]);
// send all transactions
const promises = txs.map(fn => fn());
// wait for node to have all pending transactions
while (txs.length > await countPendingTransactions()) {
await queue();
}
// mine one block
await network.provider.send('evm_mine');
// fetch receipts
const receipts = await Promise.all(promises);
// Sanity check, all tx should be in the same block
const minedBlocks = new Set(receipts.map(({ receipt }) => receipt.blockNumber));
expect(minedBlocks.size).to.equal(1);
return receipts;
} finally {
// enable auto-mining
await network.provider.send('evm_setAutomine', [true]);
}
}
contract('ERC20Votes', function (accounts) {
const [ holder, recipient, holderDelegatee, recipientDelegatee, other1, other2 ] = accounts;

View File

@ -8,11 +8,9 @@ const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;
const { promisify } = require('util');
const queue = promisify(setImmediate);
const ERC20VotesCompMock = artifacts.require('ERC20VotesCompMock');
const { batchInBlock } = require('../../../helpers/txpool');
const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712');
const Delegation = [
@ -21,37 +19,6 @@ const Delegation = [
{ name: 'expiry', type: 'uint256' },
];
async function countPendingTransactions() {
return parseInt(
await network.provider.send('eth_getBlockTransactionCountByNumber', ['pending'])
);
}
async function batchInBlock (txs) {
try {
// disable auto-mining
await network.provider.send('evm_setAutomine', [false]);
// send all transactions
const promises = txs.map(fn => fn());
// wait for node to have all pending transactions
while (txs.length > await countPendingTransactions()) {
await queue();
}
// mine one block
await network.provider.send('evm_mine');
// fetch receipts
const receipts = await Promise.all(promises);
// Sanity check, all tx should be in the same block
const minedBlocks = new Set(receipts.map(({ receipt }) => receipt.blockNumber));
expect(minedBlocks.size).to.equal(1);
return receipts;
} finally {
// enable auto-mining
await network.provider.send('evm_setAutomine', [true]);
}
}
contract('ERC20VotesComp', function (accounts) {
const [ holder, recipient, holderDelegatee, recipientDelegatee, other1, other2 ] = accounts;

View File

@ -66,7 +66,7 @@ function shouldBehaveLikeERC2981 () {
);
await expectRevert(
this.token.setTokenRoyalty(this.tokenId1, this.account1, new BN('11000')),
this.token.setDefaultRoyalty(this.account1, new BN('11000')),
'ERC2981: royalty fee will exceed salePrice',
);
});

View File

@ -25,5 +25,9 @@ contract('Strings', function () {
const input = web3.utils.asciiToHex(TEST_MESSAGE);
expect(await this.base64.encode(input)).to.equal('dGVzdDEy');
});
it('empty bytes', async function () {
expect(await this.base64.encode([])).to.equal('');
});
});
});

View File

@ -2,6 +2,8 @@ const { expectRevert, time } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { batchInBlock } = require('../helpers/txpool');
const CheckpointsImpl = artifacts.require('CheckpointsImpl');
contract('Checkpoints', function (accounts) {
@ -55,5 +57,18 @@ contract('Checkpoints', function (accounts) {
'Checkpoints: block not yet mined',
);
});
it('multiple checkpoints in the same block', async function () {
const lengthBefore = await this.checkpoint.length();
await batchInBlock([
() => this.checkpoint.push(8, { gas: 100000 }),
() => this.checkpoint.push(9, { gas: 100000 }),
() => this.checkpoint.push(10, { gas: 100000 }),
]);
const lengthAfter = await this.checkpoint.length();
expect(lengthAfter.toNumber()).to.be.equal(lengthBefore.toNumber() + 1);
expect(await this.checkpoint.latest()).to.be.bignumber.equal('10');
});
});
});