Skip to content

Commit

Permalink
test: writing some unit tests for new classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeanjones committed Jul 6, 2023
1 parent 755367d commit 4b674b6
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"scripts": {
"lint": "eslint . --cache",
"test": "npm run lint && npm run test:js",
"test:js": "mocha -R spec test --bail",
"test:coverage": "nyc --reporter=html --reporter=lcov mocha -- -R spec test --bail",
"test:js": "mocha --bail --recursive",
"test:coverage": "nyc --reporter=html --reporter=lcov mocha -- --recursive",
"release": "semantic-release"
},
"bin": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
getActual,
getTempLcovFilePath,
cleanFileDirectory,
} = require('./helpers');
} = require('../helpers');

chai.should();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const fastGlob = require('fast-glob');
const chai = require('chai');
const { getActual, getExpected } = require('./helpers');
const { mergeCoverageReportFilesStream } = require('../index.js');
const { getActual, getExpected } = require('../helpers');
const { mergeCoverageReportFilesStream } = require('../../index.js');

chai.should();

Expand Down
47 changes: 47 additions & 0 deletions test/test-unit/BRDA-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-env mocha */

const chai = require('chai');
chai.should();

const BRDA = require('../../lib/BRDA');

describe('Unit | BRDA', function () {
it('should be constructable with the new operator', async function () {
const instance = new BRDA(10, 20, 30, 40);

instance.should.be.instanceOf(BRDA);
instance.should.have.property('lineNumber', 10);
instance.should.have.property('blockNumber', 20);
instance.should.have.property('branchNumber', 30);
instance.should.have.property('hits', 40);
});

it('should be able to parse a string into meaningful values', async function () {
BRDA.parseString('1,2,3,4').should.eql([1, 2, 3, 4]);
BRDA.parseString('5,6,7,-').should.eql([5, 6, 7, '-']);
});

it('should output a valid string representation of its content', async function () {
new BRDA(10, 20, 30, 40).toString().should.equal('BRDA:10,20,30,40\n');
});

it('should accurately increment its hit count', async function () {
let instance = new BRDA(1, 2, 3, '-');
instance.should.have.property('hits', '-');

instance.addHits('-');
instance.should.have.property('hits', '-');

instance.addHits(1);
instance.should.have.property('hits', 1);

instance = new BRDA(1, 2, 3, 4);
instance.should.have.property('hits', 4);

instance.addHits('-');
instance.should.have.property('hits', 4);

instance.addHits(1);
instance.should.have.property('hits', 5);
});
});
101 changes: 101 additions & 0 deletions test/test-unit/CoverageFile-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-env mocha */

const chai = require('chai');
chai.should();

const CoverageFile = require('../../lib/CoverageFile');

describe('Unit | CoverageFile', function () {
it('should be constructable with the new operator', async function () {
const instance = new CoverageFile('test-lcov.info');

instance.should.be.instanceOf(CoverageFile);
instance.should.have.property('filename', 'test-lcov.info');
instance.should.have.property('BRDARecords').eql([]);
instance.should.have.property('DARecords').eql([]);
});

it('should allow DA records to be added', async function () {
const instance = new CoverageFile('test-lcov.info');

instance.DARecords.should.have.length(0);

instance.addDA(10, 1);
instance.DARecords.should.have.length(1);
instance.DARecords[0].should.have.property('lineNumber', 10);
instance.DARecords[0].should.have.property('hits', 1);

instance.parseDA('20,2');
instance.DARecords.should.have.length(2);
instance.DARecords[1].should.have.property('lineNumber', 20);
instance.DARecords[1].should.have.property('hits', 2);
});

it('should add hits to an existing DA record when one exists, instead of creating a duplicate', async function () {
const instance = new CoverageFile('test-lcov.info');

instance.addDA(10, 1);
instance.DARecords.should.have.length(1);
instance.DARecords[0].should.have.property('lineNumber', 10);
instance.DARecords[0].should.have.property('hits', 1);

instance.addDA(10, 3);
instance.DARecords.should.have.length(1);
instance.DARecords[0].should.have.property('lineNumber', 10);
instance.DARecords[0].should.have.property('hits', 4);
});

it('should allow BRDA records to be added', async function () {
const instance = new CoverageFile('test-lcov.info');

instance.BRDARecords.should.have.length(0);

instance.addBRDA(10, 20, 30, 40);
instance.BRDARecords.should.have.length(1);
instance.BRDARecords[0].should.have.property('lineNumber', 10);
instance.BRDARecords[0].should.have.property('blockNumber', 20);
instance.BRDARecords[0].should.have.property('branchNumber', 30);
instance.BRDARecords[0].should.have.property('hits', 40);

instance.parseBRDA('1,2,3,4');
instance.BRDARecords.should.have.length(2);
instance.BRDARecords[1].should.have.property('lineNumber', 1);
instance.BRDARecords[1].should.have.property('blockNumber', 2);
instance.BRDARecords[1].should.have.property('branchNumber', 3);
instance.BRDARecords[1].should.have.property('hits', 4);
});

it('should add hits to an existing BRDA record when one exists, instead of creating a duplicate', async function () {
const instance = new CoverageFile('test-lcov.info');

instance.addBRDA(10, 20, 30, 1);
instance.BRDARecords.should.have.length(1);
instance.BRDARecords[0].should.have.property('lineNumber', 10);
instance.BRDARecords[0].should.have.property('blockNumber', 20);
instance.BRDARecords[0].should.have.property('branchNumber', 30);
instance.BRDARecords[0].should.have.property('hits', 1);

instance.addBRDA(10, 20, 30, 3);
instance.BRDARecords.should.have.length(1);
instance.BRDARecords[0].should.have.property('lineNumber', 10);
instance.BRDARecords[0].should.have.property('blockNumber', 20);
instance.BRDARecords[0].should.have.property('branchNumber', 30);
instance.BRDARecords[0].should.have.property('hits', 4);
});

it('should output a valid string representation of its content', async function () {
const instance = new CoverageFile('test-lcov.info');

instance.addBRDA(10, 20, 30, 40);
instance.addDA(50, 60);

instance
.toString()
.should.equal(
'SF:test-lcov.info\n' +
'DA:50,60\n' +
'BRDA:10,20,30,40\n' +
'end_of_record\n'
);
});
});
32 changes: 32 additions & 0 deletions test/test-unit/DA-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-env mocha */

const chai = require('chai');
chai.should();

const DA = require('../../lib/DA');

describe('Unit | DA', function () {
it('should be constructable with the new operator', async function () {
const instance = new DA(10, 20);

instance.should.be.instanceOf(DA);
instance.should.have.property('lineNumber', 10);
instance.should.have.property('hits', 20);
});

it('should be able to parse a string into meaningful values', async function () {
DA.parseString('1,2').should.eql([1, 2]);
});

it('should output a valid string representation of its content', async function () {
new DA(10, 20).toString().should.equal('DA:10,20\n');
});

it('should accurately increment its hit count', async function () {
const instance = new DA(1, 2);
instance.should.have.property('hits', 2);

instance.addHits(1);
instance.should.have.property('hits', 3);
});
});

0 comments on commit 4b674b6

Please sign in to comment.