Browse Source

fix: made code cleaner. added helper. removed done from most tests.

Angello Pozo 8 năm trước cách đây
mục cha
commit
eb41a81faa
11 tập tin đã thay đổi với 116 bổ sung127 xóa
  1. 9 9
      test/BasicToken.js
  2. 19 16
      test/Bounty.js
  3. 11 14
      test/Claimable.js
  4. 2 2
      test/Killable.js
  5. 15 19
      test/LimitBalance.js
  6. 8 10
      test/Ownable.js
  7. 13 11
      test/PullPayment.js
  8. 13 20
      test/SafeMath.js
  9. 13 16
      test/StandardToken.js
  10. 10 10
      test/Stoppable.js
  11. 3 0
      test/helpers/assertJump.js

+ 9 - 9
test/BasicToken.js

@@ -1,31 +1,31 @@
+const assertJump = require('./helpers/assertJump');
+
 contract('BasicToken', function(accounts) {
 
-  it("should return the correct totalSupply after construction", async function(done) {
+  it("should return the correct totalSupply after construction", async function() {
     let token = await BasicTokenMock.new(accounts[0], 100);
     let totalSupply = await token.totalSupply();
+
     assert.equal(totalSupply, 100);
-    done();
   })
 
-  it("should return correct balances after transfer", async function(done){
+  it("should return correct balances after transfer", async function(){
     let token = await BasicTokenMock.new(accounts[0], 100);
     let transfer = await token.transfer(accounts[1], 100);
+
     let firstAccountBalance = await token.balanceOf(accounts[0]);
     assert.equal(firstAccountBalance, 0);
+
     let secondAccountBalance = await token.balanceOf(accounts[1]);
     assert.equal(secondAccountBalance, 100);
-    done();
   });
 
-  it("should throw an error when trying to transfer more than balance", async function(done) {
-
+  it("should throw an error when trying to transfer more than balance", async function() {
     let token = await BasicTokenMock.new(accounts[0], 100);
     try {
       let transfer = await token.transfer(accounts[1], 101);
     } catch(error) {
-      if (error.message.search('invalid JUMP') === -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 

+ 19 - 16
test/Bounty.js

@@ -8,51 +8,51 @@ let sendReward = function(sender, receiver, value){
 
 contract('Bounty', function(accounts) {
 
-  it("sets reward", async function(done){
+  it("sets reward", async function(){
     let owner = accounts[0];
     let reward = web3.toWei(1, "ether");
-
     let bounty = await SecureTargetBounty.new();
     sendReward(owner, bounty.address, reward);
+
     assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
-    done();
   })
 
-  it("empties itself when killed", async function(done){
+  it("empties itself when killed", async function(){
     let owner = accounts[0];
     let reward = web3.toWei(1, "ether");
-
     let bounty = await SecureTargetBounty.new();
     sendReward(owner, bounty.address, reward);
-    assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
+
+    assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
+
     await bounty.kill();
     assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
-    done();
   })
 
   describe("Against secure contract", function(){
-    it("checkInvariant returns true", async function(done){
 
+    it("checkInvariant returns true", async function(){
       let bounty = await SecureTargetBounty.new();
       let target = await bounty.createTarget();
       let check = await bounty.checkInvariant.call();
+
       assert.isTrue(check);
-      done();
     })
 
     it("cannot claim reward", async function(done){
       let owner = accounts[0];
       let researcher = accounts[1];
       let reward = web3.toWei(1, "ether");
-
       let bounty = await SecureTargetBounty.new();
       let event = bounty.TargetCreated({});
 
       event.watch(async function(err, result) {
         event.stopWatching();
         if (err) { throw err }
+
         var targetAddress = result.args.createdAddress;
         sendReward(owner, bounty.address, reward);
+
         assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
 
         try {
@@ -61,13 +61,14 @@ contract('Bounty', function(accounts) {
         } catch(error) {
             let reClaimedBounty = await bounty.claimed.call();
             assert.isFalse(reClaimedBounty);
+
             try {
               let withdraw = await bounty.withdrawPayments({from:researcher});
               done("should not come here")
             } catch (err) {
-              assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
+              assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
+              done();
             }
-            done();
         }//end of first try catch
       });
       bounty.createTarget({from:researcher});
@@ -75,21 +76,19 @@ contract('Bounty', function(accounts) {
   })
 
   describe("Against broken contract", function(){
-    it("checkInvariant returns false", async function(done){
+    it("checkInvariant returns false", async function(){
       let bounty = await InsecureTargetBounty.new();
       let target = await bounty.createTarget();
       let invarriantCall = await bounty.checkInvariant.call();
+
       assert.isFalse(invarriantCall);
-      done();
     })
 
     it("claims reward", async function(done){
       let owner = accounts[0];
       let researcher = accounts[1];
       let reward = web3.toWei(1, "ether");
-
       let bounty = await InsecureTargetBounty.new();
-
       let event = bounty.TargetCreated({});
 
       event.watch(async function(err, result) {
@@ -97,12 +96,16 @@ contract('Bounty', function(accounts) {
         if (err) { throw err }
         let targetAddress = result.args.createdAddress;
         sendReward(owner, bounty.address, reward);
+
         assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
 
         let bountyClaim = await bounty.claim(targetAddress, {from:researcher});
         let claim = await bounty.claimed.call();
+
         assert.isTrue(claim);
+
         let payment = await bounty.withdrawPayments({from:researcher});
+
         assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
         done();
       })

+ 11 - 14
test/Claimable.js

@@ -1,53 +1,50 @@
 contract('Claimable', function(accounts) {
   let claimable;
 
-  beforeEach(async function(done) {
+  beforeEach(async function() {
     claimable = await Claimable.new();
-    done();
   });
 
-  it("should have an owner", async function(done) {
+  it("should have an owner", async function() {
     let owner = await claimable.owner();
     assert.isTrue(owner != 0);
-    done();
   });
 
-  it("changes pendingOwner after transfer", async function(done) {
+  it("changes pendingOwner after transfer", async function() {
     let newOwner = accounts[1];
     let transfer = await claimable.transfer(newOwner);
     let pendingOwner = await claimable.pendingOwner();
+
     assert.isTrue(pendingOwner === newOwner);
-    done();
   });
 
-  it("should prevent to claimOwnership from no pendingOwner", async function(done) {
+  it("should prevent to claimOwnership from no pendingOwner", async function() {
     let claimedOwner = await claimable.claimOwnership({from: accounts[2]});
     let owner = await claimable.owner();
+    
     assert.isTrue(owner != accounts[2]);
-    done();
   });
 
-  it("should prevent non-owners from transfering", async function(done) {
+  it("should prevent non-owners from transfering", async function() {
     let transfer = await claimable.transfer(accounts[2], {from: accounts[2]});
     let pendingOwner = await claimable.pendingOwner();
+
     assert.isFalse(pendingOwner === accounts[2]);
-    done();
   });
 
   describe("after initiating a transfer", function () {
     let newOwner;
 
-    beforeEach(async function (done) {
+    beforeEach(async function () {
       newOwner = accounts[1];
       await claimable.transfer(newOwner);
-      done();
     });
 
-    it("changes allow pending owner to claim ownership", async function(done) {
+    it("changes allow pending owner to claim ownership", async function() {
       let claimedOwner = await claimable.claimOwnership({from: newOwner})
       let owner = await claimable.owner();
+
       assert.isTrue(owner === newOwner);
-      done();
     });
   });
 });

+ 2 - 2
test/Killable.js

@@ -32,7 +32,7 @@ contract('Killable', function(accounts) {
     }
 };
 
-  it("should send balance to owner after death", async function(done) {
+  it("should send balance to owner after death", async function() {
     let initBalance, newBalance, owner, address, killable, kBalance, txnHash, receiptMined;
     web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('50','ether')}, function(err, result) {
       if(err)
@@ -49,8 +49,8 @@ contract('Killable', function(accounts) {
     txnHash = await killable.kill({from: owner});
     receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
     newBalance = web3.eth.getBalance(owner);
+
     assert.isTrue(newBalance > initBalance);
-    done();
   });
 
 });

+ 15 - 19
test/LimitBalance.js

@@ -1,59 +1,55 @@
+const assertJump = require('./helpers/assertJump');
+
 contract('LimitBalance', function(accounts) {
   let lb;
 
-  beforeEach(async function(done) {
+  beforeEach(async function() {
     lb = await LimitBalanceMock.new();
-    done();
   });
 
   let LIMIT = 1000;
 
-  it("should expose limit", async function(done) {
+  it("should expose limit", async function() {
     let limit = await lb.limit();
     assert.equal(limit, LIMIT);
-    done();
   });
 
-  it("should allow sending below limit", async function(done) {
+  it("should allow sending below limit", async function() {
     let amount = 1;
     let limDeposit = await lb.limitedDeposit({value: amount});
+
     assert.equal(web3.eth.getBalance(lb.address), amount);
-    done();
   });
 
-  it("shouldnt allow sending above limit", async function(done) {
-
+  it("shouldnt allow sending above limit", async function() {
     let amount = 1110;
     try {
       let limDeposit = await lb.limitedDeposit({value: amount});
     } catch(error) {
-      if (error.message.search('invalid JUMP') == -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 
-  it("should allow multiple sends below limit", async function(done) {
+  it("should allow multiple sends below limit", async function() {
     let amount = 500;
-
     let limDeposit = await lb.limitedDeposit({value: amount});
+
     assert.equal(web3.eth.getBalance(lb.address), amount);
+
     let limDeposit2 = await lb.limitedDeposit({value: amount});
     assert.equal(web3.eth.getBalance(lb.address), amount*2);
-    done();
   });
 
-  it("shouldnt allow multiple sends above limit", async function(done) {
+  it("shouldnt allow multiple sends above limit", async function() {
     let amount = 500;
-
     let limDeposit = await lb.limitedDeposit({value: amount});
+
     assert.equal(web3.eth.getBalance(lb.address), amount);
+
     try {
       await lb.limitedDeposit({value: amount+1})
     } catch(error) {
-      if (error.message.search('invalid JUMP') == -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 

+ 8 - 10
test/Ownable.js

@@ -1,40 +1,38 @@
 contract('Ownable', function(accounts) {
   let ownable;
 
-  beforeEach(async function(done) {
+  beforeEach(async function() {
     ownable = await Ownable.new();
-    done();
   });
 
-  it("should have an owner", async function(done) {
+  it("should have an owner", async function() {
     let owner = await ownable.owner();
     assert.isTrue(owner != 0);
-    done();
   });
 
-  it("changes owner after transfer", async function(done) {
+  it("changes owner after transfer", async function() {
     let other = accounts[1];
     let transfer = await ownable.transfer(other);
     let owner = await ownable.owner();
+
     assert.isTrue(owner === other);
-    done();
   });
 
-  it("should prevent non-owners from transfering", async function(done) {
+  it("should prevent non-owners from transfering", async function() {
     let other = accounts[2];
     let transfer = await ownable.transfer(other, {from: accounts[2]});
     let owner = await ownable.owner();
+
      assert.isFalse(owner === other);
-     done();
   });
 
-  it("should guard ownership against stuck state", async function(done) {
+  it("should guard ownership against stuck state", async function() {
     let ownable = Ownable.deployed();
     let originalOwner = await ownable.owner();
     let transfer = await ownable.transfer(null, {from: originalOwner});
     let newOwner = await ownable.owner();
+    
     assert.equal(originalOwner, newOwner);
-    done();
   });
 
 });

+ 13 - 11
test/PullPayment.js

@@ -1,55 +1,57 @@
 contract('PullPayment', function(accounts) {
 
-  it("can't call asyncSend externally", async function(done) {
+  it("can't call asyncSend externally", async function() {
     let ppc = await PullPaymentMock.new();
     assert.isUndefined(ppc.asyncSend);
-    done();
   });
 
-  it("can record an async payment correctly", async function(done) {
+  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]);
+
     assert.equal(paymentsToAccount0, AMOUNT);
-    done();
   });
 
-  it("can add multiple balances on one account", async function(done) {
+  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 call2 = await ppce.callSend(accounts[0], 300);
     let paymentsToAccount0 = await ppce.payments(accounts[0]);
+
     assert.equal(paymentsToAccount0, 500);
-    done();
   });
 
-  it("can add balances on multiple accounts", async function(done) {
+  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);
+
     let paymentsToAccount0 = await ppce.payments(accounts[0]);
     assert.equal(paymentsToAccount0, 200);
+
     let paymentsToAccount1 = await ppce.payments(accounts[1]);
     assert.equal(paymentsToAccount1, 300);
-    done();
   });
 
-  it("can withdraw payment", async function(done) {
+  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);
     assert.equal(payment1, AMOUNT);
+
     let withdraw = await ppce.withdrawPayments({from: payee});
     let payment2 = await ppce.payments(payee);
     assert.equal(payment2, 0);
+
     let balance = web3.eth.getBalance(payee);
     assert(Math.abs(balance-initialBalance-AMOUNT) < 1e16);
-    done();
   });
 
 });

+ 13 - 20
test/SafeMath.js

@@ -1,73 +1,66 @@
+const assertJump = require('./helpers/assertJump');
 
 contract('SafeMath', function(accounts) {
 
   let safeMath;
 
-  before(async function(done) {
+  before(async function() {
     safeMath = await SafeMathMock.new();
-    done();
   });
 
-  it("multiplies correctly", async function(done) {
+  it("multiplies correctly", async function() {
     let a = 5678;
     let b = 1234;
     let mult = await safeMath.multiply(a, b);
     let result = await safeMath.result();
     assert.equal(result, a*b);
-    done();
   });
 
-  it("adds correctly", async function(done) {
+  it("adds correctly", async function() {
     let a = 5678;
     let b = 1234;
     let add = await safeMath.add(a, b);
     let result = await safeMath.result();
+
     assert.equal(result, a+b);
-    done();
   });
 
-  it("subtracts correctly", async function(done) {
+  it("subtracts correctly", async function() {
     let a = 5678;
     let b = 1234;
     let subtract = await safeMath.subtract(a, b);
     let result = await safeMath.result();
+
     assert.equal(result, a-b);
-    done();
   });
 
-  it("should throw an error if subtraction result would be negative", async function (done) {
+  it("should throw an error if subtraction result would be negative", async function () {
     let a = 1234;
     let b = 5678;
     try {
       let subtract = await safeMath.subtract(a, b);
     } catch(error) {
-      if (error.message.search('invalid JUMP') == -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 
-  it("should throw an error on addition overflow", async function(done) {
+  it("should throw an error on addition overflow", async function() {
     let a = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
     let b = 1;
     try {
       let add = await safeMath.add(a, b);
     } catch(error) {
-      if (error.message.search('invalid JUMP') == -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 
-  it("should throw an error on multiplication overflow", async function(done) {
+  it("should throw an error on multiplication overflow", async function() {
     let a = 115792089237316195423570985008687907853269984665640564039457584007913129639933;
     let b = 2;
     try {
       let multiply = await safeMath.multiply(a, b);
     } catch(error) {
-      if (error.message.search('invalid JUMP') == -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 

+ 13 - 16
test/StandardToken.js

@@ -1,42 +1,42 @@
+const assertJump = require('./helpers/assertJump');
+
 contract('StandardToken', function(accounts) {
 
-  it("should return the correct totalSupply after construction", async function(done) {
+  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);
-    done();
   })
 
-  it("should return the correct allowance amount after approval", async function(done) {
+  it("should return the correct allowance amount after approval", async function() {
     let token = await StandardTokenMock.new();
     let approve = await token.approve(accounts[1], 100);
     let allowance = await token.allowance(accounts[0], accounts[1]);
+
     assert.equal(allowance, 100);
-    done();
   });
 
-  it("should return correct balances after transfer", async function(done) {
+  it("should return correct balances after transfer", async function() {
     let token = await StandardTokenMock.new(accounts[0], 100);
     let transfer = await token.transfer(accounts[1], 100);
     let balance0 = await token.balanceOf(accounts[0]);
     assert.equal(balance0, 0);
+
     let balance1 = await token.balanceOf(accounts[1]);
     assert.equal(balance1, 100);
-    done();
   });
 
-  it("should throw an error when trying to transfer more than balance", async function(done) {
+  it("should throw an error when trying to transfer more than balance", async function() {
     let token = await StandardTokenMock.new(accounts[0], 100);
     try {
       let transfer = await token.transfer(accounts[1], 101);
     } catch(error) {
-      if (error.message.search('invalid JUMP') == -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 
-  it("should return correct balances after transfering from another account", async function(done) {
+  it("should return correct balances after transfering from another account", async function() {
     let token = await StandardTokenMock.new(accounts[0], 100);
     let approve = await token.approve(accounts[1], 100);
     let transferFrom = await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]});
@@ -49,18 +49,15 @@ contract('StandardToken', function(accounts) {
 
     let balance2 = await token.balanceOf(accounts[1]);
     assert.equal(balance2, 0);
-    done();
   });
 
-  it("should throw an error when trying to transfer more than allowed", async function(done) {
+  it("should throw an error when trying to transfer more than allowed", async function() {
     let token = await StandardTokenMock.new();
     let approve = await token.approve(accounts[1], 99);
     try {
       let transfer = await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]});
     } catch (error) {
-      if (error.message.search('invalid JUMP') == -1) throw error
-      assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
-      done();
+      assertJump(error);
     }
   });
 

+ 10 - 10
test/Stoppable.js

@@ -1,52 +1,52 @@
 contract('Stoppable', function(accounts) {
 
-  it("can perform normal process in non-emergency", async function(done) {
+  it("can perform normal process in non-emergency", async function() {
     let stoppable = await StoppableMock.new();
     let count0 = await stoppable.count();
     assert.equal(count0, 0);
+
     let normalProcess = await stoppable.normalProcess();
     let count1 = await stoppable.count();
     assert.equal(count1, 1);
-    done();
   });
 
-  it("can not perform normal process in emergency", async function(done) {
+  it("can not perform normal process in emergency", async function() {
     let stoppable = await StoppableMock.new();
     let emergencyStop = await stoppable.emergencyStop();
     let count0 = await stoppable.count();
     assert.equal(count0, 0);
+
     let normalProcess = await stoppable.normalProcess();
     let count1 = await stoppable.count();
     assert.equal(count1, 0);
-    done();
   });
 
 
-  it("can not take drastic measure in non-emergency", async function(done) {
+  it("can not take drastic measure in non-emergency", async function() {
     let stoppable = await StoppableMock.new();
     let drasticMeasure = await stoppable.drasticMeasure();
     let drasticMeasureTaken = await stoppable.drasticMeasureTaken();
+
     assert.isFalse(drasticMeasureTaken);
-    done();
   });
 
-  it("can take a drastic measure in an emergency", async function(done) {
+  it("can take a drastic measure in an emergency", async function() {
     let stoppable = await StoppableMock.new();
     let emergencyStop = await stoppable.emergencyStop();
     let drasticMeasure = await stoppable.drasticMeasure();
     let drasticMeasureTaken = await stoppable.drasticMeasureTaken();
+
     assert.isTrue(drasticMeasureTaken);
-    done();
   });
 
-  it("should resume allowing normal process after emergency is over", async function(done) {
+  it("should resume allowing normal process after emergency is over", async function() {
     let stoppable = await StoppableMock.new();
     let emergencyStop = await stoppable.emergencyStop();
     let release = await stoppable.release();
     let normalProcess = await stoppable.normalProcess();
     let count0 = await stoppable.count();
+
     assert.equal(count0, 1);
-    done();
   });
 
 });

+ 3 - 0
test/helpers/assertJump.js

@@ -0,0 +1,3 @@
+module.exports = function(error) {
+  assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
+}