refactor testing error throwing

This commit is contained in:
Jakub Wojciechowski
2017-07-22 21:40:01 +01:00
parent 5e7847537a
commit b3f60b9320
10 changed files with 79 additions and 78 deletions

View File

@ -26,10 +26,10 @@ contract('BasicToken', function(accounts) {
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);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
}); });

View File

@ -25,11 +25,11 @@ contract('Claimable', function(accounts) {
it('should prevent to claimOwnership from no pendingOwner', async function() { it('should prevent to claimOwnership from no pendingOwner', async function() {
try { try {
await claimable.claimOwnership({from: accounts[2]}); await claimable.claimOwnership({from: accounts[2]});
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it('should prevent non-owners from transfering', async function() { it('should prevent non-owners from transfering', async function() {
@ -37,11 +37,11 @@ contract('Claimable', function(accounts) {
const owner = await claimable.owner.call(); const owner = await claimable.owner.call();
assert.isTrue(owner !== other); assert.isTrue(owner !== other);
try { try {
await claimable.transferOwnership(other, {from: other}); await claimable.transferOwnership(other, {from: other});
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
describe('after initiating a transfer', function () { describe('after initiating a transfer', function () {

View File

@ -35,11 +35,11 @@ contract('DayLimit', function(accounts) {
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
try { try {
await dayLimit.attemptSpend(3); await dayLimit.attemptSpend(3);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it('should allow spending if daily limit is reached and then set higher', async function() { it('should allow spending if daily limit is reached and then set higher', async function() {
@ -49,17 +49,17 @@ contract('DayLimit', function(accounts) {
try { try {
await dayLimit.attemptSpend(3); await dayLimit.attemptSpend(3);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
assertJump(error); assertJump(error);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.setDailyLimit(15);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
return assert.equal(spentToday, 11);
} }
assert.fail('should have thrown before'); spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.setDailyLimit(15);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 11);
}); });
it('should allow spending if daily limit is reached and then amount spent is reset', async function() { it('should allow spending if daily limit is reached and then amount spent is reset', async function() {
@ -69,17 +69,17 @@ contract('DayLimit', function(accounts) {
try { try {
await dayLimit.attemptSpend(3); await dayLimit.attemptSpend(3);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
assertJump(error); assertJump(error);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.resetSpentToday(15);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
return assert.equal(spentToday, 3);
} }
assert.fail('should have thrown before'); spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await dayLimit.resetSpentToday(15);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 3);
}); });
it('should allow spending if daily limit is reached and then the next has come', async function() { it('should allow spending if daily limit is reached and then the next has come', async function() {
@ -92,18 +92,18 @@ contract('DayLimit', function(accounts) {
try { try {
await dayLimit.attemptSpend(3); await dayLimit.attemptSpend(3);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
assertJump(error); assertJump(error);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await timer(day);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
return assert.equal(spentToday, 3);
} }
assert.fail('should have thrown before'); spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8);
await timer(day);
await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 3);
}); });
}); });

View File

@ -27,11 +27,11 @@ contract('LimitBalance', function(accounts) {
it('shouldnt allow sending above limit', async function() { it('shouldnt allow sending above limit', async function() {
let amount = 1110; let amount = 1110;
try { try {
await lb.limitedDeposit({value: amount}); await lb.limitedDeposit({value: amount});
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it('should allow multiple sends below limit', async function() { it('should allow multiple sends below limit', async function() {
@ -52,10 +52,10 @@ contract('LimitBalance', function(accounts) {
try { try {
await lb.limitedDeposit({value: amount+1}); await lb.limitedDeposit({value: amount+1});
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
}); });

View File

@ -29,10 +29,10 @@ contract('Ownable', function(accounts) {
assert.isTrue(owner !== other); assert.isTrue(owner !== other);
try { try {
await ownable.transferOwnership(other, {from: other}); await ownable.transferOwnership(other, {from: other});
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it('should guard ownership against stuck state', async function() { it('should guard ownership against stuck state', async function() {

View File

@ -23,12 +23,12 @@ contract('Pausable', function(accounts) {
try { try {
await Pausable.normalProcess(); await Pausable.normalProcess();
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
let count1 = await Pausable.count(); assertJump(error);
assert.equal(count1, 0);
return assertJump(error);
} }
assert.fail('should have thrown before'); let count1 = await Pausable.count();
assert.equal(count1, 0);
}); });
@ -36,12 +36,12 @@ contract('Pausable', function(accounts) {
let Pausable = await PausableMock.new(); let Pausable = await PausableMock.new();
try { try {
await Pausable.drasticMeasure(); await Pausable.drasticMeasure();
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
const drasticMeasureTaken = await Pausable.drasticMeasureTaken(); assertJump(error);
assert.isFalse(drasticMeasureTaken);
return assertJump(error);
} }
assert.fail('should have thrown before'); const drasticMeasureTaken = await Pausable.drasticMeasureTaken();
assert.isFalse(drasticMeasureTaken);
}); });
it('can take a drastic measure in a pause', async function() { it('can take a drastic measure in a pause', async function() {
@ -69,6 +69,7 @@ contract('Pausable', function(accounts) {
await Pausable.unpause(); await Pausable.unpause();
try { try {
await Pausable.drasticMeasure(); await Pausable.drasticMeasure();
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
assertJump(error); assertJump(error);
} }

View File

@ -55,19 +55,19 @@ contract('PausableToken', function(accounts) {
await token.pause(); await token.pause();
try { try {
await token.transfer(accounts[1], 100); await token.transfer(accounts[1], 100);
assert.fail('should have thrown before');
} catch (error) { } catch (error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it('should throw an error trying to transfer from another account while transactions are paused', async function() { it('should throw an error trying to transfer from another account while transactions are paused', async function() {
await token.pause(); await token.pause();
try { try {
await token.transferFrom(accounts[0], accounts[1], 100); await token.transferFrom(accounts[0], accounts[1], 100);
assert.fail('should have thrown before');
} catch (error) { } catch (error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
}) })

View File

@ -40,10 +40,10 @@ contract('SafeMath', function(accounts) {
let b = 5678; let b = 5678;
try { try {
let subtract = await safeMath.subtract(a, b); let subtract = await safeMath.subtract(a, b);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it("should throw an error on addition overflow", async function() { it("should throw an error on addition overflow", async function() {
@ -51,10 +51,10 @@ contract('SafeMath', function(accounts) {
let b = 1; let b = 1;
try { try {
let add = await safeMath.add(a, b); let add = await safeMath.add(a, b);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it("should throw an error on multiplication overflow", async function() { it("should throw an error on multiplication overflow", async function() {
@ -62,10 +62,10 @@ contract('SafeMath', function(accounts) {
let b = 2; let b = 2;
try { try {
let multiply = await safeMath.multiply(a, b); let multiply = await safeMath.multiply(a, b);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
}); });

View File

@ -39,10 +39,10 @@ contract('StandardToken', function(accounts) {
let token = await StandardTokenMock.new(accounts[0], 100); let token = await StandardTokenMock.new(accounts[0], 100);
try { try {
await token.transfer(accounts[1], 101); await token.transfer(accounts[1], 101);
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
it('should return correct balances after transfering from another account', async function() { it('should return correct balances after transfering from another account', async function() {
@ -64,10 +64,10 @@ contract('StandardToken', function(accounts) {
await token.approve(accounts[1], 99); await token.approve(accounts[1], 99);
try { try {
await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]}); await token.transferFrom(accounts[0], accounts[2], 100, {from: accounts[1]});
assert.fail('should have thrown before');
} catch (error) { } catch (error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}); });
}); });

View File

@ -46,20 +46,20 @@ contract('VestedToken', function(accounts) {
it('throws when trying to transfer non vested tokens', async () => { it('throws when trying to transfer non vested tokens', async () => {
try { try {
await token.transfer(accounts[7], 1, { from: receiver }) await token.transfer(accounts[7], 1, { from: receiver })
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}) })
it('throws when trying to transfer from non vested tokens', async () => { it('throws when trying to transfer from non vested tokens', async () => {
try { try {
await token.approve(accounts[7], 1, { from: receiver }) await token.approve(accounts[7], 1, { from: receiver })
await token.transferFrom(receiver, accounts[7], tokenAmount, { from: accounts[7] }) await token.transferFrom(receiver, accounts[7], tokenAmount, { from: accounts[7] })
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}) })
it('can be revoked by granter', async () => { it('can be revoked by granter', async () => {
@ -71,10 +71,10 @@ contract('VestedToken', function(accounts) {
it('cannot be revoked by non granter', async () => { it('cannot be revoked by non granter', async () => {
try { try {
await token.revokeTokenGrant(receiver, 0, { from: accounts[3] }); await token.revokeTokenGrant(receiver, 0, { from: accounts[3] });
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}) })
it('can be revoked by granter and non vested tokens are returned', async () => { it('can be revoked by granter and non vested tokens are returned', async () => {
@ -131,10 +131,10 @@ contract('VestedToken', function(accounts) {
it('throws when granter attempts to revoke', async () => { it('throws when granter attempts to revoke', async () => {
try { try {
await token.revokeTokenGrant(receiver, 0, { from: granter }); await token.revokeTokenGrant(receiver, 0, { from: granter });
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}) })
}) })
@ -160,10 +160,10 @@ contract('VestedToken', function(accounts) {
it('cannot be revoked by non granter', async () => { it('cannot be revoked by non granter', async () => {
try { try {
await token.revokeTokenGrant(receiver, 0, { from: accounts[3] }); await token.revokeTokenGrant(receiver, 0, { from: accounts[3] });
assert.fail('should have thrown before');
} catch(error) { } catch(error) {
return assertJump(error); assertJump(error);
} }
assert.fail('should have thrown before');
}) })
it('can be revoked by granter and non vested tokens are returned', async () => { it('can be revoked by granter and non vested tokens are returned', async () => {