refactor application.yaml to become an os definition #40

This commit is contained in:
undergroundwires
2020-09-08 21:47:18 +01:00
parent e4b6cdfb18
commit f7557bcc0f
62 changed files with 1926 additions and 573 deletions

View File

@@ -5,21 +5,27 @@ import 'mocha';
import { expect } from 'chai';
import { ProjectInformation } from '@/domain/ProjectInformation';
import { IProjectInformation } from '@/domain/IProjectInformation';
import { RecommendationLevel, RecommendationLevels } from '@/domain/RecommendationLevel';
import { ICategory } from '@/domain/IApplication';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { IScriptingDefinition } from '@/domain/IScriptingDefinition';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { getEnumValues } from '@/application/Common/Enum';
describe('Application', () => {
describe('getScriptsByLevel', () => {
it('filters out scripts without levels', () => {
// arrange
const scriptsWithLevels = RecommendationLevels.map((level, index) =>
const recommendationLevels = getEnumValues(RecommendationLevel);
const scriptsWithLevels = recommendationLevels.map((level, index) =>
new ScriptStub(`Script${index}`).withLevel(level),
);
const toIgnore = new ScriptStub('script-to-ignore').withLevel(undefined);
for (const currentLevel of RecommendationLevels) {
for (const currentLevel of recommendationLevels) {
const category = new CategoryStub(0)
.withScripts(...scriptsWithLevels)
.withScript(toIgnore);
const sut = new Application(createInformation(), [category]);
const sut = new ApplicationBuilder().withActions([category]).construct();
// act
const actual = sut.getScriptsByLevel(currentLevel);
// assert
@@ -33,10 +39,11 @@ describe('Application', () => {
new ScriptStub('S1').withLevel(level),
new ScriptStub('S2').withLevel(level),
];
const sut = new Application(createInformation(), [
const actions = [
new CategoryStub(3).withScripts(...expected,
new ScriptStub('S3').withLevel(RecommendationLevel.Strict)),
]);
];
const sut = new ApplicationBuilder().withActions(actions).construct();
// act
const actual = sut.getScriptsByLevel(level);
// assert
@@ -49,9 +56,10 @@ describe('Application', () => {
new ScriptStub('S1').withLevel(RecommendationLevel.Standard),
new ScriptStub('S2').withLevel(RecommendationLevel.Strict),
];
const sut = new Application(createInformation(), [
const actions = [
new CategoryStub(3).withScripts(...expected),
]);
];
const sut = new ApplicationBuilder().withActions(actions).construct();
// act
const actual = sut.getScriptsByLevel(level);
// assert
@@ -59,7 +67,7 @@ describe('Application', () => {
});
it('throws when level is undefined', () => {
// arrange
const sut = new Application(createInformation(), [ getCategoryForValidApplication() ]);
const sut = new ApplicationBuilder().construct();
// act
const act = () => sut.getScriptsByLevel(undefined);
// assert
@@ -68,21 +76,21 @@ describe('Application', () => {
it('throws when level is out of range', () => {
// arrange
const invalidValue = 66;
const sut = new Application(createInformation(), [
getCategoryForValidApplication(),
]);
const sut = new ApplicationBuilder().construct();
// act
const act = () => sut.getScriptsByLevel(invalidValue);
// assert
expect(act).to.throw(`invalid level: ${invalidValue}`);
});
});
describe('ctor', () => {
it('cannot construct without categories', () => {
describe('actions', () => {
it('cannot construct without actions', () => {
// arrange
const categories = [];
// act
function construct() { return new Application(createInformation(), categories); }
function construct() {
new ApplicationBuilder().withActions(categories).construct();
}
// assert
expect(construct).to.throw('Application must consist of at least one category');
});
@@ -93,33 +101,31 @@ describe('Application', () => {
new CategoryStub(2),
];
// act
function construct() { return new Application(createInformation(), categories); }
function construct() {
new ApplicationBuilder().withActions(categories).construct();
}
// assert
expect(construct).to.throw('Application must consist of at least one script');
});
describe('cannot construct without any recommended scripts', () => {
for (const missingLevel of RecommendationLevels) {
// arrange
const expectedError = `none of the scripts are recommended as ${RecommendationLevel[missingLevel]}`;
const otherLevels = RecommendationLevels.filter((level) => level !== missingLevel);
const categories = otherLevels.map((level, index) =>
new CategoryStub(index).withScript(new ScriptStub(`Script${index}`).withLevel(level)),
);
// act
const construct = () => new Application(createInformation(), categories);
// assert
expect(construct).to.throw(expectedError);
}
});
it('cannot construct without information', () => {
// arrange
const categories = [ new CategoryStub(1).withScripts(
new ScriptStub('S1').withLevel(RecommendationLevel.Standard))];
const information = undefined;
// act
function construct() { return new Application(information, categories); }
// assert
expect(construct).to.throw('info is undefined');
const recommendationLevels = getEnumValues(RecommendationLevel);
for (const missingLevel of recommendationLevels) {
it(`when "${RecommendationLevel[missingLevel]}" is missing`, () => {
const expectedError = `none of the scripts are recommended as ${RecommendationLevel[missingLevel]}`;
const otherLevels = recommendationLevels.filter((level) => level !== missingLevel);
const categories = otherLevels.map((level, index) =>
new CategoryStub(index).withScript(
new ScriptStub(`Script${index}`).withLevel(level),
));
// act
const construct = () => new ApplicationBuilder()
.withActions(categories)
.construct();
// assert
expect(construct).to.throw(expectedError);
});
}
});
});
describe('totalScripts', () => {
@@ -135,7 +141,7 @@ describe('Application', () => {
new CategoryStub(4).withScripts(new ScriptStub('S4'))),
];
// act
const sut = new Application(createInformation(), categories);
const sut = new ApplicationBuilder().withActions(categories).construct();
// assert
expect(sut.totalScripts).to.equal(4);
});
@@ -143,36 +149,132 @@ describe('Application', () => {
describe('totalCategories', () => {
it('returns total of initial categories', () => {
// arrange
const expected = 4;
const categories = [
new CategoryStub(1).withScripts(new ScriptStub('S1').withLevel(RecommendationLevel.Strict)),
new CategoryStub(2).withScripts(new ScriptStub('S2'), new ScriptStub('S3')),
new CategoryStub(3).withCategories(new CategoryStub(4).withScripts(new ScriptStub('S4'))),
];
// act
const sut = new Application(createInformation(), categories);
const sut = new ApplicationBuilder()
.withActions(categories)
.construct();
// assert
expect(sut.totalCategories).to.equal(4);
expect(sut.totalCategories).to.equal(expected);
});
});
describe('info', () => {
it('returns initial information', () => {
describe('information', () => {
it('sets information as expected', () => {
// arrange
const expected = createInformation();
const expected = new ProjectInformation(
'expected-name', 'expected-repo', '0.31.0', 'expected-homepage');
// act
const sut = new Application(
expected, [ getCategoryForValidApplication() ]);
const sut = new ApplicationBuilder().withInfo(expected).construct();
// assert
expect(sut.info).to.deep.equal(expected);
});
it('cannot construct without information', () => {
// arrange
const information = undefined;
// act
function construct() {
return new ApplicationBuilder().withInfo(information).construct();
}
// assert
expect(construct).to.throw('undefined info');
});
});
describe('os', () => {
it('sets os as expected', () => {
// arrange
const expected = OperatingSystem.macOS;
// act
const sut = new ApplicationBuilder().withOs(expected).construct();
// assert
expect(sut.os).to.deep.equal(expected);
});
it('cannot construct with unknown os', () => {
// arrange
const os = OperatingSystem.Unknown;
// act
const construct = () => new ApplicationBuilder().withOs(os).construct();
// assert
expect(construct).to.throw('unknown os');
});
it('cannot construct with undefined os', () => {
// arrange
const os = undefined;
// act
const construct = () => new ApplicationBuilder().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 ApplicationBuilder().withOs(os).construct();
// assert
expect(construct).to.throw(`os "${os}" is out of range`);
});
});
describe('scriptingDefinition', () => {
it('sets scriptingDefinition as expected', () => {
// arrange
const expected = getValidScriptingDefinition();
// act
const sut = new ApplicationBuilder().withScripting(expected).construct();
// assert
expect(sut.scripting).to.deep.equal(expected);
});
it('cannot construct without initial script', () => {
// arrange
const scriptingDefinition = undefined;
// act
function construct() {
return new ApplicationBuilder().withScripting(scriptingDefinition).construct();
}
// assert
expect(construct).to.throw('undefined scripting definition');
});
});
});
function getCategoryForValidApplication() {
return new CategoryStub(1).withScripts(
new ScriptStub('S1').withLevel(RecommendationLevel.Standard),
new ScriptStub('S2').withLevel(RecommendationLevel.Strict));
function getValidScriptingDefinition(): IScriptingDefinition {
return {
fileExtension: '.bat',
language: ScriptingLanguage.batchfile,
startCode: 'start',
endCode: 'end',
};
}
function createInformation(): IProjectInformation {
return new ProjectInformation('name', 'repo', '0.1.0', 'homepage');
class ApplicationBuilder {
private os = OperatingSystem.Windows;
private info = new ProjectInformation('name', 'repo', '0.1.0', 'homepage');
private actions: readonly ICategory[] = [
new CategoryStub(1).withScripts(
new ScriptStub('S1').withLevel(RecommendationLevel.Standard),
new ScriptStub('S2').withLevel(RecommendationLevel.Strict)),
];
private script: IScriptingDefinition = getValidScriptingDefinition();
public withOs(os: OperatingSystem): ApplicationBuilder {
this.os = os;
return this;
}
public withInfo(info: IProjectInformation) {
this.info = info;
return this;
}
public withActions(actions: readonly ICategory[]) {
this.actions = actions;
return this;
}
public withScripting(script: IScriptingDefinition) {
this.script = script;
return this;
}
public construct(): Application {
return new Application(this.os, this.info, this.actions, this.script);
}
}

View File

@@ -1,17 +0,0 @@
import 'mocha';
import { expect } from 'chai';
import { RecommendationLevelNames, RecommendationLevel } from '@/domain/RecommendationLevel';
describe('RecommendationLevel', () => {
describe('RecommendationLevelNames', () => {
// arrange
const expected = [
RecommendationLevel[RecommendationLevel.Strict],
RecommendationLevel[RecommendationLevel.Standard],
];
// act
const actual = RecommendationLevelNames;
// assert
expect(actual).to.have.deep.members(expected);
});
});

View File

@@ -1,7 +1,8 @@
import { getEnumValues } from '@/application/Common/Enum';
import 'mocha';
import { expect } from 'chai';
import { Script } from '@/domain/Script';
import { RecommendationLevel, RecommendationLevels } from '@/domain/RecommendationLevel';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { ScriptCode } from '@/domain/ScriptCode';
import { IScriptCode } from '@/domain/IScriptCode';
@@ -80,7 +81,7 @@ describe('Script', () => {
});
it('sets as expected', () => {
// arrange
for (const expected of RecommendationLevels) {
for (const expected of getEnumValues(RecommendationLevel)) {
// act
const sut = new ScriptBuilder()
.withRecommendationLevel(expected)

View File

@@ -0,0 +1,134 @@
import 'mocha';
import { expect } from 'chai';
import { ScriptingDefinition } from '@/domain/ScriptingDefinition';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { getEnumValues } from '@/application/Common/Enum';
import { OperatingSystem } from '@/domain/OperatingSystem';
describe('ScriptingDefinition', () => {
describe('language', () => {
describe('sets as expected', () => {
// arrange
const expectedValues = getEnumValues(ScriptingLanguage);
expectedValues.forEach((expected) => {
it(ScriptingLanguage[expected], () => {
// act
const sut = new ScriptingDefinitionBuilder()
.withLanguage(expected)
.build();
// assert
expect(sut.language).to.equal(expected);
});
});
});
it('throws if unknown', () => {
// arrange
const unknownValue: ScriptingLanguage = 666;
const errorMessage = `unsupported language: ${unknownValue}`;
// act
const act = () => new ScriptingDefinitionBuilder()
.withLanguage(unknownValue)
.build();
// assert
expect(act).to.throw(errorMessage);
});
});
describe('fileExtension', () => {
describe('returns expected for each language', () => {
// arrange
const testCases = new Map<ScriptingLanguage, string>([
[ScriptingLanguage.batchfile, 'bat'],
[ScriptingLanguage.bash, 'sh'],
]);
Array.from(testCases.entries()).forEach((test) => {
const language = test[0];
const expectedExtension = test[1];
it(`${ScriptingLanguage[language]} has ${expectedExtension}`, () => {
// act
const sut = new ScriptingDefinitionBuilder()
.withLanguage(language)
.build();
// assert
expect(sut.fileExtension, expectedExtension);
});
});
});
});
describe('startCode', () => {
it('sets as expected', () => {
// arrange
const expected = 'REM start-code';
// act
const sut = new ScriptingDefinitionBuilder()
.withStartCode(expected)
.build();
// assert
expect(sut.startCode).to.equal(expected);
});
it('throws when undefined', () => {
// arrange
const expectedError = 'undefined start code';
const undefinedValues = [ '', undefined ];
for (const undefinedValue of undefinedValues) {
// act
const act = () => new ScriptingDefinitionBuilder()
.withStartCode(undefinedValue)
.build();
// assert
expect(act).to.throw(expectedError);
}
});
});
describe('endCode', () => {
it('sets as expected', () => {
// arrange
const expected = 'REM end-code';
// act
const sut = new ScriptingDefinitionBuilder()
.withEndCode(expected)
.build();
// assert
expect(sut.endCode).to.equal(expected);
});
it('throws when undefined', () => {
// arrange
const expectedError = 'undefined end code';
const undefinedValues = [ '', undefined ];
for (const undefinedValue of undefinedValues) {
// act
const act = () => new ScriptingDefinitionBuilder()
.withEndCode(undefinedValue)
.build();
// assert
expect(act).to.throw(expectedError);
}
});
});
});
class ScriptingDefinitionBuilder {
private language = ScriptingLanguage.bash;
private startCode = 'REM start-code';
private endCode = 'REM end-code';
public withLanguage(language: ScriptingLanguage): ScriptingDefinitionBuilder {
this.language = language;
return this;
}
public withStartCode(startCode: string): ScriptingDefinitionBuilder {
this.startCode = startCode;
return this;
}
public withEndCode(endCode: string): ScriptingDefinitionBuilder {
this.endCode = endCode;
return this;
}
public build(): ScriptingDefinition {
return new ScriptingDefinition(
this.language, this.startCode, this.endCode);
}
}