Skip to content

Commit

Permalink
Added the LcovError class
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Mar 5, 2018
1 parent 0cb4901 commit d847e3e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Version [4.3.0](https://github.com/cedx/lcov.js/compare/v4.2.0...v4.3.0)
- Added the `LcovError` class.

## Version [4.2.0](https://github.com/cedx/lcov.js/compare/v4.1.0...v4.2.0)
- Added a user guide based on [MkDocs](http://www.mkdocs.org).
- Updated the build system to [Gulp](https://gulpjs.com) version 4.
Expand Down
11 changes: 7 additions & 4 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ path: blob/master/lib
source: report.js

# Usage
**LCOV Reports for Dart** provides a set of classes representing a [LCOV](http://ltp.sourceforge.net/coverage/lcov.php) coverage report and its data.
**LCOV Reports for JS** provides a set of classes representing a [LCOV](http://ltp.sourceforge.net/coverage/lcov.php) coverage report and its data.
The `Report` class, the main one, provides the parsing and formatting features.

### Parse coverage data from a LCOV file
## Parse coverage data from a LCOV file
The `Report.fromCoverage()` static method parses a [LCOV](http://ltp.sourceforge.net/coverage/lcov.php) coverage report provided as string, and creates a `Report` instance giving detailed information about this coverage report:

```js
Expand All @@ -24,11 +24,14 @@ async function main() {
}

catch (error) {
console.log('The LCOV report has an invalid format');
console.log(`An error occurred: ${error.message}`);
}
}
```

!!! info
A `LcovError` is thrown if any error occurred while parsing the coverage report.

The `Report.toJson()` instance method will return a map like this:

```json
Expand Down Expand Up @@ -62,7 +65,7 @@ The `Report.toJson()` instance method will return a map like this:
}
```

### Format coverage data to the LCOV format
## Format coverage data to the LCOV format
Each provided class has a dedicated `toString()` instance method returning the corresponding data formatted as [LCOV](http://ltp.sourceforge.net/coverage/lcov.php) string.
All you have to do is to create the adequate structure using these different classes, and to export the final result:

Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {BranchCoverage, BranchData} = require('./branch.js');
const {FunctionCoverage, FunctionData} = require('./function.js');
const {LineCoverage, LineData} = require('./line.js');
const {Record} = require('./record.js');
const {Report} = require('./report.js');
const {LcovError, Report} = require('./report.js');
const {Token} = require('./token.js');

module.exports = {
Expand All @@ -14,6 +14,7 @@ module.exports = {
FunctionData,
LineCoverage,
LineData,
LcovError,
Record,
Report,
Token
Expand Down
33 changes: 31 additions & 2 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ const {LineCoverage, LineData} = require('./line.js');
const {Record} = require('./record.js');
const {Token} = require('./token.js');

/**
* An exception caused by a parsing error.
*/
class LcovError extends TypeError {

/**
* Creates a new LCOV error.
* @param {string} message A message describing the error.
*/
constructor(message) {
super(message);

/**
* The error name.
* @type {string}
*/
this.name = 'LcovError';
}

/**
* Returns a string representation of this object.
* @return {string} The string representation of this object.
*/
toString() {
return `${this.name}("${this.message}")`;
}
}

/**
* Represents a trace file, that is a coverage report.
*/
Expand Down Expand Up @@ -138,10 +166,10 @@ class Report {
}

catch (err) {
throw new Error('The coverage data has an invalid LCOV format');
throw new LcovError('The coverage data has an invalid LCOV format');
}

if (!report.records.length) throw new Error('The coverage data is empty');
if (!report.records.length) throw new LcovError('The coverage data is empty');
return report;
}

Expand Down Expand Up @@ -180,4 +208,5 @@ class Report {
}

// Module exports.
exports.LcovError = LcovError;
exports.Report = Report;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"main": "./lib/index.js",
"name": "@cedx/lcov",
"repository": "cedx/lcov.js",
"version": "4.2.0",
"version": "4.3.0",
"devDependencies": {
"@cedx/coveralls": "^5.1.0",
"@cedx/gulp-david": "^11.0.0",
Expand Down
6 changes: 3 additions & 3 deletions test/report_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const {expect} = require('chai');
const {BranchData, FunctionData, LineData, Record, Report} = require('../lib/index.js');
const {BranchData, FunctionData, LineData, LcovError, Record, Report} = require('../lib/index.js');

/**
* @test {Report}
Expand Down Expand Up @@ -121,11 +121,11 @@ end_of_record
});

it('should throw an error if the input is invalid', () => {
expect(() => Report.fromCoverage('ZZ')).to.throw('invalid LCOV format');
expect(() => Report.fromCoverage('ZZ')).to.throw(LcovError, 'invalid LCOV format');
});

it('should throw an error if the report is empty', () => {
expect(() => Report.fromCoverage('TN:Example')).to.throw('coverage data is empty');
expect(() => Report.fromCoverage('TN:Example')).to.throw(LcovError, 'coverage data is empty');
});
});

Expand Down

0 comments on commit d847e3e

Please sign in to comment.