Skip to content

Commit

Permalink
test: reorganize structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMalch committed Oct 22, 2020
1 parent 4a6b8f9 commit d8aa727
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,38 @@ describe('contracts', () => {
contractTest(asserts, AssertionError, 'Failed Assertion');
});

describe('NonNullish contracts', () => {
const contractTest = (
contract: <T>(value: T, message?: string) => NonNullable<T>,
errorType: new (...args: any[]) => Error,
defaultMessage: string
): void => {
describe(contract.name, () => {
it('should not error if the value is defined', () => {
expect(() => contract('A nice String')).not.toThrowError();
});
it('should throw an Error if the value is not defined', () => {
expect(() => contract(null)).toThrowError(
// eslint-disable-next-line new-cap
new errorType(defaultMessage)
);
});
});
};

contractTest(
requiresNonNullish,
PreconditionError,
'Value must be defined'
);
contractTest(
checksNonNullish,
IllegalStateError,
'Callee invariant violation'
);
contractTest(ensuresNonNullish, PostconditionError, 'Unmet postcondition');
});

describe('utils', () => {
describe('error', () => {
it('should always error', () => {
Expand All @@ -64,38 +96,6 @@ describe('utils', () => {
});
});

describe('NonNullish contract tests', () => {
const contractTest = (
contract: <T>(value: T, message?: string) => NonNullable<T>,
errorType: new (...args: any[]) => Error,
defaultMessage: string
): void => {
describe(contract.name, () => {
it('should not error if the value is defined', () => {
expect(() => contract('A nice String')).not.toThrowError();
});
it('should throw an Error if the value is not defined', () => {
expect(() => contract(null)).toThrowError(
// eslint-disable-next-line new-cap
new errorType(defaultMessage)
);
});
});
};

contractTest(
requiresNonNullish,
PreconditionError,
'Value must be defined'
);
contractTest(
checksNonNullish,
IllegalStateError,
'Callee invariant violation'
);
contractTest(ensuresNonNullish, PostconditionError, 'Unmet postcondition');
});

describe('isDefined', () => {
it('should return true for defined values', () => {
expect(isDefined('TypeScript')).toBe(true);
Expand Down

0 comments on commit d8aa727

Please sign in to comment.