From 549590d10515038c879e251293929c68115d9f6d Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Mon, 26 Jun 2017 17:31:52 -0500 Subject: [PATCH 1/8] Refactored to prevent DRY with beforeEach(). Refs# 259 --- test/DayLimit.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/test/DayLimit.js b/test/DayLimit.js index 99c565701..b2bc40e79 100644 --- a/test/DayLimit.js +++ b/test/DayLimit.js @@ -5,17 +5,19 @@ var DayLimitMock = artifacts.require('helpers/DayLimitMock.sol'); contract('DayLimit', function(accounts) { + let dayLimit; + let initLimit = 10; + + beforeEach( async function() { + dayLimit = await DayLimitMock.new(initLimit); + }); + it('should construct with the passed daily limit', async function() { - let initLimit = 10; - let dayLimit = await DayLimitMock.new(initLimit); let dailyLimit = await dayLimit.dailyLimit(); assert.equal(initLimit, dailyLimit); }); it('should be able to spend if daily limit is not reached', async function() { - let limit = 10; - let dayLimit = await DayLimitMock.new(limit); - await dayLimit.attemptSpend(8); let spentToday = await dayLimit.spentToday(); assert.equal(spentToday, 8); @@ -26,9 +28,6 @@ contract('DayLimit', function(accounts) { }); it('should prevent spending if daily limit is reached', async function() { - let limit = 10; - let dayLimit = await DayLimitMock.new(limit); - await dayLimit.attemptSpend(8); let spentToday = await dayLimit.spentToday(); assert.equal(spentToday, 8); @@ -41,9 +40,6 @@ contract('DayLimit', function(accounts) { }); it('should allow spending if daily limit is reached and then set higher', async function() { - let limit = 10; - let dayLimit = await DayLimitMock.new(limit); - await dayLimit.attemptSpend(8); let spentToday = await dayLimit.spentToday(); assert.equal(spentToday, 8); @@ -63,9 +59,6 @@ contract('DayLimit', function(accounts) { }); it('should allow spending if daily limit is reached and then amount spent is reset', async function() { - let limit = 10; - let dayLimit = await DayLimitMock.new(limit); - await dayLimit.attemptSpend(8); let spentToday = await dayLimit.spentToday(); assert.equal(spentToday, 8); From de99e7bf34b58095c1686f82e010d5f3bfc3fc6d Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Thu, 29 Jun 2017 12:31:32 -0500 Subject: [PATCH 2/8] Prevent DRY --- test/PullPayment.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/PullPayment.js b/test/PullPayment.js index f8536d732..4e74170da 100644 --- a/test/PullPayment.js +++ b/test/PullPayment.js @@ -1,15 +1,19 @@ var PullPaymentMock = artifacts.require("./helpers/PullPaymentMock.sol"); contract('PullPayment', function(accounts) { - + let ppce; + let tAMOUNT = 17*1e18; + + beforeEach(async function() { + ppce = await PullPaymentMock.new({value: tAMOUNT}); + }); + it("can't call asyncSend externally", async function() { - let ppc = await PullPaymentMock.new(); - assert.isUndefined(ppc.asyncSend); + assert.isUndefined(ppce.asyncSend); }); it("can record an async payment correctly", async function() { let AMOUNT = 100; - let ppce = await PullPaymentMock.new(); let callSend = await ppce.callSend(accounts[0], AMOUNT); let paymentsToAccount0 = await ppce.payments(accounts[0]); let totalPayments = await ppce.totalPayments(); @@ -19,7 +23,6 @@ contract('PullPayment', function(accounts) { }); it("can add multiple balances on one account", async function() { - let ppce = await PullPaymentMock.new(); let call1 = await ppce.callSend(accounts[0], 200); let call2 = await ppce.callSend(accounts[0], 300); let paymentsToAccount0 = await ppce.payments(accounts[0]); @@ -30,7 +33,6 @@ contract('PullPayment', function(accounts) { }); it("can add balances on multiple accounts", async function() { - let ppce = await PullPaymentMock.new(); let call1 = await ppce.callSend(accounts[0], 200); let call2 = await ppce.callSend(accounts[1], 300); @@ -45,11 +47,9 @@ contract('PullPayment', function(accounts) { }); it("can withdraw payment", async function() { - let AMOUNT = 17*1e18; let payee = accounts[1]; let initialBalance = web3.eth.getBalance(payee); - let ppce = await PullPaymentMock.new({value: AMOUNT}); let call1 = await ppce.callSend(payee, AMOUNT); let payment1 = await ppce.payments(payee); From 4d6b48f39e5f07ddba185df40900c325390d4e99 Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Thu, 29 Jun 2017 12:40:26 -0500 Subject: [PATCH 3/8] Typo fix. standart -> standard --- contracts/token/StandardToken.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/token/StandardToken.sol b/contracts/token/StandardToken.sol index 67e5d3f13..4e96ad97f 100644 --- a/contracts/token/StandardToken.sol +++ b/contracts/token/StandardToken.sol @@ -8,7 +8,7 @@ import './ERC20.sol'; /** * @title Standard ERC20 token * - * @dev Implemantation of the basic standart token. + * @dev Implemantation of the basic standard token. * @dev https://github.com/ethereum/EIPs/issues/20 * @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol */ From 34e3ce4ab426fffd9729e87defea9650fa233762 Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Thu, 29 Jun 2017 12:44:14 -0500 Subject: [PATCH 4/8] Typo fix: avaible -> available --- contracts/token/StandardToken.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/token/StandardToken.sol b/contracts/token/StandardToken.sol index 4e96ad97f..93fbe1ffe 100644 --- a/contracts/token/StandardToken.sol +++ b/contracts/token/StandardToken.sol @@ -56,7 +56,7 @@ contract StandardToken is BasicToken, ERC20 { * @dev Function to check the amount of tokens that an owner allowed to a spender. * @param _owner address The address which owns the funds. * @param _spender address The address which will spend the funds. - * @return A uint256 specifing the amount of tokens still avaible for the spender. + * @return A uint256 specifing the amount of tokens still available for the spender. */ function allowance(address _owner, address _spender) constant returns (uint256 remaining) { return allowed[_owner][_spender]; From b40a01e97bc245c48c3b256149f5a379e3761532 Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Thu, 29 Jun 2017 13:12:58 -0500 Subject: [PATCH 5/8] Small refactoring to not DRY --- test/TokenDestructible.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/TokenDestructible.js b/test/TokenDestructible.js index 1f943e3df..7ba052e21 100644 --- a/test/TokenDestructible.js +++ b/test/TokenDestructible.js @@ -5,9 +5,13 @@ var StandardTokenMock = artifacts.require("./helpers/StandardTokenMock.sol"); require('./helpers/transactionMined.js'); contract('TokenDestructible', function(accounts) { + let destructible; + beforeEach(async function() { + destructible = await TokenDestructible.new({fron: accounts[0], value: web3.toWei('10', 'ether')}); + }); + it('should send balance to owner after destruction', async function() { - let destructible = await TokenDestructible.new({from: accounts[0], value: web3.toWei('10','ether')}); let owner = await destructible.owner(); let initBalance = web3.eth.getBalance(owner); await destructible.destroy([], {from: owner}); @@ -16,7 +20,6 @@ contract('TokenDestructible', function(accounts) { }); it('should send tokens to owner after destruction', async function() { - let destructible = await TokenDestructible.new({from: accounts[0], value: web3.toWei('10','ether')}); let owner = await destructible.owner(); let token = await StandardTokenMock.new(destructible.address, 100); let initContractBalance = await token.balanceOf(destructible.address); From 60ef284a9a8bc1c8ca11300aa3a5b5689d291ad4 Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Thu, 29 Jun 2017 13:13:53 -0500 Subject: [PATCH 6/8] Typo in variable, using global tAMOUNT for the latest test case. --- test/PullPayment.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/PullPayment.js b/test/PullPayment.js index 4e74170da..8df6d8f20 100644 --- a/test/PullPayment.js +++ b/test/PullPayment.js @@ -50,13 +50,13 @@ contract('PullPayment', function(accounts) { let payee = accounts[1]; let initialBalance = web3.eth.getBalance(payee); - let call1 = await ppce.callSend(payee, AMOUNT); + let call1 = await ppce.callSend(payee, tAMOUNT); let payment1 = await ppce.payments(payee); - assert.equal(payment1, AMOUNT); + assert.equal(payment1, tAMOUNT); let totalPayments = await ppce.totalPayments(); - assert.equal(totalPayments, AMOUNT); + assert.equal(totalPayments, tAMOUNT); let withdraw = await ppce.withdrawPayments({from: payee}); let payment2 = await ppce.payments(payee); @@ -66,7 +66,7 @@ contract('PullPayment', function(accounts) { assert.equal(totalPayments, 0); let balance = web3.eth.getBalance(payee); - assert(Math.abs(balance-initialBalance-AMOUNT) < 1e16); + assert(Math.abs(balance-initialBalance-tAMOUNT) < 1e16); }); }); From 3c9638b62e6dc226450f49a3d46c9d1992f2485a Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Thu, 29 Jun 2017 13:18:54 -0500 Subject: [PATCH 7/8] Refactoring to not DRY. The latest test case is not affected since the aproval is for accounts[1], which does the transaction, and it is independent of the amount of tokens that accounts[0] might have. --- test/StandardToken.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/StandardToken.js b/test/StandardToken.js index e3f578726..0106926a9 100644 --- a/test/StandardToken.js +++ b/test/StandardToken.js @@ -5,8 +5,13 @@ var StandardTokenMock = artifacts.require('./helpers/StandardTokenMock.sol'); contract('StandardToken', function(accounts) { + let token; + + beforeEach(async function() { + token = await StandardTokenMock.new(accounts[0], 100); + }); + it('should return the correct totalSupply after construction', async function() { - let token = await StandardTokenMock.new(accounts[0], 100); let totalSupply = await token.totalSupply(); assert.equal(totalSupply, 100); @@ -56,7 +61,6 @@ contract('StandardToken', function(accounts) { }); it('should throw an error when trying to transfer more than allowed', async function() { - let token = await StandardTokenMock.new(); await token.approve(accounts[1], 99); try { await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]}); From 6735a3ccd61c6f2921bec2cd3ca91b1964f86769 Mon Sep 17 00:00:00 2001 From: Rudy Godoy Date: Thu, 20 Jul 2017 16:01:41 -0500 Subject: [PATCH 8/8] Renamed tAMOUNT to much friendly amount --- test/PullPayment.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/PullPayment.js b/test/PullPayment.js index 8df6d8f20..f8106464a 100644 --- a/test/PullPayment.js +++ b/test/PullPayment.js @@ -2,10 +2,10 @@ var PullPaymentMock = artifacts.require("./helpers/PullPaymentMock.sol"); contract('PullPayment', function(accounts) { let ppce; - let tAMOUNT = 17*1e18; + let amount = 17*1e18; beforeEach(async function() { - ppce = await PullPaymentMock.new({value: tAMOUNT}); + ppce = await PullPaymentMock.new({value: amount}); }); it("can't call asyncSend externally", async function() { @@ -50,13 +50,13 @@ contract('PullPayment', function(accounts) { let payee = accounts[1]; let initialBalance = web3.eth.getBalance(payee); - let call1 = await ppce.callSend(payee, tAMOUNT); + let call1 = await ppce.callSend(payee, amount); let payment1 = await ppce.payments(payee); - assert.equal(payment1, tAMOUNT); + assert.equal(payment1, amount); let totalPayments = await ppce.totalPayments(); - assert.equal(totalPayments, tAMOUNT); + assert.equal(totalPayments, amount); let withdraw = await ppce.withdrawPayments({from: payee}); let payment2 = await ppce.payments(payee); @@ -66,7 +66,7 @@ contract('PullPayment', function(accounts) { assert.equal(totalPayments, 0); let balance = web3.eth.getBalance(payee); - assert(Math.abs(balance-initialBalance-tAMOUNT) < 1e16); + assert(Math.abs(balance-initialBalance-amount) < 1e16); }); });