aboutsummaryrefslogtreecommitdiff
path: root/src/CoverageListing.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/CoverageListing.js')
-rw-r--r--src/CoverageListing.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/CoverageListing.js b/src/CoverageListing.js
new file mode 100644
index 0000000..8b7830c
--- /dev/null
+++ b/src/CoverageListing.js
@@ -0,0 +1,93 @@
1/*
2 * JaCoCo Report Viewer, a web-based coverage report viewer
3 * Copyright (C) 2018 Pacien TRAN-GIRARD
4 * Adam NAILI
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20import React, { Component } from 'react';
21
22export class CoverageListing extends Component {
23 constructor(props) {
24 super(props);
25 this.state = {
26 listingContent: null,
27 coverageMap: null
28 };
29 }
30
31 componentDidMount() {
32 this._updateState();
33 }
34
35 componentDidUpdate(prevProps) {
36 if (this.props.fileName === prevProps.fileName &&
37 this.props.sourceSet === prevProps.sourceSet &&
38 this.props.coverage === prevProps.coverage) return;
39
40 this._updateState();
41 }
42
43 _updateState() {
44 if (!this.props.sourceSet || !(this.props.fileName in this.props.sourceSet)) {
45 this.setState({ listingContent: null });
46 return;
47 }
48
49 this._updateListingContent();
50 this._updateCoverageMap();
51 }
52
53 _updateCoverageMap() {
54 const coverageMap = this.props.coverage.reduce((map, line) => (map[parseInt(line.$.nr)] = line.$, map), {});
55 this.setState({ coverageMap: coverageMap });
56 }
57
58 _updateListingContent() {
59 this.props.sourceSet[this.props.fileName]
60 .async('text')
61 .then(content => this.setState({ listingContent: content }));
62 }
63
64 _renderNone() {
65 return (<div>No source file provided.</div>);
66 }
67
68 _nonEmptyString(str) {
69 return str ? str : ' '; // workaround for empty <pre> collapsing
70 }
71
72 _renderLine(lineContent, lineNumber) {
73 if (!(lineNumber in this.state.coverageMap))
74 return (<li key={lineNumber}><pre>{lineContent}</pre></li>);
75
76 const coverage = this.state.coverageMap[lineNumber];
77 const wellCovered = parseInt(coverage.mi) === 0 && parseInt(coverage.mb) === 0;
78 return (<li key={lineNumber} well-covered={wellCovered.toString()}><pre>{lineContent}</pre></li>);
79 }
80
81 _renderListing() {
82 const lines = this.state.listingContent
83 .split('\n')
84 .map(this._nonEmptyString)
85 .map((line, index) => this._renderLine(line, index + 1));
86
87 return (<div className="listing"><ol>{lines}</ol></div>);
88 }
89
90 render() {
91 return this.state.listingContent ? this._renderListing() : this._renderNone();
92 }
93}