Add more and unify tests for absent object cases

- Unify test data for nonexistence of an object/string and collection.
- Introduce more test through adding missing test data to existing tests.
- Improve logic for checking absence of values to match tests.
- Add missing tests for absent value validation.
- Update documentation to include shared test functionality.
This commit is contained in:
undergroundwires
2022-01-21 22:34:11 +01:00
parent 0e52a99efa
commit 44d79e2c9a
100 changed files with 1380 additions and 976 deletions

View File

@@ -0,0 +1,17 @@
import { expect } from 'chai';
export async function expectThrowsAsync(
method: () => Promise<unknown>,
errorMessage: string,
) {
let error: Error;
try {
await method();
} catch (err) {
error = err;
}
expect(error).to.be.an(Error.name);
if (errorMessage) {
expect(error.message).to.equal(errorMessage);
}
}

View File

@@ -0,0 +1,68 @@
export function itEachAbsentStringValue(runner: (absentValue: string) => void): void {
itEachTestCase(AbsentStringTestCases, runner);
}
export function itEachAbsentObjectValue(runner: (absentValue: AbsentObjectType) => void): void {
itEachTestCase(AbsentObjectTestCases, runner);
}
export function itEachAbsentCollectionValue<T>(runner: (absentValue: []) => void): void {
itEachTestCase(getAbsentCollectionTestCases<T>(), runner);
}
function itEachTestCase<T>(
testCases: readonly IAbsentTestCase<T>[],
runner: (absentValue: T) => void,
): void {
for (const testCase of testCases) {
it(`given "${testCase.valueName}"`, () => {
runner(testCase.absentValue);
});
}
}
export const AbsentObjectTestCases: readonly IAbsentTestCase<AbsentObjectType>[] = [
{
valueName: 'undefined',
absentValue: undefined,
},
{
valueName: 'null',
absentValue: null,
},
];
export const AbsentStringTestCases: readonly IAbsentStringCase[] = [
{
valueName: 'empty',
absentValue: '',
},
...AbsentObjectTestCases,
];
export function getAbsentCollectionTestCases<T>(): readonly IAbsentCollectionCase<T>[] {
return [
...AbsentObjectTestCases,
{
valueName: 'empty',
absentValue: new Array<T>(),
},
];
}
type AbsentObjectType = undefined | null;
interface IAbsentTestCase<T> {
valueName: string;
absentValue: T;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IAbsentStringCase extends IAbsentTestCase<string> {
// Marker interface
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IAbsentCollectionCase<T> extends IAbsentTestCase<T[]> {
// Marker interface
}