Use hardhat-exposed to reduce the need for mocks (#3666)
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
@ -3,7 +3,8 @@ const { MAX_UINT256 } = constants;
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const SafeMathMock = artifacts.require('SafeMathMock');
|
||||
const SafeMath = artifacts.require('$SafeMath');
|
||||
const SafeMathMemoryCheck = artifacts.require('$SafeMathMemoryCheck');
|
||||
|
||||
function expectStruct (value, expected) {
|
||||
for (const key in expected) {
|
||||
@ -17,7 +18,7 @@ function expectStruct (value, expected) {
|
||||
|
||||
contract('SafeMath', function () {
|
||||
beforeEach(async function () {
|
||||
this.safeMath = await SafeMathMock.new();
|
||||
this.safeMath = await SafeMath.new();
|
||||
});
|
||||
|
||||
async function testCommutative (fn, lhs, rhs, expected, ...extra) {
|
||||
@ -46,14 +47,14 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
testCommutativeIterable(this.safeMath.tryAdd, a, b, { flag: true, value: a.add(b) });
|
||||
testCommutativeIterable(this.safeMath.$tryAdd, a, b, [ true, a.add(b) ]);
|
||||
});
|
||||
|
||||
it('reverts on addition overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('1');
|
||||
|
||||
testCommutativeIterable(this.safeMath.tryAdd, a, b, { flag: false, value: '0' });
|
||||
testCommutativeIterable(this.safeMath.$tryAdd, a, b, [ false, '0' ]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -62,14 +63,14 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
expectStruct(await this.safeMath.trySub(a, b), { flag: true, value: a.sub(b) });
|
||||
expectStruct(await this.safeMath.$trySub(a, b), [ true, a.sub(b) ]);
|
||||
});
|
||||
|
||||
it('reverts if subtraction result would be negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.trySub(a, b), { flag: false, value: '0' });
|
||||
expectStruct(await this.safeMath.$trySub(a, b), [ false, '0' ]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -78,21 +79,21 @@ contract('SafeMath', function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: true, value: a.mul(b) });
|
||||
testCommutativeIterable(this.safeMath.$tryMul, a, b, [ true, a.mul(b) ]);
|
||||
});
|
||||
|
||||
it('multiplies by zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: true, value: a.mul(b) });
|
||||
testCommutativeIterable(this.safeMath.$tryMul, a, b, [ true, a.mul(b) ]);
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('2');
|
||||
|
||||
testCommutativeIterable(this.safeMath.tryMul, a, b, { flag: false, value: '0' });
|
||||
testCommutativeIterable(this.safeMath.$tryMul, a, b, [ false, '0' ]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -101,28 +102,28 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [ true, a.div(b) ]);
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [ true, a.div(b) ]);
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.tryDiv(a, b), { flag: true, value: a.div(b) });
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [ true, a.div(b) ]);
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
expectStruct(await this.safeMath.tryDiv(a, b), { flag: false, value: '0' });
|
||||
expectStruct(await this.safeMath.$tryDiv(a, b), [ false, '0' ]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -132,28 +133,28 @@ contract('SafeMath', function () {
|
||||
const a = new BN('284');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [ true, a.mod(b) ]);
|
||||
});
|
||||
|
||||
it('when the dividend is equal to the divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [ true, a.mod(b) ]);
|
||||
});
|
||||
|
||||
it('when the dividend is larger than the divisor', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [ true, a.mod(b) ]);
|
||||
});
|
||||
|
||||
it('when the dividend is a multiple of the divisor', async function () {
|
||||
const a = new BN('17034'); // 17034 == 5678 * 3
|
||||
const b = new BN('5678');
|
||||
|
||||
expectStruct(await this.safeMath.tryMod(a, b), { flag: true, value: a.mod(b) });
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [ true, a.mod(b) ]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -161,7 +162,7 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
expectStruct(await this.safeMath.tryMod(a, b), { flag: false, value: '0' });
|
||||
expectStruct(await this.safeMath.$tryMod(a, b), [ false, '0' ]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -172,14 +173,14 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
await testCommutative(this.safeMath.doAdd, a, b, a.add(b));
|
||||
await testCommutative(this.safeMath.$add, a, b, a.add(b));
|
||||
});
|
||||
|
||||
it('reverts on addition overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('1');
|
||||
|
||||
await testFailsCommutative(this.safeMath.doAdd, a, b, undefined);
|
||||
await testFailsCommutative(this.safeMath.$add, a, b, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@ -188,14 +189,14 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
expect(await this.safeMath.doSub(a, b)).to.be.bignumber.equal(a.sub(b));
|
||||
expect(await this.safeMath.$sub(a, b)).to.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('reverts if subtraction result would be negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.doSub(a, b));
|
||||
await expectRevert.unspecified(this.safeMath.$sub(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
@ -204,21 +205,21 @@ contract('SafeMath', function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutative(this.safeMath.doMul, a, b, a.mul(b));
|
||||
await testCommutative(this.safeMath.$mul, a, b, a.mul(b));
|
||||
});
|
||||
|
||||
it('multiplies by zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
await testCommutative(this.safeMath.doMul, a, b, '0');
|
||||
await testCommutative(this.safeMath.$mul, a, b, '0');
|
||||
});
|
||||
|
||||
it('reverts on multiplication overflow', async function () {
|
||||
const a = MAX_UINT256;
|
||||
const b = new BN('2');
|
||||
|
||||
await testFailsCommutative(this.safeMath.doMul, a, b, undefined);
|
||||
await testFailsCommutative(this.safeMath.$mul, a, b, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@ -227,28 +228,28 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal(a.div(b));
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal(a.div(b));
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal('0');
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.doDiv(a, b)).to.be.bignumber.equal('1');
|
||||
expect(await this.safeMath.$div(a, b)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.doDiv(a, b));
|
||||
await expectRevert.unspecified(this.safeMath.$div(a, b));
|
||||
});
|
||||
});
|
||||
|
||||
@ -258,28 +259,28 @@ contract('SafeMath', function () {
|
||||
const a = new BN('284');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is equal to the divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is larger than the divisor', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is a multiple of the divisor', async function () {
|
||||
const a = new BN('17034'); // 17034 == 5678 * 3
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.doMod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.$mod(a, b)).to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
});
|
||||
|
||||
@ -287,7 +288,7 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert.unspecified(this.safeMath.doMod(a, b));
|
||||
await expectRevert.unspecified(this.safeMath.$mod(a, b));
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -298,14 +299,18 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('1234');
|
||||
|
||||
expect(await this.safeMath.subWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.sub(b));
|
||||
expect(await this.safeMath.methods['$sub(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal(a.sub(b));
|
||||
});
|
||||
|
||||
it('reverts if subtraction result would be negative', async function () {
|
||||
const a = new BN('1234');
|
||||
const b = new BN('5678');
|
||||
|
||||
await expectRevert(this.safeMath.subWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
|
||||
await expectRevert(
|
||||
this.safeMath.methods['$sub(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
'MyErrorMessage',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -314,28 +319,34 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.div(b));
|
||||
expect(await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal(a.div(b));
|
||||
});
|
||||
|
||||
it('divides zero correctly', async function () {
|
||||
const a = new BN('0');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal('0');
|
||||
expect(await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('returns complete number result on non-even division', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.divWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal('1');
|
||||
expect(await this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('reverts on division by zero', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert(this.safeMath.divWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
|
||||
await expectRevert(
|
||||
this.safeMath.methods['$div(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
'MyErrorMessage',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -345,28 +356,32 @@ contract('SafeMath', function () {
|
||||
const a = new BN('284');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is equal to the divisor', async function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is larger than the divisor', async function () {
|
||||
const a = new BN('7000');
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
|
||||
it('when the dividend is a multiple of the divisor', async function () {
|
||||
const a = new BN('17034'); // 17034 == 5678 * 3
|
||||
const b = new BN('5678');
|
||||
|
||||
expect(await this.safeMath.modWithMessage(a, b, 'MyErrorMessage')).to.be.bignumber.equal(a.mod(b));
|
||||
expect(await this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'))
|
||||
.to.be.bignumber.equal(a.mod(b));
|
||||
});
|
||||
});
|
||||
|
||||
@ -374,30 +389,37 @@ contract('SafeMath', function () {
|
||||
const a = new BN('5678');
|
||||
const b = new BN('0');
|
||||
|
||||
await expectRevert(this.safeMath.modWithMessage(a, b, 'MyErrorMessage'), 'MyErrorMessage');
|
||||
await expectRevert(
|
||||
this.safeMath.methods['$mod(uint256,uint256,string)'](a, b, 'MyErrorMessage'),
|
||||
'MyErrorMessage',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('memory leakage', function () {
|
||||
beforeEach(async function () {
|
||||
this.safeMathMemoryCheck = await SafeMathMemoryCheck.new();
|
||||
});
|
||||
|
||||
it('add', async function () {
|
||||
expect(await this.safeMath.addMemoryCheck()).to.be.bignumber.equal('0');
|
||||
expect(await this.safeMathMemoryCheck.$addMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('sub', async function () {
|
||||
expect(await this.safeMath.subMemoryCheck()).to.be.bignumber.equal('0');
|
||||
expect(await this.safeMathMemoryCheck.$subMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('mul', async function () {
|
||||
expect(await this.safeMath.mulMemoryCheck()).to.be.bignumber.equal('0');
|
||||
expect(await this.safeMathMemoryCheck.$mulMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('div', async function () {
|
||||
expect(await this.safeMath.divMemoryCheck()).to.be.bignumber.equal('0');
|
||||
expect(await this.safeMathMemoryCheck.$divMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('mod', async function () {
|
||||
expect(await this.safeMath.modMemoryCheck()).to.be.bignumber.equal('0');
|
||||
expect(await this.safeMathMemoryCheck.$modMemoryCheck()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user