From 4b674b6462306fbd37db8ac4bedfff6d0b7a9a2f Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Thu, 6 Jul 2023 10:40:45 +0200 Subject: [PATCH] test: writing some unit tests for new classes --- package.json | 4 +- .../lcov-result-merger-cmd-spec.js | 2 +- ...merge-coverage-report-file-streams-spec.js | 4 +- test/test-unit/BRDA-spec.js | 47 ++++++++ test/test-unit/CoverageFile-spec.js | 101 ++++++++++++++++++ test/test-unit/DA-spec.js | 32 ++++++ 6 files changed, 185 insertions(+), 5 deletions(-) rename test/{ => test-e2e}/lcov-result-merger-cmd-spec.js (99%) rename test/{ => test-e2e}/merge-coverage-report-file-streams-spec.js (94%) create mode 100644 test/test-unit/BRDA-spec.js create mode 100644 test/test-unit/CoverageFile-spec.js create mode 100644 test/test-unit/DA-spec.js diff --git a/package.json b/package.json index 86a64c5..d57ba7b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/lcov-result-merger-cmd-spec.js b/test/test-e2e/lcov-result-merger-cmd-spec.js similarity index 99% rename from test/lcov-result-merger-cmd-spec.js rename to test/test-e2e/lcov-result-merger-cmd-spec.js index 151da9a..197274b 100644 --- a/test/lcov-result-merger-cmd-spec.js +++ b/test/test-e2e/lcov-result-merger-cmd-spec.js @@ -8,7 +8,7 @@ const { getActual, getTempLcovFilePath, cleanFileDirectory, -} = require('./helpers'); +} = require('../helpers'); chai.should(); diff --git a/test/merge-coverage-report-file-streams-spec.js b/test/test-e2e/merge-coverage-report-file-streams-spec.js similarity index 94% rename from test/merge-coverage-report-file-streams-spec.js rename to test/test-e2e/merge-coverage-report-file-streams-spec.js index cbefcc7..f215d21 100644 --- a/test/merge-coverage-report-file-streams-spec.js +++ b/test/test-e2e/merge-coverage-report-file-streams-spec.js @@ -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(); diff --git a/test/test-unit/BRDA-spec.js b/test/test-unit/BRDA-spec.js new file mode 100644 index 0000000..bec5785 --- /dev/null +++ b/test/test-unit/BRDA-spec.js @@ -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); + }); +}); diff --git a/test/test-unit/CoverageFile-spec.js b/test/test-unit/CoverageFile-spec.js new file mode 100644 index 0000000..182f91a --- /dev/null +++ b/test/test-unit/CoverageFile-spec.js @@ -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' + ); + }); +}); diff --git a/test/test-unit/DA-spec.js b/test/test-unit/DA-spec.js new file mode 100644 index 0000000..4f961ac --- /dev/null +++ b/test/test-unit/DA-spec.js @@ -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); + }); +});