Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Start counting blocks at hook end, report gas on failure #4

Merged
merged 1 commit into from
Oct 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -36,28 +47,20 @@ 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');
console.log(fmt, test.title);
});

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() +
Expand All @@ -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));
Expand Down