From b9793abec6fe21590ac472386fa4b7d1ecdab977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Mon, 26 Nov 2018 15:41:39 -0300 Subject: [PATCH] balanceDifference tests and application (#1514) * Added balanceDifference tests. * Added balanceDifference tests. * Now using balanceDifference in all (most) tests. * Fixed typo. --- test/crowdsale/AllowanceCrowdsale.test.js | 9 +++--- test/crowdsale/Crowdsale.test.js | 16 +++++----- test/crowdsale/MintedCrowdsale.behavior.js | 9 +++--- test/crowdsale/RefundableCrowdsale.test.js | 8 ++--- test/examples/SampleCrowdsale.test.js | 31 ++++++++----------- .../{balanceDiff.js => balanceDifference.js} | 4 +-- test/helpers/test/balanceDifference.test.js | 22 +++++++++++++ test/payment/PullPayment.test.js | 15 ++++----- test/payment/escrow/Escrow.behavior.js | 13 +++----- test/payment/escrow/RefundEscrow.test.js | 18 +++++------ 10 files changed, 74 insertions(+), 71 deletions(-) rename test/helpers/{balanceDiff.js => balanceDifference.js} (70%) create mode 100644 test/helpers/test/balanceDifference.test.js diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index 413d25544..5f4c0ad30 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -1,7 +1,7 @@ const expectEvent = require('../helpers/expectEvent'); const { ether } = require('../helpers/ether'); const shouldFail = require('../helpers/shouldFail'); -const { ethGetBalance } = require('../helpers/web3'); +const { balanceDifference } = require('../helpers/balanceDifference'); const { ZERO_ADDRESS } = require('../helpers/constants'); const BigNumber = web3.BigNumber; @@ -56,10 +56,9 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW }); it('should forward funds to wallet', async function () { - const pre = await ethGetBalance(wallet); - await this.crowdsale.sendTransaction({ value, from: investor }); - const post = await ethGetBalance(wallet); - post.minus(pre).should.be.bignumber.equal(value); + (await balanceDifference(wallet, () => + this.crowdsale.sendTransaction({ value, from: investor })) + ).should.be.bignumber.equal(value); }); }); diff --git a/test/crowdsale/Crowdsale.test.js b/test/crowdsale/Crowdsale.test.js index b7528e4e9..c2646db9b 100644 --- a/test/crowdsale/Crowdsale.test.js +++ b/test/crowdsale/Crowdsale.test.js @@ -1,7 +1,7 @@ const expectEvent = require('../helpers/expectEvent'); const shouldFail = require('../helpers/shouldFail'); +const { balanceDifference } = require('../helpers/balanceDifference'); const { ether } = require('../helpers/ether'); -const { ethGetBalance } = require('../helpers/web3'); const { ZERO_ADDRESS } = require('../helpers/constants'); const BigNumber = web3.BigNumber; @@ -97,10 +97,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }); it('should forward funds to wallet', async function () { - const pre = await ethGetBalance(wallet); - await this.crowdsale.sendTransaction({ value, from: investor }); - const post = await ethGetBalance(wallet); - post.minus(pre).should.be.bignumber.equal(value); + (await balanceDifference(wallet, () => + this.crowdsale.sendTransaction({ value, from: investor })) + ).should.be.bignumber.equal(value); }); }); @@ -121,10 +120,9 @@ contract('Crowdsale', function ([_, investor, wallet, purchaser]) { }); it('should forward funds to wallet', async function () { - const pre = await ethGetBalance(wallet); - await this.crowdsale.buyTokens(investor, { value, from: purchaser }); - const post = await ethGetBalance(wallet); - post.minus(pre).should.be.bignumber.equal(value); + (await balanceDifference(wallet, () => + this.crowdsale.buyTokens(investor, { value, from: purchaser })) + ).should.be.bignumber.equal(value); }); }); }); diff --git a/test/crowdsale/MintedCrowdsale.behavior.js b/test/crowdsale/MintedCrowdsale.behavior.js index ab8dc8259..e58d54aa6 100644 --- a/test/crowdsale/MintedCrowdsale.behavior.js +++ b/test/crowdsale/MintedCrowdsale.behavior.js @@ -1,5 +1,5 @@ const expectEvent = require('../helpers/expectEvent'); -const { ethGetBalance } = require('../helpers/web3'); +const { balanceDifference } = require('../helpers/balanceDifference'); const BigNumber = web3.BigNumber; @@ -35,10 +35,9 @@ function shouldBehaveLikeMintedCrowdsale ([_, investor, wallet, purchaser], rate }); it('should forward funds to wallet', async function () { - const pre = await ethGetBalance(wallet); - await this.crowdsale.sendTransaction({ value, from: investor }); - const post = await ethGetBalance(wallet); - post.minus(pre).should.be.bignumber.equal(value); + (await balanceDifference(wallet, () => + this.crowdsale.sendTransaction({ value, from: investor })) + ).should.be.bignumber.equal(value); }); }); }); diff --git a/test/crowdsale/RefundableCrowdsale.test.js b/test/crowdsale/RefundableCrowdsale.test.js index 6b863abad..8472b87f2 100644 --- a/test/crowdsale/RefundableCrowdsale.test.js +++ b/test/crowdsale/RefundableCrowdsale.test.js @@ -1,5 +1,6 @@ const { ether } = require('../helpers/ether'); const { advanceBlock } = require('../helpers/advanceToBlock'); +const { balanceDifference } = require('../helpers/balanceDifference'); const shouldFail = require('../helpers/shouldFail'); const time = require('../helpers/time'); const { ethGetBalance } = require('../helpers/web3'); @@ -75,10 +76,9 @@ contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyon }); it('refunds', async function () { - const pre = await ethGetBalance(investor); - await this.crowdsale.claimRefund(investor, { gasPrice: 0 }); - const post = await ethGetBalance(investor); - post.minus(pre).should.be.bignumber.equal(lessThanGoal); + (await balanceDifference(investor, () => + this.crowdsale.claimRefund(investor, { gasPrice: 0 })) + ).should.be.bignumber.equal(lessThanGoal); }); }); }); diff --git a/test/examples/SampleCrowdsale.test.js b/test/examples/SampleCrowdsale.test.js index da367c970..da0bf1f7c 100644 --- a/test/examples/SampleCrowdsale.test.js +++ b/test/examples/SampleCrowdsale.test.js @@ -2,7 +2,7 @@ const { ether } = require('../helpers/ether'); const { advanceBlock } = require('../helpers/advanceToBlock'); const shouldFail = require('../helpers/shouldFail'); const time = require('../helpers/time'); -const { ethGetBalance } = require('../helpers/web3'); +const { balanceDifference } = require('../helpers/balanceDifference'); const BigNumber = web3.BigNumber; @@ -82,26 +82,21 @@ contract('SampleCrowdsale', function ([_, deployer, owner, wallet, investor]) { await time.increaseTo(this.openingTime); await this.crowdsale.send(GOAL); - const beforeFinalization = await ethGetBalance(wallet); - await time.increaseTo(this.afterClosingTime); - await this.crowdsale.finalize({ from: owner }); - const afterFinalization = await ethGetBalance(wallet); - - afterFinalization.minus(beforeFinalization).should.be.bignumber.equal(GOAL); + (await balanceDifference(wallet, async () => { + await time.increaseTo(this.afterClosingTime); + await this.crowdsale.finalize({ from: owner }); + })).should.be.bignumber.equal(GOAL); }); it('should allow refunds if the goal is not reached', async function () { - const balanceBeforeInvestment = await ethGetBalance(investor); - - await time.increaseTo(this.openingTime); - await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 }); - await time.increaseTo(this.afterClosingTime); - - await this.crowdsale.finalize({ from: owner }); - await this.crowdsale.claimRefund(investor, { gasPrice: 0 }); - - const balanceAfterRefund = await ethGetBalance(investor); - balanceBeforeInvestment.should.be.bignumber.equal(balanceAfterRefund); + (await balanceDifference(investor, async () => { + await time.increaseTo(this.openingTime); + await this.crowdsale.sendTransaction({ value: ether(1), from: investor, gasPrice: 0 }); + await time.increaseTo(this.afterClosingTime); + + await this.crowdsale.finalize({ from: owner }); + await this.crowdsale.claimRefund(investor, { gasPrice: 0 }); + })).should.be.bignumber.equal(0); }); describe('when goal > cap', function () { diff --git a/test/helpers/balanceDiff.js b/test/helpers/balanceDifference.js similarity index 70% rename from test/helpers/balanceDiff.js rename to test/helpers/balanceDifference.js index 8f88ba400..883d7df49 100644 --- a/test/helpers/balanceDiff.js +++ b/test/helpers/balanceDifference.js @@ -1,6 +1,6 @@ -async function balanceDifference (account, promise) { +async function balanceDifference (account, promiseFunc) { const balanceBefore = web3.eth.getBalance(account); - await promise(); + await promiseFunc(); const balanceAfter = web3.eth.getBalance(account); return balanceAfter.minus(balanceBefore); } diff --git a/test/helpers/test/balanceDifference.test.js b/test/helpers/test/balanceDifference.test.js new file mode 100644 index 000000000..e4eddc7f5 --- /dev/null +++ b/test/helpers/test/balanceDifference.test.js @@ -0,0 +1,22 @@ +const { balanceDifference } = require('../balanceDifference'); +const { sendEther } = require('../sendTransaction'); +const { ether } = require('../ether'); + +const BigNumber = web3.BigNumber; +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('balanceDifference', function ([sender, receiver]) { + it('returns balance increments', async function () { + (await balanceDifference(receiver, () => + sendEther(sender, receiver, ether(1))) + ).should.be.bignumber.equal(ether(1)); + }); + + it('returns balance decrements', async function () { + (await balanceDifference(sender, () => + sendEther(sender, receiver, ether(1))) + ).should.be.bignumber.equal(ether(-1)); + }); +}); diff --git a/test/payment/PullPayment.test.js b/test/payment/PullPayment.test.js index 1f4bcc525..13b27ff01 100644 --- a/test/payment/PullPayment.test.js +++ b/test/payment/PullPayment.test.js @@ -1,4 +1,4 @@ -const { ethGetBalance } = require('../helpers/web3'); +const { balanceDifference } = require('../helpers/balanceDifference'); const { ether } = require('../helpers/ether'); const BigNumber = web3.BigNumber; @@ -37,16 +37,13 @@ contract('PullPayment', function ([_, payer, payee1, payee2]) { }); it('can withdraw payment', async function () { - const initialBalance = await ethGetBalance(payee1); + (await balanceDifference(payee1, async () => { + await this.contract.callTransfer(payee1, amount, { from: payer }); + (await this.contract.payments(payee1)).should.be.bignumber.equal(amount); - await this.contract.callTransfer(payee1, amount, { from: payer }); + await this.contract.withdrawPayments(payee1); + })).should.be.bignumber.equal(amount); - (await this.contract.payments(payee1)).should.be.bignumber.equal(amount); - - await this.contract.withdrawPayments(payee1); (await this.contract.payments(payee1)).should.be.bignumber.equal(0); - - const balance = await ethGetBalance(payee1); - Math.abs(balance - initialBalance - amount).should.be.lt(1e16); }); }); diff --git a/test/payment/escrow/Escrow.behavior.js b/test/payment/escrow/Escrow.behavior.js index d7a7975e7..7dd0918a4 100644 --- a/test/payment/escrow/Escrow.behavior.js +++ b/test/payment/escrow/Escrow.behavior.js @@ -1,6 +1,7 @@ const expectEvent = require('../../helpers/expectEvent'); const shouldFail = require('../../helpers/shouldFail'); const { ethGetBalance } = require('../../helpers/web3'); +const { balanceDifference } = require('../../helpers/balanceDifference'); const { ether } = require('../../helpers/ether'); const BigNumber = web3.BigNumber; @@ -61,17 +62,13 @@ function shouldBehaveLikeEscrow (primary, [payee1, payee2]) { describe('withdrawals', async function () { it('can withdraw payments', async function () { - const payeeInitialBalance = await ethGetBalance(payee1); - - await this.escrow.deposit(payee1, { from: primary, value: amount }); - await this.escrow.withdraw(payee1, { from: primary }); + (await balanceDifference(payee1, async () => { + await this.escrow.deposit(payee1, { from: primary, value: amount }); + await this.escrow.withdraw(payee1, { from: primary }); + })).should.be.bignumber.equal(amount); (await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0); - (await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0); - - const payeeFinalBalance = await ethGetBalance(payee1); - payeeFinalBalance.sub(payeeInitialBalance).should.be.bignumber.equal(amount); }); it('can do an empty withdrawal', async function () { diff --git a/test/payment/escrow/RefundEscrow.test.js b/test/payment/escrow/RefundEscrow.test.js index 1f92e23af..bb5189ddc 100644 --- a/test/payment/escrow/RefundEscrow.test.js +++ b/test/payment/escrow/RefundEscrow.test.js @@ -1,6 +1,6 @@ const shouldFail = require('../../helpers/shouldFail'); const expectEvent = require('../../helpers/expectEvent'); -const { ethGetBalance } = require('../../helpers/web3'); +const { balanceDifference } = require('../../helpers/balanceDifference'); const { ether } = require('../../helpers/ether'); const { ZERO_ADDRESS } = require('../../helpers/constants'); @@ -73,11 +73,9 @@ contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee }); it('allows beneficiary withdrawal', async function () { - const beneficiaryInitialBalance = await ethGetBalance(beneficiary); - await this.escrow.beneficiaryWithdraw(); - const beneficiaryFinalBalance = await ethGetBalance(beneficiary); - - beneficiaryFinalBalance.sub(beneficiaryInitialBalance).should.be.bignumber.equal(amount * refundees.length); + (await balanceDifference(beneficiary, () => + this.escrow.beneficiaryWithdraw() + )).should.be.bignumber.equal(amount * refundees.length); }); it('prevents entering the refund state', async function () { @@ -109,11 +107,9 @@ contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee it('refunds refundees', async function () { for (const refundee of [refundee1, refundee2]) { - const refundeeInitialBalance = await ethGetBalance(refundee); - await this.escrow.withdraw(refundee, { from: primary }); - const refundeeFinalBalance = await ethGetBalance(refundee); - - refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount); + (await balanceDifference(refundee, () => + this.escrow.withdraw(refundee, { from: primary })) + ).should.be.bignumber.equal(amount); } });