add support for different recommendation levels: strict and standard

This commit is contained in:
undergroundwires
2020-10-19 14:51:42 +01:00
parent 978bab0b81
commit 14be3017c5
20 changed files with 954 additions and 654 deletions

View File

@@ -5,25 +5,79 @@ import 'mocha';
import { expect } from 'chai';
import { ProjectInformation } from '@/domain/ProjectInformation';
import { IProjectInformation } from '@/domain/IProjectInformation';
import { RecommendationLevel, RecommendationLevels } from '@/domain/RecommendationLevel';
describe('Application', () => {
it('getRecommendedScripts returns as expected', () => {
// arrange
const expected = [
new ScriptStub('S3').withIsRecommended(true),
new ScriptStub('S4').withIsRecommended(true),
];
const sut = new Application(createInformation(), [
new CategoryStub(3).withScripts(expected[0], new ScriptStub('S1').withIsRecommended(false)),
new CategoryStub(2).withScripts(expected[1], new ScriptStub('S2').withIsRecommended(false)),
]);
// act
const actual = sut.getRecommendedScripts();
// assert
expect(expected[0]).to.deep.equal(actual[0]);
expect(expected[1]).to.deep.equal(actual[1]);
describe('getScriptsByLevel', () => {
it('filters out scripts without levels', () => {
// arrange
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) {
const category = new CategoryStub(0)
.withScripts(...scriptsWithLevels)
.withScript(toIgnore);
const sut = new Application(createInformation(), [category]);
// act
const actual = sut.getScriptsByLevel(currentLevel);
// assert
expect(actual).to.not.include(toIgnore);
}
});
it(`${RecommendationLevel[RecommendationLevel.Standard]} filters ${RecommendationLevel[RecommendationLevel.Strict]}`, () => {
// arrange
const level = RecommendationLevel.Standard;
const expected = [
new ScriptStub('S1').withLevel(level),
new ScriptStub('S2').withLevel(level),
];
const sut = new Application(createInformation(), [
new CategoryStub(3).withScripts(...expected,
new ScriptStub('S3').withLevel(RecommendationLevel.Strict)),
]);
// act
const actual = sut.getScriptsByLevel(level);
// assert
expect(expected).to.deep.equal(actual);
});
it(`${RecommendationLevel[RecommendationLevel.Strict]} includes ${RecommendationLevel[RecommendationLevel.Standard]}`, () => {
// arrange
const level = RecommendationLevel.Strict;
const expected = [
new ScriptStub('S1').withLevel(RecommendationLevel.Standard),
new ScriptStub('S2').withLevel(RecommendationLevel.Strict),
];
const sut = new Application(createInformation(), [
new CategoryStub(3).withScripts(...expected),
]);
// act
const actual = sut.getScriptsByLevel(level);
// assert
expect(expected).to.deep.equal(actual);
});
it('throws when level is undefined', () => {
// arrange
const sut = new Application(createInformation(), [ getCategoryForValidApplication() ]);
// act
const act = () => sut.getScriptsByLevel(undefined);
// assert
expect(act).to.throw('undefined level');
});
it('throws when level is out of range', () => {
// arrange
const invalidValue = 66;
const sut = new Application(createInformation(), [
getCategoryForValidApplication(),
]);
// act
const act = () => sut.getScriptsByLevel(invalidValue);
// assert
expect(act).to.throw(`invalid level: ${invalidValue}`);
});
});
describe('parameter validation', () => {
describe('ctor', () => {
it('cannot construct without categories', () => {
// arrange
const categories = [];
@@ -43,20 +97,24 @@ describe('Application', () => {
// assert
expect(construct).to.throw('Application must consist of at least one script');
});
it('cannot construct without any recommended scripts', () => {
// arrange
const categories = [
new CategoryStub(3).withScripts(new ScriptStub('S1').withIsRecommended(false)),
new CategoryStub(2).withScripts(new ScriptStub('S2').withIsRecommended(false)),
];
// act
function construct() { return new Application(createInformation(), categories); }
// assert
expect(construct).to.throw('Application must consist of at least one recommended 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').withIsRecommended(true))];
const categories = [ new CategoryStub(1).withScripts(
new ScriptStub('S1').withLevel(RecommendationLevel.Standard))];
const information = undefined;
// act
function construct() { return new Application(information, categories); }
@@ -64,42 +122,57 @@ describe('Application', () => {
expect(construct).to.throw('info is undefined');
});
});
it('totalScripts counts right', () => {
// arrange
const categories = [
new CategoryStub(1).withScripts(new ScriptStub('S1').withIsRecommended(true)),
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);
// assert
expect(sut.totalScripts).to.equal(4);
describe('totalScripts', () => {
it('returns total of initial scripts', () => {
// arrange
const categories = [
new CategoryStub(1).withScripts(
new ScriptStub('S1').withLevel(RecommendationLevel.Standard)),
new CategoryStub(2).withScripts(
new ScriptStub('S2'),
new ScriptStub('S3').withLevel(RecommendationLevel.Strict)),
new CategoryStub(3).withCategories(
new CategoryStub(4).withScripts(new ScriptStub('S4'))),
];
// act
const sut = new Application(createInformation(), categories);
// assert
expect(sut.totalScripts).to.equal(4);
});
});
it('totalCategories counts right', () => {
// arrange
const categories = [
new CategoryStub(1).withScripts(new ScriptStub('S1').withIsRecommended(true)),
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);
// assert
expect(sut.totalCategories).to.equal(4);
describe('totalCategories', () => {
it('returns total of initial categories', () => {
// arrange
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);
// assert
expect(sut.totalCategories).to.equal(4);
});
});
it('sets information as expected', () => {
// arrange
const expected = createInformation();
// act
const sut = new Application(
expected,
[new CategoryStub(1).withScripts(new ScriptStub('S1').withIsRecommended(true))]);
// assert
expect(sut.info).to.deep.equal(expected);
describe('info', () => {
it('returns initial information', () => {
// arrange
const expected = createInformation();
// act
const sut = new Application(
expected, [ getCategoryForValidApplication() ]);
// assert
expect(sut.info).to.deep.equal(expected);
});
});
});
function getCategoryForValidApplication() {
return new CategoryStub(1).withScripts(
new ScriptStub('S1').withLevel(RecommendationLevel.Standard),
new ScriptStub('S2').withLevel(RecommendationLevel.Strict));
}
function createInformation(): IProjectInformation {
return new ProjectInformation('name', 'repo', '0.1.0', 'homepage');
}

View File

@@ -0,0 +1,17 @@
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,6 +1,7 @@
import 'mocha';
import { expect } from 'chai';
import { Script } from '@/domain/Script';
import { RecommendationLevelNames, RecommendationLevel } from '@/domain/RecommendationLevel';
describe('Script', () => {
describe('ctor', () => {
@@ -13,6 +14,11 @@ describe('Script', () => {
const code = 'duplicate\n\n\ntest\nduplicate';
expect(() => createWithCode(code)).to.throw();
});
it('sets as expected', () => {
const expected = 'expected-revert';
const sut = createWithCode(expected);
expect(sut.code).to.equal(expected);
});
});
describe('revertCode', () => {
it('cannot construct with duplicate lines', () => {
@@ -27,6 +33,11 @@ describe('Script', () => {
const code = 'REM';
expect(() => createWithCode(code, code)).to.throw();
});
it('sets as expected', () => {
const expected = 'expected-revert';
const sut = createWithCode('abc', expected);
expect(sut.revertCode).to.equal(expected);
});
});
describe('canRevert', () => {
it('returns false without revert code', () => {
@@ -38,9 +49,28 @@ describe('Script', () => {
expect(sut.canRevert()).to.equal(true);
});
});
describe('level', () => {
it('cannot construct with invalid wrong value', () => {
expect(() => createWithLevel(55)).to.throw('invalid level');
});
it('sets undefined as expected', () => {
const sut = createWithLevel(undefined);
expect(sut.level).to.equal(undefined);
});
it('sets as expected', () => {
for (const expected of RecommendationLevelNames) {
const sut = createWithLevel(RecommendationLevel[expected]);
const actual = RecommendationLevel[sut.level];
expect(actual).to.equal(expected);
}
});
});
});
});
function createWithCode(code: string, revertCode?: string): Script {
return new Script('name', code, revertCode, [], false);
return new Script('name', code, revertCode, [], RecommendationLevel.Standard);
}
function createWithLevel(level: RecommendationLevel): Script {
return new Script('name', 'code', 'revertCode', [], level);
}