- Use function abstractions (such as map, reduce, filter etc.) over for-of loops to gain benefits of having less side effects and easier readability. - Enable `downLevelIterations` for writing modern code with lazy evaluation. - Refactor for of loops to named abstractions to clearly express their intentions without needing to analyse the loop itself. - Add missing cases for changes that had no tests.
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import 'mocha';
|
|
import { expect } from 'chai';
|
|
import { IScriptingLanguageFactory } from '@/application/Common/ScriptingLanguage/IScriptingLanguageFactory';
|
|
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
|
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
|
|
|
|
export class ScriptingLanguageFactoryTestRunner<T> {
|
|
private expectedTypes = new Map<ScriptingLanguage, T>();
|
|
|
|
public expect(language: ScriptingLanguage, resultType: T) {
|
|
this.expectedTypes.set(language, resultType);
|
|
return this;
|
|
}
|
|
|
|
public testCreateMethod(sut: IScriptingLanguageFactory<T>) {
|
|
if (!sut) { throw new Error('undefined sut'); }
|
|
testLanguageValidation(sut);
|
|
testExpectedInstanceTypes(sut, this.expectedTypes);
|
|
}
|
|
}
|
|
|
|
function testExpectedInstanceTypes<T>(
|
|
sut: IScriptingLanguageFactory<T>,
|
|
expectedTypes: Map<ScriptingLanguage, T>,
|
|
) {
|
|
describe('create returns expected instances', () => {
|
|
// arrange
|
|
for (const language of expectedTypes.keys()) {
|
|
it(ScriptingLanguage[language], () => {
|
|
// act
|
|
const expected = expectedTypes.get(language);
|
|
const result = sut.create(language);
|
|
// assert
|
|
expect(result).to.be.instanceOf(expected, `Actual was: ${result.constructor.name}`);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
function testLanguageValidation<T>(sut: IScriptingLanguageFactory<T>) {
|
|
describe('validates language', () => {
|
|
// arrange
|
|
const validValue = ScriptingLanguage.batchfile;
|
|
// act
|
|
const act = (value: ScriptingLanguage) => sut.create(value);
|
|
// assert
|
|
new EnumRangeTestRunner(act)
|
|
.testOutOfRangeThrows()
|
|
.testUndefinedValueThrows()
|
|
.testValidValueDoesNotThrow(validValue);
|
|
});
|
|
}
|