Browse Source

Brought Truffle to 4.0.4 to be able to properly use asserts on revert.

Simon de la Rouviere 6 years ago
parent
commit
d0c1a27a96
6 changed files with 36 additions and 25 deletions
  1. 3 0
      .babelrc
  2. 6 1
      package.json
  3. 16 12
      test/eip20/eip20.js
  4. 9 0
      test/helpers/assertRevert.js
  5. 0 12
      test/utils.js
  6. 2 0
      truffle.js

+ 3 - 0
.babelrc

@@ -0,0 +1,3 @@
+{
+  "presets": ["es2015", "stage-2", "stage-3"]
+}

+ 6 - 1
package.json

@@ -33,10 +33,15 @@
   },
   "homepage": "https://github.com/ConsenSys/Tokens#readme",
   "dependencies": {
-    "truffle": "4.0.1",
+    "truffle": "4.0.4",
     "truffle-hdwallet-provider": "0.0.3"
   },
   "devDependencies": {
+    "babel-polyfill": "^6.26.0",
+    "babel-preset-es2015": "^6.24.1",
+    "babel-preset-stage-2": "^6.24.1",
+    "babel-preset-stage-3": "^6.24.1",
+    "babel-register": "^6.26.0",
     "eslint": "4.13.1",
     "eslint-config-airbnb": "16.1.0",
     "eslint-plugin-import": "2.8.0",

+ 16 - 12
test/eip20/eip20.js

@@ -1,4 +1,4 @@
-const { expectThrow } = require('../utils');
+import assertRevert from '../helpers/assertRevert';
 
 const EIP20Abstraction = artifacts.require('EIP20');
 let HST;
@@ -38,15 +38,15 @@ contract('EIP20', (accounts) => {
     const balanceBefore = await HST.balanceOf.call(accounts[0]);
     assert.strictEqual(balanceBefore.toNumber(), 10000);
 
-    web3.eth.sendTransaction({ from: accounts[0], to: HST.address, value: web3.toWei('10', 'Ether') }, async (err, res) => {
-      expectThrow(new Promise((resolve, reject) => {
-        if (err) reject(err);
+    await assertRevert(new Promise((resolve, reject) => {
+      web3.eth.sendTransaction({ from: accounts[0], to: HST.address, value: web3.toWei('10', 'Ether') }, (err, res) => {
+        if (err) { reject(err); }
         resolve(res);
-      }));
+      });
+    }));
 
-      const balanceAfter = await HST.balanceOf.call(accounts[0]);
-      assert.strictEqual(balanceAfter.toNumber(), 10000);
-    });
+    const balanceAfter = await HST.balanceOf.call(accounts[0]);
+    assert.strictEqual(balanceAfter.toNumber(), 10000);
   });
 
   it('transfers: should transfer 10000 to accounts[1] with accounts[0] having 10000', async () => {
@@ -55,7 +55,9 @@ contract('EIP20', (accounts) => {
     assert.strictEqual(balance.toNumber(), 10000);
   });
 
-  it('transfers: should fail when trying to transfer 10001 to accounts[1] with accounts[0] having 10000', () => expectThrow(HST.transfer.call(accounts[1], 10001, { from: accounts[0] })));
+  it('transfers: should fail when trying to transfer 10001 to accounts[1] with accounts[0] having 10000', async () => {
+    await assertRevert(HST.transfer.call(accounts[1], 10001, { from: accounts[0] }));
+  });
 
   it('transfers: should handle zero-transfers normally', async () => {
     assert(await HST.transfer.call(accounts[1], 0, { from: accounts[0] }), 'zero-transfer has failed');
@@ -140,16 +142,18 @@ contract('EIP20', (accounts) => {
 
     // FIRST tx done.
     // onto next.
-    expectThrow(HST.transferFrom.call(accounts[0], accounts[2], 60, { from: accounts[1] }));
+    await assertRevert(HST.transferFrom.call(accounts[0], accounts[2], 60, { from: accounts[1] }));
   });
 
-  it('approvals: attempt withdrawal from account with no allowance (should fail)', () => expectThrow(HST.transferFrom.call(accounts[0], accounts[2], 60, { from: accounts[1] })));
+  it('approvals: attempt withdrawal from account with no allowance (should fail)', async () => {
+    await assertRevert(HST.transferFrom.call(accounts[0], accounts[2], 60, { from: accounts[1] }));
+  });
 
   it('approvals: allow accounts[1] 100 to withdraw from accounts[0]. Withdraw 60 and then approve 0 & attempt transfer.', async () => {
     await HST.approve(accounts[1], 100, { from: accounts[0] });
     await HST.transferFrom(accounts[0], accounts[2], 60, { from: accounts[1] });
     await HST.approve(accounts[1], 0, { from: accounts[0] });
-    expectThrow(HST.transferFrom.call(accounts[0], accounts[2], 10, { from: accounts[1] }));
+    await assertRevert(HST.transferFrom.call(accounts[0], accounts[2], 10, { from: accounts[1] }));
   });
 
   it('approvals: approve max (2^256 - 1)', async () => {

+ 9 - 0
test/helpers/assertRevert.js

@@ -0,0 +1,9 @@
+export default async (promise) => {
+  try {
+    await promise;
+    assert.fail('Expected revert not received');
+  } catch (error) {
+    const revertFound = error.message.search('revert') >= 0;
+    assert(revertFound, `Expected "revert", got ${error} instead`);
+  }
+};

+ 0 - 12
test/utils.js

@@ -1,12 +0,0 @@
-module.exports = {
-  expectThrow: async (promise) => {
-    const errMsg = 'Expected throw not received';
-    try {
-      await promise;
-    } catch (err) {
-      assert(err.toString().includes('invalid opcode'), errMsg);
-      return;
-    }
-    assert.fail(errMsg);
-  },
-};

+ 2 - 0
truffle.js

@@ -1,5 +1,7 @@
 const HDWalletProvider = require('truffle-hdwallet-provider')
 const fs = require('fs')
+require('babel-register')
+require('babel-polyfill')
 
 // First read in the secrets.json to get our mnemonic
 let secrets