Skip to content

Commit

Permalink
refactor: convert to class syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC committed Jan 19, 2024
1 parent 8668ddc commit 9f3b9c8
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 234 deletions.
77 changes: 49 additions & 28 deletions src/builder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
var _ = require('lodash');
var xmlBuilder = require('xmlbuilder');
var path = require('path');
Expand All @@ -6,38 +7,58 @@ var fs = require('fs');
var TestSuite = require('./test_suite');
var TestCase = require('./test_case');

function JUnitReportBuilder(factory) {
this._factory = factory;
this._testSuitesAndCases = [];
}
class JUnitReportBuilder {
/**
* @param {import('./factory')} factory
*/
constructor(factory) {
this._factory = factory;
this._testSuitesAndCases = [];
}

JUnitReportBuilder.prototype.writeTo = function (reportPath) {
makeDir.sync(path.dirname(reportPath));
fs.writeFileSync(reportPath, this.build(), 'utf8');
};
/**
* @param {string} reportPath
*/
writeTo(reportPath) {
makeDir.sync(path.dirname(reportPath));
fs.writeFileSync(reportPath, this.build(), 'utf8');
}

JUnitReportBuilder.prototype.build = function () {
var xmlTree = xmlBuilder.create('testsuites', { encoding: 'UTF-8', invalidCharReplacement: '' });
_.forEach(this._testSuitesAndCases, function (suiteOrCase) {
suiteOrCase.build(xmlTree);
});
return xmlTree.end({ pretty: true });
};
/**
* @returns {string} xml file content
*/
build() {
var xmlTree = xmlBuilder.create('testsuites', { encoding: 'UTF-8', invalidCharReplacement: '' });
_.forEach(this._testSuitesAndCases, function (suiteOrCase) {
suiteOrCase.build(xmlTree);
});
return xmlTree.end({ pretty: true });
}

JUnitReportBuilder.prototype.testSuite = function () {
var suite = this._factory.newTestSuite();
this._testSuitesAndCases.push(suite);
return suite;
};
/**
* @returns {import('./test_suite')}
*/
testSuite() {
var suite = this._factory.newTestSuite();
this._testSuitesAndCases.push(suite);
return suite;
}

JUnitReportBuilder.prototype.testCase = function () {
var testCase = this._factory.newTestCase();
this._testSuitesAndCases.push(testCase);
return testCase;
};
/**
* @returns {import('./test_case')}
*/
testCase() {
var testCase = this._factory.newTestCase();
this._testSuitesAndCases.push(testCase);
return testCase;
}

JUnitReportBuilder.prototype.newBuilder = function () {
return this._factory.newBuilder();
};
/**
* @returns {ReturnType<import('./factory')['newBuilder']>}
*/
newBuilder() {
return this._factory.newBuilder();
}
}

module.exports = JUnitReportBuilder;
31 changes: 21 additions & 10 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ var Builder = require('./builder');
var TestSuite = require('./test_suite');
var TestCase = require('./test_case');

function Factory() {}
class Factory {
constructor() {}

Factory.prototype.newBuilder = function () {
return new Builder(this);
};
/**
* @returns {Builder}
*/
newBuilder() {
return new Builder(this);
}

Factory.prototype.newTestSuite = function () {
return new TestSuite(this);
};
/**
* @returns {TestSuite}
*/
newTestSuite() {
return new TestSuite(this);
}

Factory.prototype.newTestCase = function () {
return new TestCase(this);
};
/**
* @returns {TestCase}
*/
newTestCase() {
return new TestCase(this);
}
}

module.exports = Factory;
Loading

0 comments on commit 9f3b9c8

Please sign in to comment.