Skip to content

Commit

Permalink
Add tests for improved coverage (OpenZeppelin#3448)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
  • Loading branch information
Amxx and frangio authored Jun 2, 2022
1 parent 04204b8 commit 35090c1
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 69 deletions.
4 changes: 4 additions & 0 deletions contracts/mocks/CheckpointsImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions test/crosschain/CrossChainEnabled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
10 changes: 10 additions & 0 deletions test/governance/compatibility/GovernorCompatibilityBravo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
40 changes: 40 additions & 0 deletions test/helpers/txpool.js
Original file line number Diff line number Diff line change
@@ -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,
};
35 changes: 1 addition & 34 deletions test/token/ERC20/extensions/ERC20Votes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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;

Expand Down
35 changes: 1 addition & 34 deletions test/token/ERC20/extensions/ERC20VotesComp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion test/token/common/ERC2981.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});
Expand Down
4 changes: 4 additions & 0 deletions test/utils/Base64.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});
});
});
15 changes: 15 additions & 0 deletions test/utils/Checkpoints.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 35090c1

Please sign in to comment.