Add tests for improved coverage (#3448)

Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
pull/3452/head
Hadrien Croubois 3 years ago committed by GitHub
parent 04204b8fb9
commit 35090c1bf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      contracts/mocks/CheckpointsImpl.sol
  2. 5
      test/crosschain/CrossChainEnabled.test.js
  3. 10
      test/governance/compatibility/GovernorCompatibilityBravo.test.js
  4. 40
      test/helpers/txpool.js
  5. 35
      test/token/ERC20/extensions/ERC20Votes.test.js
  6. 35
      test/token/ERC20/extensions/ERC20VotesComp.test.js
  7. 2
      test/token/common/ERC2981.behavior.js
  8. 4
      test/utils/Base64.test.js
  9. 15
      test/utils/Checkpoints.test.js

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

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

@ -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 () { it('with function selector and arguments', async function () {
const target = this.receiver.address; const target = this.receiver.address;
this.helper.setProposal([ this.helper.setProposal([

@ -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,
};

@ -8,11 +8,9 @@ const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util'); const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default; const Wallet = require('ethereumjs-wallet').default;
const { promisify } = require('util');
const queue = promisify(setImmediate);
const ERC20VotesMock = artifacts.require('ERC20VotesMock'); const ERC20VotesMock = artifacts.require('ERC20VotesMock');
const { batchInBlock } = require('../../../helpers/txpool');
const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712'); const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712');
const Delegation = [ const Delegation = [
@ -21,37 +19,6 @@ const Delegation = [
{ name: 'expiry', type: 'uint256' }, { 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) { contract('ERC20Votes', function (accounts) {
const [ holder, recipient, holderDelegatee, recipientDelegatee, other1, other2 ] = accounts; const [ holder, recipient, holderDelegatee, recipientDelegatee, other1, other2 ] = accounts;

@ -8,11 +8,9 @@ const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util'); const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default; const Wallet = require('ethereumjs-wallet').default;
const { promisify } = require('util');
const queue = promisify(setImmediate);
const ERC20VotesCompMock = artifacts.require('ERC20VotesCompMock'); const ERC20VotesCompMock = artifacts.require('ERC20VotesCompMock');
const { batchInBlock } = require('../../../helpers/txpool');
const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712'); const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712');
const Delegation = [ const Delegation = [
@ -21,37 +19,6 @@ const Delegation = [
{ name: 'expiry', type: 'uint256' }, { 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) { contract('ERC20VotesComp', function (accounts) {
const [ holder, recipient, holderDelegatee, recipientDelegatee, other1, other2 ] = accounts; const [ holder, recipient, holderDelegatee, recipientDelegatee, other1, other2 ] = accounts;

@ -66,7 +66,7 @@ function shouldBehaveLikeERC2981 () {
); );
await expectRevert( 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', 'ERC2981: royalty fee will exceed salePrice',
); );
}); });

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

@ -2,6 +2,8 @@ const { expectRevert, time } = require('@openzeppelin/test-helpers');
const { expect } = require('chai'); const { expect } = require('chai');
const { batchInBlock } = require('../helpers/txpool');
const CheckpointsImpl = artifacts.require('CheckpointsImpl'); const CheckpointsImpl = artifacts.require('CheckpointsImpl');
contract('Checkpoints', function (accounts) { contract('Checkpoints', function (accounts) {
@ -55,5 +57,18 @@ contract('Checkpoints', function (accounts) {
'Checkpoints: block not yet mined', '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');
});
}); });
}); });

Loading…
Cancel
Save