Refactor to improve iterations

- 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.
This commit is contained in:
undergroundwires
2022-01-04 21:45:22 +01:00
parent bd23faa28f
commit 31f70913a2
35 changed files with 342 additions and 343 deletions

View File

@@ -121,24 +121,45 @@ describe('CategoryCollection', () => {
expect(construct).to.throw('must consist of at least one script');
});
describe('cannot construct without any recommended scripts', () => {
// arrange
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 CategoryCollectionBuilder()
.withActions(categories)
.construct();
// assert
expect(construct).to.throw(expectedError);
});
}
describe('single missing', () => {
// arrange
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 CategoryCollectionBuilder()
.withActions(categories)
.construct();
// assert
expect(construct).to.throw(expectedError);
});
}
});
it('multiple are missing', () => {
// arrange
const expectedError = 'none of the scripts are recommended as '
+ `"${RecommendationLevel[RecommendationLevel.Standard]}, "${RecommendationLevel[RecommendationLevel.Strict]}".`;
const categories = [
new CategoryStub(0)
.withScript(
new ScriptStub(`Script${0}`).withLevel(undefined),
),
];
// act
const construct = () => new CategoryCollectionBuilder()
.withActions(categories)
.construct();
// assert
expect(construct).to.throw(expectedError);
});
});
});
describe('totalScripts', () => {

View File

@@ -61,25 +61,24 @@ describe('ScriptCode', () => {
},
];
// act
const actions = [];
for (const testCase of testCases) {
actions.push(...[
{
act: () => new ScriptCodeBuilder()
.withExecute(testCase.code)
.build(),
testName: `execute: ${testCase.testName}`,
expectedMessage: testCase.expectedMessage,
},
{
act: () => new ScriptCodeBuilder()
.withRevert(testCase.code)
.build(),
testName: `revert: ${testCase.testName}`,
expectedMessage: `(revert): ${testCase.expectedMessage}`,
},
]);
}
const actions = testCases.flatMap((testCase) => ([
{
act: () => new ScriptCodeBuilder()
.withExecute(testCase.code)
.build(),
testName: `execute: ${testCase.testName}`,
expectedMessage: testCase.expectedMessage,
code: testCase.code,
},
{
act: () => new ScriptCodeBuilder()
.withRevert(testCase.code)
.build(),
testName: `revert: ${testCase.testName}`,
expectedMessage: `(revert): ${testCase.expectedMessage}`,
code: testCase.code,
},
]));
// assert
for (const action of actions) {
it(action.testName, () => {
@@ -115,27 +114,24 @@ describe('ScriptCode', () => {
},
];
// act
const actions = [];
for (const testCase of testCases) {
actions.push(...[
{
testName: `execute: ${testCase.testName}`,
act: () => new ScriptCodeBuilder()
.withSyntax(syntax)
.withExecute(testCase.code)
.build(),
expect: (sut: IScriptCode) => sut.execute === testCase.code,
},
{
testName: `revert: ${testCase.testName}`,
act: () => new ScriptCodeBuilder()
.withSyntax(syntax)
.withRevert(testCase.code)
.build(),
expect: (sut: IScriptCode) => sut.revert === testCase.code,
},
]);
}
const actions = testCases.flatMap((testCase) => ([
{
testName: `execute: ${testCase.testName}`,
act: () => new ScriptCodeBuilder()
.withSyntax(syntax)
.withExecute(testCase.code)
.build(),
expect: (sut: IScriptCode) => sut.execute === testCase.code,
},
{
testName: `revert: ${testCase.testName}`,
act: () => new ScriptCodeBuilder()
.withSyntax(syntax)
.withRevert(testCase.code)
.build(),
expect: (sut: IScriptCode) => sut.revert === testCase.code,
},
]));
// assert
for (const action of actions) {
it(action.testName, () => {

View File

@@ -39,7 +39,7 @@ describe('ScriptingDefinition', () => {
[ScriptingLanguage.batchfile, 'bat'],
[ScriptingLanguage.shellscript, 'sh'],
]);
Array.from(testCases.entries()).forEach((test) => {
for (const test of testCases.entries()) {
const language = test[0];
const expectedExtension = test[1];
it(`${ScriptingLanguage[language]} has ${expectedExtension}`, () => {
@@ -50,7 +50,7 @@ describe('ScriptingDefinition', () => {
// assert
expect(sut.fileExtension, expectedExtension);
});
});
}
});
});
describe('startCode', () => {