Skip to content

Commit

Permalink
Renamed the Report.parse() static method to fromCoverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Aug 1, 2017
1 parent 9d721ab commit a883c04
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ This file contains highlights of what changes on each version of the [LCOV Repor

## Version 3.0.0
- Breaking change: renamed the `fromJSON()` static methods to `fromJson`.
- Breaking change: renamed the `Report.parse()` static method to `fromCoverage`.
- Changed the naming convention: acronyms and abbreviations are capitalized like regular words, except for two-letter acronyms.
- Updated the package dependencies.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This package provides a set of classes representing a coverage report and its da
The [`Report`](https://github.com/cedx/lcov.js/blob/master/src/report.js) class, the main one, provides the parsing and formatting features.

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

```javascript
const {Report} = require('@cedx/lcov');
Expand All @@ -30,7 +30,7 @@ try {
const loadReport = promisify(readFile);

let coverage = await loadReport('lcov.info', 'utf8');
let report = Report.parse(coverage);
let report = Report.fromCoverage(coverage);

console.log(`The coverage report contains ${report.records.length} records:`);
console.log(report.toJSON());
Expand Down
26 changes: 13 additions & 13 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,13 @@ exports.Report = class Report {
this.testName = testName;
}

/**
* Creates a new record from the specified JSON map.
* @param {object} map A JSON map representing a record.
* @return {Report} The instance corresponding to the specified JSON map, or `null` if a parsing error occurred.
*/
static fromJson(map) {
return !map || typeof map != 'object' ? null : new Report(
typeof map.testName == 'string' ? map.testName : '',
Array.isArray(map.records) ? map.records.map(item => Record.fromJson(item)).filter(item => item) : []
);
}

/**
* Parses the specified coverage data in [LCOV](http://ltp.sourceforge.net/coverage/lcov.php) format.
* @param {string} coverage The coverage data.
* @return {Report} The resulting coverage report.
* @throws {Error} A parsing error occurred.
*/
static parse(coverage) {
static fromCoverage(coverage) {
let report = new Report;

try {
Expand Down Expand Up @@ -157,6 +145,18 @@ exports.Report = class Report {
return report;
}

/**
* Creates a new record from the specified JSON map.
* @param {object} map A JSON map representing a record.
* @return {Report} The instance corresponding to the specified JSON map, or `null` if a parsing error occurred.
*/
static fromJson(map) {
return !map || typeof map != 'object' ? null : new Report(
typeof map.testName == 'string' ? map.testName : '',
Array.isArray(map.records) ? map.records.map(item => Record.fromJson(item)).filter(item => item) : []
);
}

/**
* Converts this object to a map in JSON format.
* @return {object} The map in JSON format corresponding to this object.
Expand Down
74 changes: 37 additions & 37 deletions test/report_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,19 @@ const {BranchData, FunctionData, LineData, Record, Report} = require('../lib');
describe('Report', () => {

/**
* @test {Report.fromJson}
*/
describe('.fromJson()', () => {
it('should return a null reference with a non-object value', () => {
expect(Report.fromJson('foo')).to.be.null;
});

it('should return an instance with default values for an empty map', () => {
let report = Report.fromJson({});
expect(report).to.be.instanceof(Report);
expect(report.records).to.be.an('array').and.be.empty;
expect(report.testName).to.be.empty;
});

it('should return an initialized instance for a non-empty map', () => {
let report = Report.fromJson({
records: [{}],
testName: 'LcovTest'
});

expect(report).to.be.instanceof(Report);
expect(report.records).to.be.an('array').and.have.lengthOf(1);
expect(report.records[0]).to.be.instanceof(Record);
expect(report.testName).to.equal('LcovTest');
});
});

/**
* @test {Report.parse}
* @test {Report.fromCoverage}
*/
describe('.parse()', async () => {
describe('.fromCoverage()', async () => {
const loadReport = promisify(readFile);
let reportPath = 'test/fixtures/lcov.info';

it('should have a test name', async () => {
let report = Report.parse(await loadReport(reportPath, 'utf8'));
let report = Report.fromCoverage(await loadReport(reportPath, 'utf8'));
expect(report.testName).to.equal('Example');
});

it('should contain three records', async () => {
let report = Report.parse(await loadReport(reportPath, 'utf8'));
let report = Report.fromCoverage(await loadReport(reportPath, 'utf8'));
expect(report.records).to.have.lengthOf(3);
expect(report.records[0]).to.be.instanceof(Record);
expect(report.records[0].sourceFile).to.equal('/home/cedx/lcov.js/fixture.js');
Expand All @@ -60,7 +32,7 @@ describe('Report', () => {
});

it('should have detailed branch coverage', async () => {
let report = Report.parse(await loadReport(reportPath, 'utf8'));
let report = Report.fromCoverage(await loadReport(reportPath, 'utf8'));

let branches = report.records[1].branches;
expect(branches.found).to.equal(4);
Expand All @@ -72,7 +44,7 @@ describe('Report', () => {
});

it('should have detailed function coverage', async () => {
let report = Report.parse(await loadReport(reportPath, 'utf8'));
let report = Report.fromCoverage(await loadReport(reportPath, 'utf8'));

let functions = report.records[1].functions;
expect(functions.found).to.equal(1);
Expand All @@ -84,7 +56,7 @@ describe('Report', () => {
});

it('should have detailed line coverage', async () => {
let report = Report.parse(await loadReport(reportPath, 'utf8'));
let report = Report.fromCoverage(await loadReport(reportPath, 'utf8'));

let lines = report.records[1].lines;
expect(lines.found).to.equal(9);
Expand All @@ -96,11 +68,39 @@ describe('Report', () => {
});

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

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

/**
* @test {Report.fromJson}
*/
describe('.fromJson()', () => {
it('should return a null reference with a non-object value', () => {
expect(Report.fromJson('foo')).to.be.null;
});

it('should return an instance with default values for an empty map', () => {
let report = Report.fromJson({});
expect(report).to.be.instanceof(Report);
expect(report.records).to.be.an('array').and.be.empty;
expect(report.testName).to.be.empty;
});

it('should return an initialized instance for a non-empty map', () => {
let report = Report.fromJson({
records: [{}],
testName: 'LcovTest'
});

expect(report).to.be.instanceof(Report);
expect(report.records).to.be.an('array').and.have.lengthOf(1);
expect(report.records[0]).to.be.instanceof(Record);
expect(report.testName).to.equal('LcovTest');
});
});

Expand Down

0 comments on commit a883c04

Please sign in to comment.