1 | /*
|
---|
2 | Copyright 2012-2015, Yahoo Inc.
|
---|
3 | Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
---|
4 | */
|
---|
5 | 'use strict';
|
---|
6 |
|
---|
7 | const percent = require('./percent');
|
---|
8 | const dataProperties = require('./data-properties');
|
---|
9 |
|
---|
10 | function blankSummary() {
|
---|
11 | const empty = () => ({
|
---|
12 | total: 0,
|
---|
13 | covered: 0,
|
---|
14 | skipped: 0,
|
---|
15 | pct: 'Unknown'
|
---|
16 | });
|
---|
17 |
|
---|
18 | return {
|
---|
19 | lines: empty(),
|
---|
20 | statements: empty(),
|
---|
21 | functions: empty(),
|
---|
22 | branches: empty()
|
---|
23 | };
|
---|
24 | }
|
---|
25 |
|
---|
26 | // asserts that a data object "looks like" a summary coverage object
|
---|
27 | function assertValidSummary(obj) {
|
---|
28 | const valid =
|
---|
29 | obj && obj.lines && obj.statements && obj.functions && obj.branches;
|
---|
30 | if (!valid) {
|
---|
31 | throw new Error(
|
---|
32 | 'Invalid summary coverage object, missing keys, found:' +
|
---|
33 | Object.keys(obj).join(',')
|
---|
34 | );
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * CoverageSummary provides a summary of code coverage . It exposes 4 properties,
|
---|
40 | * `lines`, `statements`, `branches`, and `functions`. Each of these properties
|
---|
41 | * is an object that has 4 keys `total`, `covered`, `skipped` and `pct`.
|
---|
42 | * `pct` is a percentage number (0-100).
|
---|
43 | */
|
---|
44 | class CoverageSummary {
|
---|
45 | /**
|
---|
46 | * @constructor
|
---|
47 | * @param {Object|CoverageSummary} [obj=undefined] an optional data object or
|
---|
48 | * another coverage summary to initialize this object with.
|
---|
49 | */
|
---|
50 | constructor(obj) {
|
---|
51 | if (!obj) {
|
---|
52 | this.data = blankSummary();
|
---|
53 | } else if (obj instanceof CoverageSummary) {
|
---|
54 | this.data = obj.data;
|
---|
55 | } else {
|
---|
56 | this.data = obj;
|
---|
57 | }
|
---|
58 | assertValidSummary(this.data);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * merges a second summary coverage object into this one
|
---|
63 | * @param {CoverageSummary} obj - another coverage summary object
|
---|
64 | */
|
---|
65 | merge(obj) {
|
---|
66 | const keys = ['lines', 'statements', 'branches', 'functions'];
|
---|
67 | keys.forEach(key => {
|
---|
68 | this[key].total += obj[key].total;
|
---|
69 | this[key].covered += obj[key].covered;
|
---|
70 | this[key].skipped += obj[key].skipped;
|
---|
71 | this[key].pct = percent(this[key].covered, this[key].total);
|
---|
72 | });
|
---|
73 |
|
---|
74 | return this;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * returns a POJO that is JSON serializable. May be used to get the raw
|
---|
79 | * summary object.
|
---|
80 | */
|
---|
81 | toJSON() {
|
---|
82 | return this.data;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * return true if summary has no lines of code
|
---|
87 | */
|
---|
88 | isEmpty() {
|
---|
89 | return this.lines.total === 0;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | dataProperties(CoverageSummary, [
|
---|
94 | 'lines',
|
---|
95 | 'statements',
|
---|
96 | 'functions',
|
---|
97 | 'branches'
|
---|
98 | ]);
|
---|
99 |
|
---|
100 | module.exports = {
|
---|
101 | CoverageSummary
|
---|
102 | };
|
---|