Skip to content

Commit

Permalink
chore: modernize istanbul-lib-coverage (istanbuljs#452)
Browse files Browse the repository at this point in the history
* Convert CoverageMap to class
* Convert CoverageSummary to class
* Convert FileCoverage to class
* Enforce full coverage
  • Loading branch information
coreyfarrell committed Jul 23, 2019
1 parent adf6972 commit b348a06
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 448 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*
* @module Exports
*/
const CoverageSummary = require('./lib/file').CoverageSummary;
const FileCoverage = require('./lib/file').FileCoverage;
const CoverageMap = require('./lib/coverage-map').CoverageMap;
const { FileCoverage } = require('./lib/file-coverage');
const { CoverageMap } = require('./lib/coverage-map');
const { CoverageSummary } = require('./lib/coverage-summary');

module.exports = {
/**
Expand Down
213 changes: 111 additions & 102 deletions lib/coverage-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,130 @@
*/
'use strict';

const FileCoverage = require('./file').FileCoverage;
const CoverageSummary = require('./file').CoverageSummary;
const { FileCoverage } = require('./file-coverage');
const { CoverageSummary } = require('./coverage-summary');

function maybeConstruct(obj, klass) {
if (obj instanceof klass) {
return obj;
}

return new klass(obj);
}

function loadMap(source) {
const data = Object.create(null);
if (!source) {
return data;
}

Object.entries(source).forEach(([k, cov]) => {
if (cov instanceof FileCoverage) {
data[k] = cov;
} else {
data[k] = new FileCoverage(cov);
}
data[k] = maybeConstruct(cov, FileCoverage);
});

return data;
}
/**
* CoverageMap is a map of `FileCoverage` objects keyed by file paths.
* @param {Object} [obj=undefined] obj A coverage map from which to initialize this
* map's contents. This can be the raw global coverage object.
* @constructor
*/
function CoverageMap(obj) {
if (!obj) {
this.data = Object.create(null);
} else if (obj instanceof CoverageMap) {
this.data = obj.data;
} else {
this.data = loadMap(obj);

/** CoverageMap is a map of `FileCoverage` objects keyed by file paths. */
class CoverageMap {
/**
* @constructor
* @param {Object} [obj=undefined] obj A coverage map from which to initialize this
* map's contents. This can be the raw global coverage object.
*/
constructor(obj) {
if (obj instanceof CoverageMap) {
this.data = obj.data;
} else {
this.data = loadMap(obj);
}
}
}
/**
* merges a second coverage map into this one
* @param {CoverageMap} obj - a CoverageMap or its raw data. Coverage is merged
* correctly for the same files and additional file coverage keys are created
* as needed.
*/
CoverageMap.prototype.merge = function(obj) {
let other;
if (obj instanceof CoverageMap) {
other = obj;
} else {
other = new CoverageMap(obj);

/**
* merges a second coverage map into this one
* @param {CoverageMap} obj - a CoverageMap or its raw data. Coverage is merged
* correctly for the same files and additional file coverage keys are created
* as needed.
*/
merge(obj) {
const other = maybeConstruct(obj, CoverageMap);
Object.values(other.data).forEach(fc => {
this.addFileCoverage(fc);
});
}
Object.entries(other.data).forEach(([k, fc]) => {
if (this.data[k]) {
this.data[k].merge(fc);
} else {
this.data[k] = fc;

/**
* filter the coveragemap based on the callback provided
* @param {Function (filename)} callback - Returns true if the path
* should be included in the coveragemap. False if it should be
* removed.
*/
filter(callback) {
Object.keys(this.data).forEach(k => {
if (!callback(k)) {
delete this.data[k];
}
});
}

/**
* returns a JSON-serializable POJO for this coverage map
* @returns {Object}
*/
toJSON() {
return this.data;
}

/**
* returns an array for file paths for which this map has coverage
* @returns {Array{string}} - array of files
*/
files() {
return Object.keys(this.data);
}

/**
* returns the file coverage for the specified file.
* @param {String} file
* @returns {FileCoverage}
*/
fileCoverageFor(file) {
const fc = this.data[file];
if (!fc) {
throw new Error(`No file coverage available for: ${file}`);
}
});
};
/**
* filter the coveragemap based on the callback provided
* @param {Function (filename)} callback - Returns true if the path
* should be included in the coveragemap. False if it should be
* removed.
*/
CoverageMap.prototype.filter = function(callback) {
Object.keys(this.data).forEach(k => {
if (!callback(k)) {
delete this.data[k];
return fc;
}

/**
* adds a file coverage object to this map. If the path for the object,
* already exists in the map, it is merged with the existing coverage
* otherwise a new key is added to the map.
* @param {FileCoverage} fc the file coverage to add
*/
addFileCoverage(fc) {
const cov = new FileCoverage(fc);
const { path } = cov;
if (this.data[path]) {
this.data[path].merge(cov);
} else {
this.data[path] = cov;
}
});
};
/**
* returns a JSON-serializable POJO for this coverage map
* @returns {Object}
*/
CoverageMap.prototype.toJSON = function() {
return this.data;
};
/**
* returns an array for file paths for which this map has coverage
* @returns {Array{string}} - array of files
*/
CoverageMap.prototype.files = function() {
return Object.keys(this.data);
};
/**
* returns the file coverage for the specified file.
* @param {String} file
* @returns {FileCoverage}
*/
CoverageMap.prototype.fileCoverageFor = function(file) {
const fc = this.data[file];
if (!fc) {
throw new Error('No file coverage available for: ' + file);
}
return fc;
};
/**
* adds a file coverage object to this map. If the path for the object,
* already exists in the map, it is merged with the existing coverage
* otherwise a new key is added to the map.
* @param {FileCoverage} fc the file coverage to add
*/
CoverageMap.prototype.addFileCoverage = function(fc) {
const cov = new FileCoverage(fc);
const path = cov.path;
if (this.data[path]) {
this.data[path].merge(cov);
} else {
this.data[path] = cov;

/**
* returns the coverage summary for all the file coverage objects in this map.
* @returns {CoverageSummary}
*/
getCoverageSummary() {
const ret = new CoverageSummary();
Object.values(this.data).forEach(fc => {
ret.merge(fc.toSummary());
});

return ret;
}
};
/**
* returns the coverage summary for all the file coverage objects in this map.
* @returns {CoverageSummary}
*/
CoverageMap.prototype.getCoverageSummary = function() {
const ret = new CoverageSummary();
this.files().forEach(key => {
ret.merge(this.fileCoverageFor(key).toSummary());
});
return ret;
};
}

module.exports = {
CoverageMap
Expand Down
102 changes: 102 additions & 0 deletions lib/coverage-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';

const percent = require('./percent');
const dataProperties = require('./data-properties');

function blankSummary() {
const empty = () => ({
total: 0,
covered: 0,
skipped: 0,
pct: 'Unknown'
});

return {
lines: empty(),
statements: empty(),
functions: empty(),
branches: empty()
};
}

// asserts that a data object "looks like" a summary coverage object
function assertValidSummary(obj) {
const valid =
obj && obj.lines && obj.statements && obj.functions && obj.branches;
if (!valid) {
throw new Error(
'Invalid summary coverage object, missing keys, found:' +
Object.keys(obj).join(',')
);
}
}

/**
* CoverageSummary provides a summary of code coverage . It exposes 4 properties,
* `lines`, `statements`, `branches`, and `functions`. Each of these properties
* is an object that has 4 keys `total`, `covered`, `skipped` and `pct`.
* `pct` is a percentage number (0-100).
*/
class CoverageSummary {
/**
* @constructor
* @param {Object|CoverageSummary} [obj=undefined] an optional data object or
* another coverage summary to initialize this object with.
*/
constructor(obj) {
if (!obj) {
this.data = blankSummary();
} else if (obj instanceof CoverageSummary) {
this.data = obj.data;
} else {
this.data = obj;
}
assertValidSummary(this.data);
}

/**
* merges a second summary coverage object into this one
* @param {CoverageSummary} obj - another coverage summary object
*/
merge(obj) {
const keys = ['lines', 'statements', 'branches', 'functions'];
keys.forEach(key => {
this[key].total += obj[key].total;
this[key].covered += obj[key].covered;
this[key].skipped += obj[key].skipped;
this[key].pct = percent(this[key].covered, this[key].total);
});

return this;
}

/**
* returns a POJO that is JSON serializable. May be used to get the raw
* summary object.
*/
toJSON() {
return this.data;
}

/**
* return true if summary has no lines of code
*/
isEmpty() {
return this.lines.total === 0;
}
}

dataProperties(CoverageSummary, [
'lines',
'statements',
'functions',
'branches'
]);

module.exports = {
CoverageSummary
};
12 changes: 12 additions & 0 deletions lib/data-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = function dataProperties(klass, properties) {
properties.forEach(p => {
Object.defineProperty(klass.prototype, p, {
enumerable: true,
get() {
return this.data[p];
}
});
});
};

0 comments on commit b348a06

Please sign in to comment.