refactor extra code, duplicates, complexity

- refactor array equality check and add tests
- remove OperatingSystem.Unknown causing extra logic, return undefined instead
- refactor enum validation to share same logic
- refactor scripting language factories to share same logic
- refactor too many args in runCodeAsync
- refactor ScriptCode constructor to reduce complexity
- fix writing useless write to member object since another property write always override it
This commit is contained in:
undergroundwires
2021-04-11 14:37:02 +01:00
parent 3e9c99f5f8
commit 00d8e551db
37 changed files with 512 additions and 233 deletions

View File

@@ -9,6 +9,7 @@ import { getEnumValues } from '@/application/Common/Enum';
import { CategoryCollection } from '@/domain/CategoryCollection';
import { ScriptStub } from '../stubs/ScriptStub';
import { CategoryStub } from '../stubs/CategoryStub';
import { EnumRangeTestRunner } from '../application/Common/EnumRangeTestRunner';
describe('CategoryCollection', () => {
describe('getScriptsByLevel', () => {
@@ -186,35 +187,15 @@ describe('CategoryCollection', () => {
// assert
expect(sut.os).to.deep.equal(expected);
});
it('cannot construct with unknown os', () => {
// arrange
const os = OperatingSystem.Unknown;
describe('throws when invalid', () => {
// act
const construct = () => new CategoryCollectionBuilder()
const act = (os: OperatingSystem) => new CategoryCollectionBuilder()
.withOs(os)
.construct();
// assert
expect(construct).to.throw('unknown os');
});
it('cannot construct with undefined os', () => {
// arrange
const os = undefined;
// act
const construct = () => new CategoryCollectionBuilder()
.withOs(os)
.construct();
// assert
expect(construct).to.throw('undefined os');
});
it('cannot construct with OS not in range', () => {
// arrange
const os: OperatingSystem = 666;
// act
const construct = () => new CategoryCollectionBuilder()
.withOs(os)
.construct();
// assert
expect(construct).to.throw(`os "${os}" is out of range`);
new EnumRangeTestRunner(act)
.testOutOfRangeThrows()
.testUndefinedValueThrows();
});
});
describe('scriptingDefinition', () => {

View File

@@ -2,6 +2,7 @@ import 'mocha';
import { expect } from 'chai';
import { ProjectInformation } from '@/domain/ProjectInformation';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { EnumRangeTestRunner } from '../application/Common/EnumRangeTestRunner';
describe('ProjectInformation', () => {
it('sets name as expected', () => {
@@ -115,14 +116,16 @@ describe('ProjectInformation', () => {
// assert
expect(actual).to.equal(expected);
});
it('throws when OS is unknown', () => {
describe('throws when os is invalid', () => {
// arrange
const sut = new ProjectInformation('name', 'version', 'repositoryUrl', 'homepage');
const os = OperatingSystem.Unknown;
// act
const act = () => sut.getDownloadUrl(os);
const act = (os: OperatingSystem) => sut.getDownloadUrl(os);
// assert
expect(act).to.throw(`Unsupported os: ${OperatingSystem[os]}`);
new EnumRangeTestRunner(act)
.testOutOfRangeThrows()
.testUndefinedValueThrows()
.testInvalidValueThrows(OperatingSystem.KaiOS, `Unsupported os: ${OperatingSystem[OperatingSystem.KaiOS]}`);
});
});
});