forked from istanbuljs/istanbuljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: modernize istanbul-lib-coverage (istanbuljs#452)
* Convert CoverageMap to class * Convert CoverageSummary to class * Convert FileCoverage to class * Enforce full coverage
- Loading branch information
1 parent
adf6972
commit b348a06
Showing
11 changed files
with
513 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}); | ||
}); | ||
}; |
Oops, something went wrong.