Refactor to enforce strictNullChecks
This commit applies `strictNullChecks` to the entire codebase to improve maintainability and type safety. Key changes include: - Remove some explicit null-checks where unnecessary. - Add necessary null-checks. - Refactor static factory functions for a more functional approach. - Improve some test names and contexts for better debugging. - Add unit tests for any additional logic introduced. - Refactor `createPositionFromRegexFullMatch` to its own function as the logic is reused. - Prefer `find` prefix on functions that may return `undefined` and `get` prefix for those that always return a value.
This commit is contained in:
@@ -1,36 +1,9 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { scrambledEqual, sequenceEqual } from '@/application/Common/Array';
|
||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { ComparerTestScenario } from './Array.ComparerTestScenario';
|
||||
|
||||
describe('Array', () => {
|
||||
describe('scrambledEqual', () => {
|
||||
describe('throws if arguments are absent', () => {
|
||||
describe('first argument is absent', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing first array';
|
||||
const firstArray = absentValue;
|
||||
const secondArray = [];
|
||||
// act
|
||||
const act = () => scrambledEqual(firstArray, secondArray);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('second argument is absent', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing second array';
|
||||
const firstArray = [];
|
||||
const secondArray = absentValue;
|
||||
// act
|
||||
const act = () => scrambledEqual(firstArray, secondArray);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('returns as expected', () => {
|
||||
// arrange
|
||||
const scenario = new ComparerTestScenario()
|
||||
@@ -49,43 +22,17 @@ describe('Array', () => {
|
||||
});
|
||||
});
|
||||
describe('sequenceEqual', () => {
|
||||
describe('throws if arguments are absent', () => {
|
||||
describe('first argument is absent', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing first array';
|
||||
const firstArray = absentValue;
|
||||
const secondArray = [];
|
||||
// act
|
||||
const act = () => sequenceEqual(firstArray, secondArray);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('second argument is absent', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing second array';
|
||||
const firstArray = [];
|
||||
const secondArray = absentValue;
|
||||
// act
|
||||
const act = () => sequenceEqual(firstArray, secondArray);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('returns as expected', () => {
|
||||
// arrange
|
||||
const scenario = new ComparerTestScenario()
|
||||
.addSameItemsWithSameOrder(true)
|
||||
.addSameItemsWithDifferentOrder(true)
|
||||
.addSameItemsWithDifferentOrder(false)
|
||||
.addDifferentItemsWithSameLength(false)
|
||||
.addDifferentItemsWithDifferentLength(false);
|
||||
// act
|
||||
scenario.forEachCase((testCase) => {
|
||||
it(testCase.name, () => {
|
||||
const actual = scrambledEqual(testCase.first, testCase.second);
|
||||
const actual = sequenceEqual(testCase.first, testCase.second);
|
||||
// assert
|
||||
expect(actual).to.equal(testCase.expected);
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import {
|
||||
describe, it, afterEach, expect,
|
||||
} from 'vitest';
|
||||
import { CustomError, Environment } from '@/application/Common/CustomError';
|
||||
import { CustomError, PlatformErrorPrototypeManipulation } from '@/application/Common/CustomError';
|
||||
|
||||
describe('CustomError', () => {
|
||||
afterEach(() => {
|
||||
Environment.getSetPrototypeOf = () => Object.setPrototypeOf;
|
||||
Environment.getCaptureStackTrace = () => Error.captureStackTrace;
|
||||
PlatformErrorPrototypeManipulation.getSetPrototypeOf = () => Object.setPrototypeOf;
|
||||
PlatformErrorPrototypeManipulation.getCaptureStackTrace = () => Error.captureStackTrace;
|
||||
});
|
||||
describe('sets members as expected', () => {
|
||||
it('`name`', () => {
|
||||
@@ -39,7 +39,7 @@ describe('CustomError', () => {
|
||||
it('sets using `getCaptureStackTrace` if available', () => {
|
||||
// arrange
|
||||
const mockStackTrace = 'mocked stack trace';
|
||||
Environment.getCaptureStackTrace = () => (error) => {
|
||||
PlatformErrorPrototypeManipulation.getCaptureStackTrace = () => (error) => {
|
||||
(error as Error).stack = mockStackTrace;
|
||||
};
|
||||
// act
|
||||
@@ -102,7 +102,7 @@ describe('CustomError', () => {
|
||||
describe('Object.setPrototypeOf', () => {
|
||||
it('does not throw if unavailable', () => {
|
||||
// arrange
|
||||
Environment.getSetPrototypeOf = () => undefined;
|
||||
PlatformErrorPrototypeManipulation.getSetPrototypeOf = () => undefined;
|
||||
|
||||
// act
|
||||
const act = () => new CustomErrorConcrete();
|
||||
@@ -114,7 +114,7 @@ describe('CustomError', () => {
|
||||
// arrange
|
||||
let wasCalled = false;
|
||||
const setPrototypeOf = () => { wasCalled = true; };
|
||||
Environment.getSetPrototypeOf = () => setPrototypeOf;
|
||||
PlatformErrorPrototypeManipulation.getSetPrototypeOf = () => setPrototypeOf;
|
||||
|
||||
// act
|
||||
// eslint-disable-next-line no-new
|
||||
@@ -127,7 +127,7 @@ describe('CustomError', () => {
|
||||
describe('Error.captureStackTrace', () => {
|
||||
it('does not throw if unavailable', () => {
|
||||
// arrange
|
||||
Environment.getCaptureStackTrace = () => undefined;
|
||||
PlatformErrorPrototypeManipulation.getCaptureStackTrace = () => undefined;
|
||||
|
||||
// act
|
||||
const act = () => new CustomErrorConcrete();
|
||||
@@ -139,7 +139,7 @@ describe('CustomError', () => {
|
||||
// arrange
|
||||
let wasCalled = false;
|
||||
const captureStackTrace = () => { wasCalled = true; };
|
||||
Environment.getCaptureStackTrace = () => captureStackTrace;
|
||||
PlatformErrorPrototypeManipulation.getCaptureStackTrace = () => captureStackTrace;
|
||||
|
||||
// act
|
||||
// eslint-disable-next-line no-new
|
||||
|
||||
@@ -36,8 +36,15 @@ describe('Enum', () => {
|
||||
describe('throws as expected', () => {
|
||||
// arrange
|
||||
const enumName = 'ParsableEnum';
|
||||
const testCases = [
|
||||
...getAbsentStringTestCases().map((test) => ({
|
||||
const testScenarios: ReadonlyArray<{
|
||||
readonly name: string;
|
||||
readonly value: string;
|
||||
readonly expectedError: string;
|
||||
}> = [
|
||||
...getAbsentStringTestCases({
|
||||
excludeNull: true,
|
||||
excludeUndefined: true,
|
||||
}).map((test) => ({
|
||||
name: test.valueName,
|
||||
value: test.absentValue,
|
||||
expectedError: `missing ${enumName}`,
|
||||
@@ -59,7 +66,7 @@ describe('Enum', () => {
|
||||
},
|
||||
];
|
||||
// act
|
||||
for (const testCase of testCases) {
|
||||
for (const testCase of testScenarios) {
|
||||
it(testCase.name, () => {
|
||||
const parser = createEnumParser(ParsableEnum);
|
||||
const act = () => parser.parseEnum(testCase.value, enumName);
|
||||
@@ -100,7 +107,6 @@ describe('Enum', () => {
|
||||
// assert
|
||||
new EnumRangeTestRunner(act)
|
||||
.testOutOfRangeThrows()
|
||||
.testAbsentValueThrows()
|
||||
.testValidValueDoesNotThrow(validValue);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { it, expect } from 'vitest';
|
||||
import { EnumType } from '@/application/Common/Enum';
|
||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
|
||||
export class EnumRangeTestRunner<TEnumValue extends EnumType> {
|
||||
constructor(private readonly runner: (value: TEnumValue) => void) {
|
||||
}
|
||||
|
||||
public testOutOfRangeThrows() {
|
||||
public testOutOfRangeThrows(errorMessageBuilder?: (outOfRangeValue: TEnumValue) => string) {
|
||||
it('throws when value is out of range', () => {
|
||||
// arrange
|
||||
const value = Number.MAX_SAFE_INTEGER as TEnumValue;
|
||||
const expectedError = `enum value "${value}" is out of range`;
|
||||
const expectedError = errorMessageBuilder
|
||||
? errorMessageBuilder(value)
|
||||
: `enum value "${value}" is out of range`;
|
||||
// act
|
||||
const act = () => this.runner(value);
|
||||
// assert
|
||||
@@ -19,23 +20,8 @@ export class EnumRangeTestRunner<TEnumValue extends EnumType> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public testAbsentValueThrows() {
|
||||
describe('throws when value is absent', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const value = absentValue;
|
||||
const expectedError = 'absent enum value';
|
||||
// act
|
||||
const act = () => this.runner(value);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public testInvalidValueThrows(invalidValue: TEnumValue, expectedError: string) {
|
||||
it(`throws ${expectedError}`, () => {
|
||||
it(`throws: \`${expectedError}\``, () => {
|
||||
// arrange
|
||||
const value = invalidValue;
|
||||
// act
|
||||
|
||||
@@ -2,7 +2,6 @@ import { describe, it, expect } from 'vitest';
|
||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
||||
import { ScriptingLanguageFactory } from '@/application/Common/ScriptingLanguage/ScriptingLanguageFactory';
|
||||
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
|
||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { ScriptingLanguageFactoryTestRunner } from './ScriptingLanguageFactoryTestRunner';
|
||||
|
||||
class ScriptingLanguageConcrete extends ScriptingLanguageFactory<number> {
|
||||
@@ -23,22 +22,8 @@ describe('ScriptingLanguageFactory', () => {
|
||||
// assert
|
||||
new EnumRangeTestRunner(act)
|
||||
.testOutOfRangeThrows()
|
||||
.testAbsentValueThrows()
|
||||
.testValidValueDoesNotThrow(validValue);
|
||||
});
|
||||
describe('describe when getter is absent', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing getter';
|
||||
const language = ScriptingLanguage.batchfile;
|
||||
const getter = absentValue;
|
||||
const sut = new ScriptingLanguageConcrete();
|
||||
// act
|
||||
const act = () => sut.registerGetter(language, getter);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
it('throw when language is already registered', () => {
|
||||
// arrange
|
||||
const language = ScriptingLanguage.batchfile;
|
||||
|
||||
@@ -21,7 +21,6 @@ export class ScriptingLanguageFactoryTestRunner<T> {
|
||||
}
|
||||
|
||||
public testCreateMethod(sut: IScriptingLanguageFactory<T>) {
|
||||
if (!sut) { throw new Error('missing sut'); }
|
||||
testLanguageValidation(sut);
|
||||
if (this.expectedLanguageTypes.size) {
|
||||
testExpectedInstanceTypes(sut, this.expectedLanguageTypes);
|
||||
@@ -36,7 +35,7 @@ function testExpectedInstanceTypes<T>(
|
||||
sut: IScriptingLanguageFactory<T>,
|
||||
expectedTypes: Map<ScriptingLanguage, Constructible<T>>,
|
||||
) {
|
||||
if (!expectedTypes?.size) {
|
||||
if (!expectedTypes.size) {
|
||||
throw new Error('No expected types provided.');
|
||||
}
|
||||
describe('`create` creates expected instances', () => {
|
||||
@@ -47,7 +46,7 @@ function testExpectedInstanceTypes<T>(
|
||||
const expected = expectedTypes.get(language);
|
||||
const result = sut.create(language);
|
||||
// assert
|
||||
expect(result).to.be.instanceOf(expected, `Actual was: ${result.constructor.name}`);
|
||||
expect(result).to.be.instanceOf(expected, `Actual was: ${result}`);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -57,7 +56,7 @@ function testExpectedValues<T>(
|
||||
sut: IScriptingLanguageFactory<T>,
|
||||
expectedValues: Map<ScriptingLanguage, T>,
|
||||
) {
|
||||
if (!expectedValues?.size) {
|
||||
if (!expectedValues.size) {
|
||||
throw new Error('No expected values provided.');
|
||||
}
|
||||
describe('`create` creates expected values', () => {
|
||||
@@ -83,7 +82,6 @@ function testLanguageValidation<T>(sut: IScriptingLanguageFactory<T>) {
|
||||
// assert
|
||||
new EnumRangeTestRunner(act)
|
||||
.testOutOfRangeThrows()
|
||||
.testAbsentValueThrows()
|
||||
.testValidValueDoesNotThrow(validValue);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user