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

pull/97/head
Angello Pozo 8 years ago
parent 688106e9c3
commit eb41a81faa
  1. 18
      test/BasicToken.js
  2. 35
      test/Bounty.js
  3. 25
      test/Claimable.js
  4. 4
      test/Killable.js
  5. 34
      test/LimitBalance.js
  6. 18
      test/Ownable.js
  7. 24
      test/PullPayment.js
  8. 33
      test/SafeMath.js
  9. 29
      test/StandardToken.js
  10. 20
      test/Stoppable.js
  11. 3
      test/helpers/assertJump.js

@ -1,31 +1,31 @@
const assertJump = require('./helpers/assertJump');
contract('BasicToken', function(accounts) { 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 token = await BasicTokenMock.new(accounts[0], 100);
let totalSupply = await token.totalSupply(); let totalSupply = await token.totalSupply();
assert.equal(totalSupply, 100); 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 token = await BasicTokenMock.new(accounts[0], 100);
let transfer = await token.transfer(accounts[1], 100); let transfer = await token.transfer(accounts[1], 100);
let firstAccountBalance = await token.balanceOf(accounts[0]); let firstAccountBalance = await token.balanceOf(accounts[0]);
assert.equal(firstAccountBalance, 0); assert.equal(firstAccountBalance, 0);
let secondAccountBalance = await token.balanceOf(accounts[1]); let secondAccountBalance = await token.balanceOf(accounts[1]);
assert.equal(secondAccountBalance, 100); 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); let token = await BasicTokenMock.new(accounts[0], 100);
try { try {
let transfer = await token.transfer(accounts[1], 101); let transfer = await token.transfer(accounts[1], 101);
} catch(error) { } catch(error) {
if (error.message.search('invalid JUMP') === -1) throw error assertJump(error);
assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
done();
} }
}); });

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

@ -1,53 +1,50 @@
contract('Claimable', function(accounts) { contract('Claimable', function(accounts) {
let claimable; let claimable;
beforeEach(async function(done) { beforeEach(async function() {
claimable = await Claimable.new(); 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(); let owner = await claimable.owner();
assert.isTrue(owner != 0); 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 newOwner = accounts[1];
let transfer = await claimable.transfer(newOwner); let transfer = await claimable.transfer(newOwner);
let pendingOwner = await claimable.pendingOwner(); let pendingOwner = await claimable.pendingOwner();
assert.isTrue(pendingOwner === newOwner); 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 claimedOwner = await claimable.claimOwnership({from: accounts[2]});
let owner = await claimable.owner(); let owner = await claimable.owner();
assert.isTrue(owner != accounts[2]); 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 transfer = await claimable.transfer(accounts[2], {from: accounts[2]});
let pendingOwner = await claimable.pendingOwner(); let pendingOwner = await claimable.pendingOwner();
assert.isFalse(pendingOwner === accounts[2]); assert.isFalse(pendingOwner === accounts[2]);
done();
}); });
describe("after initiating a transfer", function () { describe("after initiating a transfer", function () {
let newOwner; let newOwner;
beforeEach(async function (done) { beforeEach(async function () {
newOwner = accounts[1]; newOwner = accounts[1];
await claimable.transfer(newOwner); 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 claimedOwner = await claimable.claimOwnership({from: newOwner})
let owner = await claimable.owner(); let owner = await claimable.owner();
assert.isTrue(owner === newOwner); assert.isTrue(owner === newOwner);
done();
}); });
}); });
}); });

@ -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; 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) { web3.eth.sendTransaction({from: web3.eth.coinbase, to: accounts[0], value: web3.toWei('50','ether')}, function(err, result) {
if(err) if(err)
@ -49,8 +49,8 @@ contract('Killable', function(accounts) {
txnHash = await killable.kill({from: owner}); txnHash = await killable.kill({from: owner});
receiptMined = await web3.eth.getTransactionReceiptMined(txnHash); receiptMined = await web3.eth.getTransactionReceiptMined(txnHash);
newBalance = web3.eth.getBalance(owner); newBalance = web3.eth.getBalance(owner);
assert.isTrue(newBalance > initBalance); assert.isTrue(newBalance > initBalance);
done();
}); });
}); });

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

@ -1,40 +1,38 @@
contract('Ownable', function(accounts) { contract('Ownable', function(accounts) {
let ownable; let ownable;
beforeEach(async function(done) { beforeEach(async function() {
ownable = await Ownable.new(); 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(); let owner = await ownable.owner();
assert.isTrue(owner != 0); 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 other = accounts[1];
let transfer = await ownable.transfer(other); let transfer = await ownable.transfer(other);
let owner = await ownable.owner(); let owner = await ownable.owner();
assert.isTrue(owner === other); 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 other = accounts[2];
let transfer = await ownable.transfer(other, {from: accounts[2]}); let transfer = await ownable.transfer(other, {from: accounts[2]});
let owner = await ownable.owner(); let owner = await ownable.owner();
assert.isFalse(owner === other); 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 ownable = Ownable.deployed();
let originalOwner = await ownable.owner(); let originalOwner = await ownable.owner();
let transfer = await ownable.transfer(null, {from: originalOwner}); let transfer = await ownable.transfer(null, {from: originalOwner});
let newOwner = await ownable.owner(); let newOwner = await ownable.owner();
assert.equal(originalOwner, newOwner); assert.equal(originalOwner, newOwner);
done();
}); });
}); });

@ -1,55 +1,57 @@
contract('PullPayment', function(accounts) { 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(); let ppc = await PullPaymentMock.new();
assert.isUndefined(ppc.asyncSend); 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 AMOUNT = 100;
let ppce = await PullPaymentMock.new(); let ppce = await PullPaymentMock.new();
let callSend = await ppce.callSend(accounts[0], AMOUNT); let callSend = await ppce.callSend(accounts[0], AMOUNT);
let paymentsToAccount0 = await ppce.payments(accounts[0]); let paymentsToAccount0 = await ppce.payments(accounts[0]);
assert.equal(paymentsToAccount0, AMOUNT); 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 ppce = await PullPaymentMock.new();
let call1 = await ppce.callSend(accounts[0], 200); 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]); let paymentsToAccount0 = await ppce.payments(accounts[0]);
assert.equal(paymentsToAccount0, 500); 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 ppce = await PullPaymentMock.new();
let call1 = await ppce.callSend(accounts[0], 200); let call1 = await ppce.callSend(accounts[0], 200);
let call2 = await ppce.callSend(accounts[1], 300); let call2 = await ppce.callSend(accounts[1], 300);
let paymentsToAccount0 = await ppce.payments(accounts[0]); let paymentsToAccount0 = await ppce.payments(accounts[0]);
assert.equal(paymentsToAccount0, 200); assert.equal(paymentsToAccount0, 200);
let paymentsToAccount1 = await ppce.payments(accounts[1]); let paymentsToAccount1 = await ppce.payments(accounts[1]);
assert.equal(paymentsToAccount1, 300); assert.equal(paymentsToAccount1, 300);
done();
}); });
it("can withdraw payment", async function(done) { it("can withdraw payment", async function() {
let AMOUNT = 17*1e18; let AMOUNT = 17*1e18;
let payee = accounts[1]; let payee = accounts[1];
let initialBalance = web3.eth.getBalance(payee); let initialBalance = web3.eth.getBalance(payee);
let ppce = await PullPaymentMock.new({value: AMOUNT}); let ppce = await PullPaymentMock.new({value: AMOUNT});
let call1 = await ppce.callSend(payee, AMOUNT); let call1 = await ppce.callSend(payee, AMOUNT);
let payment1 = await ppce.payments(payee); let payment1 = await ppce.payments(payee);
assert.equal(payment1, AMOUNT); assert.equal(payment1, AMOUNT);
let withdraw = await ppce.withdrawPayments({from: payee}); let withdraw = await ppce.withdrawPayments({from: payee});
let payment2 = await ppce.payments(payee); let payment2 = await ppce.payments(payee);
assert.equal(payment2, 0); assert.equal(payment2, 0);
let balance = web3.eth.getBalance(payee); let balance = web3.eth.getBalance(payee);
assert(Math.abs(balance-initialBalance-AMOUNT) < 1e16); assert(Math.abs(balance-initialBalance-AMOUNT) < 1e16);
done();
}); });
}); });

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

@ -1,42 +1,42 @@
const assertJump = require('./helpers/assertJump');
contract('StandardToken', function(accounts) { 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 token = await StandardTokenMock.new(accounts[0], 100);
let totalSupply = await token.totalSupply(); let totalSupply = await token.totalSupply();
assert.equal(totalSupply, 100); 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 token = await StandardTokenMock.new();
let approve = await token.approve(accounts[1], 100); let approve = await token.approve(accounts[1], 100);
let allowance = await token.allowance(accounts[0], accounts[1]); let allowance = await token.allowance(accounts[0], accounts[1]);
assert.equal(allowance, 100); 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 token = await StandardTokenMock.new(accounts[0], 100);
let transfer = await token.transfer(accounts[1], 100); let transfer = await token.transfer(accounts[1], 100);
let balance0 = await token.balanceOf(accounts[0]); let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0); assert.equal(balance0, 0);
let balance1 = await token.balanceOf(accounts[1]); let balance1 = await token.balanceOf(accounts[1]);
assert.equal(balance1, 100); 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); let token = await StandardTokenMock.new(accounts[0], 100);
try { try {
let transfer = await token.transfer(accounts[1], 101); let transfer = await token.transfer(accounts[1], 101);
} catch(error) { } catch(error) {
if (error.message.search('invalid JUMP') == -1) throw error assertJump(error);
assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
done();
} }
}); });
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 token = await StandardTokenMock.new(accounts[0], 100);
let approve = await token.approve(accounts[1], 100); let approve = await token.approve(accounts[1], 100);
let transferFrom = await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]}); 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]); let balance2 = await token.balanceOf(accounts[1]);
assert.equal(balance2, 0); 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 token = await StandardTokenMock.new();
let approve = await token.approve(accounts[1], 99); let approve = await token.approve(accounts[1], 99);
try { try {
let transfer = await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]}); let transfer = await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]});
} catch (error) { } catch (error) {
if (error.message.search('invalid JUMP') == -1) throw error assertJump(error);
assert.isAbove(error.message.search('invalid JUMP'), -1, 'Invalid JUMP error must be returned');
done();
} }
}); });

@ -1,52 +1,52 @@
contract('Stoppable', function(accounts) { 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 stoppable = await StoppableMock.new();
let count0 = await stoppable.count(); let count0 = await stoppable.count();
assert.equal(count0, 0); assert.equal(count0, 0);
let normalProcess = await stoppable.normalProcess(); let normalProcess = await stoppable.normalProcess();
let count1 = await stoppable.count(); let count1 = await stoppable.count();
assert.equal(count1, 1); 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 stoppable = await StoppableMock.new();
let emergencyStop = await stoppable.emergencyStop(); let emergencyStop = await stoppable.emergencyStop();
let count0 = await stoppable.count(); let count0 = await stoppable.count();
assert.equal(count0, 0); assert.equal(count0, 0);
let normalProcess = await stoppable.normalProcess(); let normalProcess = await stoppable.normalProcess();
let count1 = await stoppable.count(); let count1 = await stoppable.count();
assert.equal(count1, 0); 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 stoppable = await StoppableMock.new();
let drasticMeasure = await stoppable.drasticMeasure(); let drasticMeasure = await stoppable.drasticMeasure();
let drasticMeasureTaken = await stoppable.drasticMeasureTaken(); let drasticMeasureTaken = await stoppable.drasticMeasureTaken();
assert.isFalse(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 stoppable = await StoppableMock.new();
let emergencyStop = await stoppable.emergencyStop(); let emergencyStop = await stoppable.emergencyStop();
let drasticMeasure = await stoppable.drasticMeasure(); let drasticMeasure = await stoppable.drasticMeasure();
let drasticMeasureTaken = await stoppable.drasticMeasureTaken(); let drasticMeasureTaken = await stoppable.drasticMeasureTaken();
assert.isTrue(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 stoppable = await StoppableMock.new();
let emergencyStop = await stoppable.emergencyStop(); let emergencyStop = await stoppable.emergencyStop();
let release = await stoppable.release(); let release = await stoppable.release();
let normalProcess = await stoppable.normalProcess(); let normalProcess = await stoppable.normalProcess();
let count0 = await stoppable.count(); let count0 = await stoppable.count();
assert.equal(count0, 1); assert.equal(count0, 1);
done();
}); });
}); });

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