From 6b3fcef3b00a4a5e21b4aab1816e06d6461cc434 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Sun, 1 Oct 2017 18:22:36 -0700 Subject: [PATCH] Start counting blocks at hook end, report gas on failure --- index.js | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index b28a43e..6f99010 100644 --- a/index.js +++ b/index.js @@ -13,13 +13,24 @@ function Gas (runner) { var indents = 0; var n = 0; var startBlock; - var endBlock; - var gasUsed = 0; function indent () { return Array(indents).join(' '); } + function calculateGasUsed(){ + let gasUsed = 0; + const endBlock = web3.eth.blockNumber; + while(startBlock <= endBlock){ + let block = web3.eth.getBlock(startBlock); + if (block) + gasUsed += block.gasUsed; + + startBlock++; + } + return gasUsed; + }; + runner.on('start', function () { console.log(); }); @@ -36,9 +47,11 @@ function Gas (runner) { } }); - runner.on('test', function() { + runner.on('hook end', function () { startBlock = web3.eth.blockNumber + 1; - }); + }) + + runner.on('test', function() {}); runner.on('pending', function (test) { var fmt = indent() + color('pending', ' - %s'); @@ -46,18 +59,8 @@ function Gas (runner) { }); runner.on('pass', function (test) { - var fmt; - - gasUsed = 0; - endBlock = web3.eth.blockNumber; - while(startBlock <= endBlock){ - var block = web3.eth.getBlock(startBlock); - - if (block) - gasUsed += block.gasUsed; - - startBlock++; - } + let fmt; + let gasUsed = calculateGasUsed(); if (test.speed === 'fast') { fmt = indent() + @@ -76,7 +79,13 @@ function Gas (runner) { }); runner.on('fail', function (test) { - console.log(indent() + color('fail', ' %d) %s'), ++n, test.title); + let gasUsed = calculateGasUsed(); + let fmt = indent() + + color('fail', ' %d) %s') + + color('pass', ' (%d gas)'); + console.log() + + console.log(fmt, ++n, test.title, gasUsed); }); runner.on('end', self.epilogue.bind(self));