You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.2 KiB
105 lines
4.2 KiB
8 years ago
|
|
||
|
/**
|
||
|
* This file contains methods that produce a coverage map to pass to instanbul
|
||
|
* from data generated by `instrumentSolidity.js`
|
||
|
*/
|
||
|
const SolidityCoder = require('web3/lib/solidity/coder.js');
|
||
|
const path = require('path');
|
||
8 years ago
|
const keccak = require('keccakjs');
|
||
8 years ago
|
|
||
|
/**
|
||
|
* Converts solcover event data into an object that can be
|
||
|
* be passed to instanbul to produce coverage reports.
|
||
|
* @type {CoverageMap}
|
||
|
*/
|
||
|
module.exports = class CoverageMap {
|
||
|
|
||
|
constructor() {
|
||
|
this.coverage = {};
|
||
8 years ago
|
this.lineTopics = [];
|
||
|
this.functionTopics = [];
|
||
|
this.branchTopics = [];
|
||
|
this.statementTopics = [];
|
||
8 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Initializes a coverage map object for contract instrumented per `info` and located
|
||
|
* at `canonicalContractPath`
|
||
|
* @param {Object} info `info = getIntrumentedVersion(contract, fileName, true)`
|
||
|
* @param {String} canonicalContractPath target file location
|
||
|
* @return {Object} coverage map with all values set to zero
|
||
|
*/
|
||
8 years ago
|
|
||
8 years ago
|
addContract(info, canonicalContractPath) {
|
||
|
this.coverage[canonicalContractPath] = {
|
||
|
l: {},
|
||
|
path: canonicalContractPath,
|
||
|
s: {},
|
||
|
b: {},
|
||
|
f: {},
|
||
|
fnMap: {},
|
||
|
statementMap: {},
|
||
|
branchMap: {},
|
||
|
};
|
||
|
|
||
|
info.runnableLines.forEach((item, idx) => {
|
||
|
this.coverage[canonicalContractPath].l[info.runnableLines[idx]] = 0;
|
||
|
});
|
||
|
this.coverage[canonicalContractPath].fnMap = info.fnMap;
|
||
|
for (let x = 1; x <= Object.keys(info.fnMap).length; x++) {
|
||
|
this.coverage[canonicalContractPath].f[x] = 0;
|
||
|
}
|
||
|
this.coverage[canonicalContractPath].branchMap = info.branchMap;
|
||
|
for (let x = 1; x <= Object.keys(info.branchMap).length; x++) {
|
||
|
this.coverage[canonicalContractPath].b[x] = [0, 0];
|
||
|
}
|
||
|
this.coverage[canonicalContractPath].statementMap = info.statementMap;
|
||
|
for (let x = 1; x <= Object.keys(info.statementMap).length; x++) {
|
||
|
this.coverage[canonicalContractPath].s[x] = 0;
|
||
|
}
|
||
8 years ago
|
|
||
|
const keccakhex = (x => {
|
||
8 years ago
|
const hash = new keccak(256); // eslint-disable-line new-cap
|
||
|
hash.update(x);
|
||
|
return hash.digest('hex');
|
||
8 years ago
|
});
|
||
|
|
||
8 years ago
|
this.lineTopics.push(keccakhex('__Coverage' + info.contractName + '(string,uint256)'));
|
||
|
this.functionTopics.push(keccakhex('__FunctionCoverage' + info.contractName + '(string,uint256)'));
|
||
|
this.branchTopics.push(keccakhex('__BranchCoverage' + info.contractName + '(string,uint256,uint256)'));
|
||
|
this.statementTopics.push(keccakhex('__StatementCoverage' + info.contractName + '(string,uint256)'));
|
||
8 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Populates an empty coverage map with values derived from an array of events
|
||
|
* fired by instrumented contracts as they are tested
|
||
|
* @param {Array} events
|
||
|
* @param {String} relative path to host contracts eg: './../contracts'
|
||
|
* @return {Object} coverage map.
|
||
|
*/
|
||
|
generate(events, pathPrefix) {
|
||
|
for (let idx = 0; idx < events.length; idx++) {
|
||
|
const event = JSON.parse(events[idx]);
|
||
8 years ago
|
|
||
8 years ago
|
if (event.topics.filter(t => this.lineTopics.indexOf(t) >= 0).length > 0) {
|
||
8 years ago
|
const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', ''));
|
||
8 years ago
|
const canonicalContractPath = data[0];
|
||
8 years ago
|
this.coverage[canonicalContractPath].l[data[1].toNumber()] += 1;
|
||
8 years ago
|
} else if (event.topics.filter(t => this.functionTopics.indexOf(t) >= 0).length > 0) {
|
||
8 years ago
|
const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', ''));
|
||
8 years ago
|
const canonicalContractPath = data[0];
|
||
8 years ago
|
this.coverage[canonicalContractPath].f[data[1].toNumber()] += 1;
|
||
8 years ago
|
} else if (event.topics.filter(t => this.branchTopics.indexOf(t) >= 0).length > 0) {
|
||
8 years ago
|
const data = SolidityCoder.decodeParams(['string', 'uint256', 'uint256'], event.data.replace('0x', ''));
|
||
8 years ago
|
const canonicalContractPath = data[0];
|
||
8 years ago
|
this.coverage[canonicalContractPath].b[data[1].toNumber()][data[2].toNumber()] += 1;
|
||
8 years ago
|
} else if (event.topics.filter(t => this.statementTopics.indexOf(t) >= 0).length > 0) {
|
||
8 years ago
|
const data = SolidityCoder.decodeParams(['string', 'uint256'], event.data.replace('0x', ''));
|
||
8 years ago
|
const canonicalContractPath = data[0];
|
||
8 years ago
|
this.coverage[canonicalContractPath].s[data[1].toNumber()] += 1;
|
||
|
}
|
||
|
}
|
||
|
return Object.assign({}, this.coverage);
|
||
|
}
|
||
|
};
|