123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- '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()
- };
- }
- 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(',')
- );
- }
- }
- class CoverageSummary {
-
- constructor(obj) {
- if (!obj) {
- this.data = blankSummary();
- } else if (obj instanceof CoverageSummary) {
- this.data = obj.data;
- } else {
- this.data = obj;
- }
- assertValidSummary(this.data);
- }
-
- 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;
- }
-
- toJSON() {
- return this.data;
- }
-
- isEmpty() {
- return this.lines.total === 0;
- }
- }
- dataProperties(CoverageSummary, [
- 'lines',
- 'statements',
- 'functions',
- 'branches'
- ]);
- module.exports = {
- CoverageSummary
- };
|