@augment-vir/assert 30.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/dist/assertions/boolean.d.ts +97 -0
- package/dist/assertions/boolean.js +47 -0
- package/dist/assertions/boundary.d.ts +83 -0
- package/dist/assertions/boundary.js +104 -0
- package/dist/assertions/enum.d.ts +38 -0
- package/dist/assertions/enum.js +42 -0
- package/dist/assertions/equality/entry-equality.d.ts +40 -0
- package/dist/assertions/equality/entry-equality.js +56 -0
- package/dist/assertions/equality/json-equality.d.ts +42 -0
- package/dist/assertions/equality/json-equality.js +75 -0
- package/dist/assertions/equality/simple-equality.d.ts +85 -0
- package/dist/assertions/equality/simple-equality.js +61 -0
- package/dist/assertions/equality/ts-type-equality.d.ts +42 -0
- package/dist/assertions/equality/ts-type-equality.js +18 -0
- package/dist/assertions/extendable-assertions.d.ts +762 -0
- package/dist/assertions/extendable-assertions.js +108 -0
- package/dist/assertions/http.d.ts +36 -0
- package/dist/assertions/http.js +41 -0
- package/dist/assertions/instance.d.ts +43 -0
- package/dist/assertions/instance.js +41 -0
- package/dist/assertions/keys.d.ts +90 -0
- package/dist/assertions/keys.js +111 -0
- package/dist/assertions/length.d.ts +56 -0
- package/dist/assertions/length.js +68 -0
- package/dist/assertions/nullish.d.ts +43 -0
- package/dist/assertions/nullish.js +42 -0
- package/dist/assertions/numeric.d.ts +69 -0
- package/dist/assertions/numeric.js +60 -0
- package/dist/assertions/output.d.ts +83 -0
- package/dist/assertions/output.example.d.ts +1 -0
- package/dist/assertions/output.example.js +13 -0
- package/dist/assertions/output.js +173 -0
- package/dist/assertions/primitive.d.ts +58 -0
- package/dist/assertions/primitive.js +62 -0
- package/dist/assertions/promise.d.ts +65 -0
- package/dist/assertions/promise.js +69 -0
- package/dist/assertions/regexp.d.ts +19 -0
- package/dist/assertions/regexp.js +18 -0
- package/dist/assertions/runtime-type.d.ts +274 -0
- package/dist/assertions/runtime-type.example.d.ts +1 -0
- package/dist/assertions/runtime-type.example.js +3 -0
- package/dist/assertions/runtime-type.js +242 -0
- package/dist/assertions/throws.d.ts +109 -0
- package/dist/assertions/throws.example.d.ts +1 -0
- package/dist/assertions/throws.example.js +11 -0
- package/dist/assertions/throws.js +166 -0
- package/dist/assertions/uuid.d.ts +35 -0
- package/dist/assertions/uuid.js +33 -0
- package/dist/assertions/values.d.ts +106 -0
- package/dist/assertions/values.js +113 -0
- package/dist/augments/assertion-exports.d.ts +5 -0
- package/dist/augments/assertion-exports.js +1 -0
- package/dist/augments/assertion.error.d.ts +23 -0
- package/dist/augments/assertion.error.example.d.ts +1 -0
- package/dist/augments/assertion.error.example.js +3 -0
- package/dist/augments/assertion.error.js +26 -0
- package/dist/augments/guards/assert-wrap.d.ts +244 -0
- package/dist/augments/guards/assert-wrap.example.d.ts +1 -0
- package/dist/augments/guards/assert-wrap.example.js +9 -0
- package/dist/augments/guards/assert-wrap.js +32 -0
- package/dist/augments/guards/assert.d.ts +177 -0
- package/dist/augments/guards/assert.example.d.ts +1 -0
- package/dist/augments/guards/assert.example.js +4 -0
- package/dist/augments/guards/assert.js +32 -0
- package/dist/augments/guards/check-wrap.d.ts +247 -0
- package/dist/augments/guards/check-wrap.example.d.ts +1 -0
- package/dist/augments/guards/check-wrap.example.js +9 -0
- package/dist/augments/guards/check-wrap.js +34 -0
- package/dist/augments/guards/check.d.ts +229 -0
- package/dist/augments/guards/check.example.d.ts +1 -0
- package/dist/augments/guards/check.example.js +5 -0
- package/dist/augments/guards/check.js +21 -0
- package/dist/augments/guards/wait-until.d.ts +262 -0
- package/dist/augments/guards/wait-until.example.d.ts +1 -0
- package/dist/augments/guards/wait-until.example.js +13 -0
- package/dist/augments/guards/wait-until.js +34 -0
- package/dist/augments/if-equals.d.ts +17 -0
- package/dist/augments/if-equals.js +1 -0
- package/dist/guard-types/assert-function.d.ts +2 -0
- package/dist/guard-types/assert-function.js +3 -0
- package/dist/guard-types/assert-wrap-function.d.ts +9 -0
- package/dist/guard-types/assert-wrap-function.js +14 -0
- package/dist/guard-types/check-function.d.ts +11 -0
- package/dist/guard-types/check-function.js +22 -0
- package/dist/guard-types/check-wrap-wrapper-function.d.ts +9 -0
- package/dist/guard-types/check-wrap-wrapper-function.js +19 -0
- package/dist/guard-types/guard-group.d.ts +13 -0
- package/dist/guard-types/guard-group.js +3 -0
- package/dist/guard-types/guard-override.d.ts +4 -0
- package/dist/guard-types/guard-override.js +10 -0
- package/dist/guard-types/wait-until-function.d.ts +63 -0
- package/dist/guard-types/wait-until-function.js +95 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/test-timeout.mock.d.ts +3 -0
- package/dist/test-timeout.mock.js +8 -0
- package/package.json +54 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { MaybePromise, PartialWithNullable, TypedFunction } from '@augment-vir/core';
|
|
2
|
+
import { WaitUntilOptions } from '../guard-types/wait-until-function.js';
|
|
3
|
+
declare function isError(actual: unknown, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): asserts actual is Error;
|
|
4
|
+
/**
|
|
5
|
+
* A type that represents possible error matching patterns. This is used by the `.throws` and
|
|
6
|
+
* `isError`, guards in `@augment-vir/assert` as well as `itCases` in `@augment-vir/test`. Each
|
|
7
|
+
* property is optional, and whichever properties are provided will be checked.
|
|
8
|
+
*
|
|
9
|
+
* @category Assert : Util
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import {assert, type ErrorMatchOptions} from '@augment-vir/assert';
|
|
14
|
+
*
|
|
15
|
+
* // define the options
|
|
16
|
+
* const matchOptions: ErrorMatchOptions = {
|
|
17
|
+
* matchConstructor: Error,
|
|
18
|
+
* matchMessage: 'some error',
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* assert.throws(
|
|
22
|
+
* () => {
|
|
23
|
+
* throw new Error('some error');
|
|
24
|
+
* },
|
|
25
|
+
* // use the options
|
|
26
|
+
* matchOptions,
|
|
27
|
+
* ); // this assertion will pass
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @package @augment-vir/assert
|
|
31
|
+
*/
|
|
32
|
+
export type ErrorMatchOptions = PartialWithNullable<{
|
|
33
|
+
/**
|
|
34
|
+
* A string or RegExp that an error's message will be compared with.
|
|
35
|
+
*
|
|
36
|
+
* - If this is a string, the error's message will checked for _containing_ the given string (not
|
|
37
|
+
* strictly equalling it): `error.message.includes(options.matchMessage)`
|
|
38
|
+
* - If this is a RegExp, the error's message will be tested against it:
|
|
39
|
+
* `error.message.match(options.matchMessage)`
|
|
40
|
+
*
|
|
41
|
+
* If this property is omitted, the error message won't be checked at all.
|
|
42
|
+
*/
|
|
43
|
+
matchMessage: string | RegExp;
|
|
44
|
+
/**
|
|
45
|
+
* A constructor that the error will be compared to with `instanceof`: `error instanceof
|
|
46
|
+
* options.matchConstructor`. If this property is omitted, the error's constructor or
|
|
47
|
+
* inheritance will not be checked.
|
|
48
|
+
*/
|
|
49
|
+
matchConstructor: ErrorConstructor | (new (...args: any[]) => Error);
|
|
50
|
+
}>;
|
|
51
|
+
declare function throws(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): void;
|
|
52
|
+
declare function throws(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<void>;
|
|
53
|
+
declare function throws(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): void;
|
|
54
|
+
declare function throws(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<void>;
|
|
55
|
+
declare function throwsCheck(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined): boolean;
|
|
56
|
+
declare function throwsCheck(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined): Promise<boolean>;
|
|
57
|
+
declare function throwsCheck(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined): boolean;
|
|
58
|
+
declare function throwsCheck(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined): MaybePromise<boolean>;
|
|
59
|
+
declare function throwsAssertWrap(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error;
|
|
60
|
+
declare function throwsAssertWrap(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
|
|
61
|
+
declare function throwsAssertWrap(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error;
|
|
62
|
+
declare function throwsAssertWrap(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<Error>;
|
|
63
|
+
declare function throwsCheckWrap(callbackOrPromise: TypedFunction<void, never>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error | undefined;
|
|
64
|
+
declare function throwsCheckWrap(callbackOrPromise: TypedFunction<void, Promise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Promise<Error | undefined>;
|
|
65
|
+
declare function throwsCheckWrap(callback: TypedFunction<void, any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): Error | undefined;
|
|
66
|
+
declare function throwsCheckWrap(callback: TypedFunction<void, MaybePromise<any>> | Promise<any>, matchOptions?: ErrorMatchOptions | undefined, failureMessage?: string | undefined): MaybePromise<Error | undefined>;
|
|
67
|
+
declare function throwsWaitUntil(callbackOrPromise: TypedFunction<void, any> | Promise<any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
|
|
68
|
+
declare function throwsWaitUntil(matchOptions: ErrorMatchOptions, callbackOrPromise: TypedFunction<void, any> | Promise<any>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined): Promise<Error>;
|
|
69
|
+
export declare const throwGuards: {
|
|
70
|
+
assertions: {
|
|
71
|
+
/**
|
|
72
|
+
* Checks that the input is an instance of the built-in `Error` class and compares it to the
|
|
73
|
+
* given {@link ErrorMatchOptions}, if provided.
|
|
74
|
+
*
|
|
75
|
+
* Type guards the input.
|
|
76
|
+
*/
|
|
77
|
+
isError: typeof isError;
|
|
78
|
+
/**
|
|
79
|
+
* If a function input is provided:
|
|
80
|
+
*
|
|
81
|
+
* Calls that function and checks that the function throw an error, comparing the error with the
|
|
82
|
+
* given {@link ErrorMatchOptions}, if provided.
|
|
83
|
+
*
|
|
84
|
+
* If a promise is provided:
|
|
85
|
+
*
|
|
86
|
+
* Awaits the promise and checks that the promise rejected with an error, comparing the error
|
|
87
|
+
* with the given {@link ErrorMatchOptions}, if provided.
|
|
88
|
+
*
|
|
89
|
+
* This method will automatically type itself as async vs async based on the input. (A promise
|
|
90
|
+
* or async function inputs results in async. Otherwise, sync.)
|
|
91
|
+
*
|
|
92
|
+
* Performs no type guarding.
|
|
93
|
+
*/
|
|
94
|
+
throws: typeof throws;
|
|
95
|
+
};
|
|
96
|
+
checkOverrides: {
|
|
97
|
+
throws: typeof throwsCheck;
|
|
98
|
+
};
|
|
99
|
+
assertWrapOverrides: {
|
|
100
|
+
throws: typeof throwsAssertWrap;
|
|
101
|
+
};
|
|
102
|
+
checkWrapOverrides: {
|
|
103
|
+
throws: typeof throwsCheckWrap;
|
|
104
|
+
};
|
|
105
|
+
waitUntilOverrides: {
|
|
106
|
+
throws: typeof throwsWaitUntil;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { assert } from '@augment-vir/assert';
|
|
2
|
+
// define the options
|
|
3
|
+
const matchOptions = {
|
|
4
|
+
matchConstructor: Error,
|
|
5
|
+
matchMessage: 'some error',
|
|
6
|
+
};
|
|
7
|
+
assert.throws(() => {
|
|
8
|
+
throw new Error('some error');
|
|
9
|
+
},
|
|
10
|
+
// use the options
|
|
11
|
+
matchOptions); // this assertion will pass
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { ensureError, extractErrorMessage, stringify, } from '@augment-vir/core';
|
|
2
|
+
import { AssertionError } from '../augments/assertion.error.js';
|
|
3
|
+
import { createWaitUntil } from '../guard-types/wait-until-function.js';
|
|
4
|
+
var ThrowsCheckType;
|
|
5
|
+
(function (ThrowsCheckType) {
|
|
6
|
+
ThrowsCheckType["Assert"] = "assert";
|
|
7
|
+
ThrowsCheckType["AssertWrap"] = "assert-wrap";
|
|
8
|
+
ThrowsCheckType["CheckWrap"] = "check-wrap";
|
|
9
|
+
ThrowsCheckType["Check"] = "check";
|
|
10
|
+
})(ThrowsCheckType || (ThrowsCheckType = {}));
|
|
11
|
+
function isError(actual, matchOptions, failureMessage) {
|
|
12
|
+
internalAssertError(actual, {
|
|
13
|
+
noError: 'No error.',
|
|
14
|
+
notInstance: `'${stringify(actual)}' is not an error instance.`,
|
|
15
|
+
}, matchOptions, failureMessage);
|
|
16
|
+
}
|
|
17
|
+
function assertThrownError(actual, matchOptions, failureMessage) {
|
|
18
|
+
internalAssertError(actual, {
|
|
19
|
+
noError: 'No Error was thrown.',
|
|
20
|
+
notInstance: `Thrown value '${stringify(actual)}' is not an error instance.`,
|
|
21
|
+
}, matchOptions, failureMessage);
|
|
22
|
+
}
|
|
23
|
+
function internalAssertError(actual, errorMessages, matchOptions, failureMessage) {
|
|
24
|
+
if (!actual) {
|
|
25
|
+
throw new AssertionError(errorMessages.noError, failureMessage);
|
|
26
|
+
}
|
|
27
|
+
else if (!(actual instanceof Error)) {
|
|
28
|
+
throw new AssertionError(errorMessages.notInstance, failureMessage);
|
|
29
|
+
}
|
|
30
|
+
else if (matchOptions?.matchConstructor &&
|
|
31
|
+
!(actual instanceof matchOptions.matchConstructor)) {
|
|
32
|
+
const constructorName = actual.constructor.name;
|
|
33
|
+
throw new AssertionError(`Error constructor '${constructorName}' did not match expected constructor '${matchOptions.matchConstructor.name}'.`, failureMessage);
|
|
34
|
+
}
|
|
35
|
+
else if (matchOptions?.matchMessage) {
|
|
36
|
+
const message = extractErrorMessage(actual);
|
|
37
|
+
if (typeof matchOptions.matchMessage === 'string') {
|
|
38
|
+
if (!message.includes(matchOptions.matchMessage)) {
|
|
39
|
+
throw new AssertionError(`Error message\n\n'${message}'\n\ndoes not contain\n\n'${matchOptions.matchMessage}'.`, failureMessage);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (!message.match(matchOptions.matchMessage)) {
|
|
43
|
+
throw new AssertionError(`Error message\n\n'${message}'\n\ndoes not match RegExp\n\n'${matchOptions.matchMessage}'.`, failureMessage);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function internalThrowsCheck(checkType, callbackOrPromise, matchOptions, failureMessage) {
|
|
48
|
+
let caughtError = undefined;
|
|
49
|
+
try {
|
|
50
|
+
const result = callbackOrPromise instanceof Promise ? callbackOrPromise : callbackOrPromise();
|
|
51
|
+
if (result instanceof Promise) {
|
|
52
|
+
return new Promise(async (resolve, reject) => {
|
|
53
|
+
try {
|
|
54
|
+
await result;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
caughtError = ensureError(error);
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
assertThrownError(caughtError, matchOptions, failureMessage);
|
|
61
|
+
if (checkType === ThrowsCheckType.Assert) {
|
|
62
|
+
resolve();
|
|
63
|
+
}
|
|
64
|
+
else if (checkType === ThrowsCheckType.Check) {
|
|
65
|
+
resolve(true);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
resolve(caughtError);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (checkType === ThrowsCheckType.CheckWrap) {
|
|
73
|
+
resolve(undefined);
|
|
74
|
+
}
|
|
75
|
+
else if (checkType === ThrowsCheckType.Check) {
|
|
76
|
+
resolve(false);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
reject(ensureError(error));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
caughtError = ensureError(error);
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
assertThrownError(caughtError, matchOptions, failureMessage);
|
|
90
|
+
if (checkType === ThrowsCheckType.Check) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
else if (checkType !== ThrowsCheckType.Assert) {
|
|
94
|
+
return caughtError;
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (checkType === ThrowsCheckType.CheckWrap) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
else if (checkType === ThrowsCheckType.Check) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function throws(callbackOrPromise, matchOptions, failureMessage) {
|
|
111
|
+
return internalThrowsCheck(ThrowsCheckType.Assert, callbackOrPromise, matchOptions, failureMessage);
|
|
112
|
+
}
|
|
113
|
+
function throwsCheck(callbackOrPromise, matchOptions) {
|
|
114
|
+
return internalThrowsCheck(ThrowsCheckType.Check, callbackOrPromise, matchOptions);
|
|
115
|
+
}
|
|
116
|
+
function throwsAssertWrap(callbackOrPromise, matchOptions, failureMessage) {
|
|
117
|
+
return internalThrowsCheck(ThrowsCheckType.AssertWrap, callbackOrPromise, matchOptions, failureMessage);
|
|
118
|
+
}
|
|
119
|
+
function throwsCheckWrap(callbackOrPromise, matchOptions, failureMessage) {
|
|
120
|
+
return internalThrowsCheck(ThrowsCheckType.CheckWrap, callbackOrPromise, matchOptions, failureMessage);
|
|
121
|
+
}
|
|
122
|
+
const internalWaitUntilThrows = createWaitUntil(isError);
|
|
123
|
+
function throwsWaitUntil(matchOptionsOrCallbackOrPromise, callbackOrPromiseOrOptions, optionsOrFailureMessage, failureMessage) {
|
|
124
|
+
const matchOptions = typeof matchOptionsOrCallbackOrPromise === 'function' ||
|
|
125
|
+
matchOptionsOrCallbackOrPromise instanceof Promise
|
|
126
|
+
? undefined
|
|
127
|
+
: matchOptionsOrCallbackOrPromise;
|
|
128
|
+
const callbackOrPromise = (matchOptions ? callbackOrPromiseOrOptions : matchOptionsOrCallbackOrPromise);
|
|
129
|
+
const actualFailureMessage = typeof optionsOrFailureMessage === 'object' ? failureMessage : optionsOrFailureMessage;
|
|
130
|
+
const waitUntilOptions = typeof optionsOrFailureMessage === 'object'
|
|
131
|
+
? optionsOrFailureMessage
|
|
132
|
+
: callbackOrPromiseOrOptions;
|
|
133
|
+
return internalWaitUntilThrows(matchOptions, async () => {
|
|
134
|
+
try {
|
|
135
|
+
if (typeof callbackOrPromise === 'function') {
|
|
136
|
+
await callbackOrPromise();
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
await callbackOrPromise;
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
return ensureError(error);
|
|
145
|
+
}
|
|
146
|
+
}, waitUntilOptions, actualFailureMessage);
|
|
147
|
+
}
|
|
148
|
+
const assertions = {
|
|
149
|
+
throws,
|
|
150
|
+
isError: isError,
|
|
151
|
+
};
|
|
152
|
+
export const throwGuards = {
|
|
153
|
+
assertions,
|
|
154
|
+
checkOverrides: {
|
|
155
|
+
throws: throwsCheck,
|
|
156
|
+
},
|
|
157
|
+
assertWrapOverrides: {
|
|
158
|
+
throws: throwsAssertWrap,
|
|
159
|
+
},
|
|
160
|
+
checkWrapOverrides: {
|
|
161
|
+
throws: throwsCheckWrap,
|
|
162
|
+
},
|
|
163
|
+
waitUntilOverrides: {
|
|
164
|
+
throws: throwsWaitUntil,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MaybePromise, Uuid } from '@augment-vir/core';
|
|
2
|
+
import { WaitUntilOptions } from '../guard-types/wait-until-function.js';
|
|
3
|
+
/** Checks if the input string is a valid v4 UUID. */
|
|
4
|
+
declare function isUuid(actual: unknown, failureMessage?: string | undefined): asserts actual is Uuid;
|
|
5
|
+
declare function isNotUuid<const Actual>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, Uuid>;
|
|
6
|
+
export declare const uuidGuards: {
|
|
7
|
+
assertions: {
|
|
8
|
+
/**
|
|
9
|
+
* Check if the given string is a valid UUID. This supports UUID versions 1-8 and does _not_
|
|
10
|
+
* support the Nil UUID (all zeros) or the Max UUID (all Fs).
|
|
11
|
+
*
|
|
12
|
+
* Type guards the input.
|
|
13
|
+
*/
|
|
14
|
+
isUuid: typeof isUuid;
|
|
15
|
+
/**
|
|
16
|
+
* Check if the given string is _not_ a valid UUID.
|
|
17
|
+
*
|
|
18
|
+
* Type guards the input.
|
|
19
|
+
*/
|
|
20
|
+
isNotUuid: typeof isNotUuid;
|
|
21
|
+
};
|
|
22
|
+
checkOverrides: {
|
|
23
|
+
isNotUuid: <const Actual>(actual: Actual, failureMessage?: string | undefined) => actual is Exclude<Actual, Uuid>;
|
|
24
|
+
};
|
|
25
|
+
assertWrapOverrides: {
|
|
26
|
+
isNotUuid: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, Uuid>;
|
|
27
|
+
};
|
|
28
|
+
checkWrapOverrides: {
|
|
29
|
+
isNotUuid: <const Actual>(actual: Actual, failureMessage?: string | undefined) => Exclude<Actual, Uuid> | undefined;
|
|
30
|
+
};
|
|
31
|
+
waitUntilOverrides: {
|
|
32
|
+
isNotUuid: <const Actual>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Uuid>>;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AssertionError } from '../augments/assertion.error.js';
|
|
2
|
+
import { autoGuard } from '../guard-types/guard-override.js';
|
|
3
|
+
const uuidRegExp = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
4
|
+
/** Checks if the input string is a valid v4 UUID. */
|
|
5
|
+
function isUuid(actual, failureMessage) {
|
|
6
|
+
if (!String(actual).match(uuidRegExp)) {
|
|
7
|
+
throw new AssertionError(`'${String(actual)}' is not a UUID.`, failureMessage);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function isNotUuid(actual, failureMessage) {
|
|
11
|
+
if (String(actual).match(uuidRegExp)) {
|
|
12
|
+
throw new AssertionError(`'${String(actual)}' is a UUID.`, failureMessage);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const assertions = {
|
|
16
|
+
isUuid,
|
|
17
|
+
isNotUuid,
|
|
18
|
+
};
|
|
19
|
+
export const uuidGuards = {
|
|
20
|
+
assertions,
|
|
21
|
+
checkOverrides: {
|
|
22
|
+
isNotUuid: autoGuard(),
|
|
23
|
+
},
|
|
24
|
+
assertWrapOverrides: {
|
|
25
|
+
isNotUuid: autoGuard(),
|
|
26
|
+
},
|
|
27
|
+
checkWrapOverrides: {
|
|
28
|
+
isNotUuid: autoGuard(),
|
|
29
|
+
},
|
|
30
|
+
waitUntilOverrides: {
|
|
31
|
+
isNotUuid: autoGuard(),
|
|
32
|
+
},
|
|
33
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { NarrowToActual, NarrowToExpected } from '@augment-vir/core';
|
|
2
|
+
import { AnyObject, MaybePromise, Values } from '@augment-vir/core';
|
|
3
|
+
import type { EmptyObject } from 'type-fest';
|
|
4
|
+
import type { WaitUntilOptions } from '../guard-types/wait-until-function.js';
|
|
5
|
+
declare function hasValue(parent: object, value: unknown, failureMessage?: string | undefined): void;
|
|
6
|
+
declare function lacksValue(parent: object, value: unknown, failureMessage?: string | undefined): void;
|
|
7
|
+
declare function hasValues(parent: object, values: ReadonlyArray<unknown>, failureMessage?: string | undefined): void;
|
|
8
|
+
declare function lacksValues(parent: object, values: ReadonlyArray<unknown>, failureMessage?: string | undefined): void;
|
|
9
|
+
export declare function isIn<const Parent extends object | string>(child: unknown, parent: Parent, failureMessage?: string | undefined): asserts child is Values<Parent>;
|
|
10
|
+
declare function isNotIn<const Parent extends object | string, const Child>(child: Child, parent: Parent, failureMessage?: string | undefined): asserts child is Exclude<Child, Values<Parent>>;
|
|
11
|
+
/**
|
|
12
|
+
* All types that can be checked for emptiness. The empty variants of these types are represented in
|
|
13
|
+
* {@link Empty}.
|
|
14
|
+
*
|
|
15
|
+
* @category Assert : Util
|
|
16
|
+
* @package @augment-vir/assert
|
|
17
|
+
*/
|
|
18
|
+
export type CanBeEmpty = string | Map<any, any> | Set<any> | AnyObject | any[];
|
|
19
|
+
/**
|
|
20
|
+
* Empty versions of {@link CanBeEmpty}. Note that there is no way to distinguish an empty `Set` or
|
|
21
|
+
* `Map` from their non-empty counterparts in TypeScript (so you will get no emptiness type safety
|
|
22
|
+
* for them.)
|
|
23
|
+
*
|
|
24
|
+
* @category Assert : Util
|
|
25
|
+
* @package @augment-vir/assert
|
|
26
|
+
*/
|
|
27
|
+
export type Empty = '' | EmptyObject | [] | Map<any, any> | Set<any>;
|
|
28
|
+
declare function isEmpty<const Actual extends CanBeEmpty>(actual: Actual, failureMessage?: string | undefined): asserts actual is NarrowToActual<Actual, Empty>;
|
|
29
|
+
declare function isNotEmpty<const Actual extends CanBeEmpty>(actual: Actual, failureMessage?: string | undefined): asserts actual is Exclude<Actual, Empty>;
|
|
30
|
+
export declare const valueGuards: {
|
|
31
|
+
assertions: {
|
|
32
|
+
/**
|
|
33
|
+
* Check if an object/array includes the given value through reference equality.
|
|
34
|
+
*
|
|
35
|
+
* Performs no type guarding.
|
|
36
|
+
*/
|
|
37
|
+
hasValue: typeof hasValue;
|
|
38
|
+
/**
|
|
39
|
+
* Check if an object/array does _not_ include the given value through reference equality.
|
|
40
|
+
*
|
|
41
|
+
* Performs no type guarding.
|
|
42
|
+
*/
|
|
43
|
+
lacksValue: typeof lacksValue;
|
|
44
|
+
/**
|
|
45
|
+
* Check if an object/array includes the given values through reference equality.
|
|
46
|
+
*
|
|
47
|
+
* Performs no type guarding.
|
|
48
|
+
*/
|
|
49
|
+
hasValues: typeof hasValues;
|
|
50
|
+
/**
|
|
51
|
+
* Check if an object/array does _not_ include the given values through reference equality.
|
|
52
|
+
*
|
|
53
|
+
* Performs no type guarding.
|
|
54
|
+
*/
|
|
55
|
+
lacksValues: typeof lacksValues;
|
|
56
|
+
/**
|
|
57
|
+
* Check if a child value is contained within a parent object, array, or string.
|
|
58
|
+
*
|
|
59
|
+
* Type guards the child when possible.
|
|
60
|
+
*/
|
|
61
|
+
isIn: typeof isIn;
|
|
62
|
+
/**
|
|
63
|
+
* Check if a child value is _not_ contained within a parent object, array, or string.
|
|
64
|
+
*
|
|
65
|
+
* Type guards the child when possible.
|
|
66
|
+
*/
|
|
67
|
+
isNotIn: typeof isNotIn;
|
|
68
|
+
/**
|
|
69
|
+
* Check if a value is empty.
|
|
70
|
+
*
|
|
71
|
+
* Type guards the value.
|
|
72
|
+
*/
|
|
73
|
+
isEmpty: typeof isEmpty;
|
|
74
|
+
/**
|
|
75
|
+
* Check if a value is not empty.
|
|
76
|
+
*
|
|
77
|
+
* Type guards the value.
|
|
78
|
+
*/
|
|
79
|
+
isNotEmpty: typeof isNotEmpty;
|
|
80
|
+
};
|
|
81
|
+
checkOverrides: {
|
|
82
|
+
isIn: <const Parent>(child: unknown, parent: Parent) => child is Values<Parent>;
|
|
83
|
+
isNotIn: <const Parent, const Child>(child: Child, parent: Parent, failureMessage?: string | undefined) => child is Exclude<Child, Values<Parent>>;
|
|
84
|
+
isEmpty: <const Actual extends CanBeEmpty>(actual: Actual) => actual is NarrowToActual<Actual, Empty>;
|
|
85
|
+
isNotEmpty: <const Actual extends CanBeEmpty>(actual: Actual) => actual is Exclude<Actual, Empty>;
|
|
86
|
+
};
|
|
87
|
+
assertWrapOverrides: {
|
|
88
|
+
isIn: <const Child, const Parent>(child: Child, parent: Parent, failureMessage?: string | undefined) => NarrowToExpected<Child, Values<Parent>>;
|
|
89
|
+
isNotIn: <const Parent, const Child>(child: Child, parent: Parent, failureMessage?: string | undefined) => Exclude<Child, Values<Parent>>;
|
|
90
|
+
isEmpty: <const Actual extends CanBeEmpty>(actual: Actual, failureMessage?: string | undefined) => NarrowToActual<Actual, Empty>;
|
|
91
|
+
isNotEmpty: <const Actual extends CanBeEmpty>(actual: Actual) => Exclude<Actual, Empty>;
|
|
92
|
+
};
|
|
93
|
+
checkWrapOverrides: {
|
|
94
|
+
isIn: <const Child, const Parent>(child: Child, parent: Parent) => NarrowToExpected<Child, Values<Parent>> | undefined;
|
|
95
|
+
isNotIn: <const Parent, const Child>(child: Child, parent: Parent, failureMessage?: string | undefined) => Exclude<Child, Values<Parent>> | undefined;
|
|
96
|
+
isEmpty: <const Actual extends CanBeEmpty>(actual: Actual) => NarrowToActual<Actual, Empty> | undefined;
|
|
97
|
+
isNotEmpty: <const Actual extends CanBeEmpty>(actual: Actual) => Exclude<Actual, Empty> | undefined;
|
|
98
|
+
};
|
|
99
|
+
waitUntilOverrides: {
|
|
100
|
+
isIn: <const Child, const Parent>(parent: Parent, callback: () => MaybePromise<Child>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToExpected<Child, Values<Parent>>>;
|
|
101
|
+
isNotIn: <const Child, const Parent>(parent: Parent, callback: () => MaybePromise<Child>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Child, Values<Parent>>>;
|
|
102
|
+
isEmpty: <const Actual extends CanBeEmpty>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToActual<Actual, Empty>>;
|
|
103
|
+
isNotEmpty: <const Actual extends CanBeEmpty>(callback: () => MaybePromise<Actual>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Actual, Empty>>;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
export {};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { stringify } from '@augment-vir/core';
|
|
2
|
+
import { AssertionError } from '../augments/assertion.error.js';
|
|
3
|
+
import { autoGuard } from '../guard-types/guard-override.js';
|
|
4
|
+
function hasValue(parent, value, failureMessage) {
|
|
5
|
+
/** Wrap this in a try/catch because `Reflect.ownKeys` can fail depending on what its input is. */
|
|
6
|
+
try {
|
|
7
|
+
const hasValue = Reflect.ownKeys(parent)
|
|
8
|
+
.map((key) => parent[key])
|
|
9
|
+
.includes(value);
|
|
10
|
+
if (!hasValue) {
|
|
11
|
+
throw new Error('fail');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
throw new AssertionError(`'${stringify(parent)}' does not have value '${stringify(value)}'.`, failureMessage);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function lacksValue(parent, value, failureMessage) {
|
|
19
|
+
try {
|
|
20
|
+
hasValue(parent, value);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
throw new AssertionError(`'${stringify(parent)}' has value '${stringify(value)}'.`, failureMessage);
|
|
26
|
+
}
|
|
27
|
+
function hasValues(parent, values, failureMessage) {
|
|
28
|
+
values.forEach((value) => hasValue(parent, value, failureMessage));
|
|
29
|
+
}
|
|
30
|
+
function lacksValues(parent, values, failureMessage) {
|
|
31
|
+
values.forEach((value) => lacksValue(parent, value, failureMessage));
|
|
32
|
+
}
|
|
33
|
+
export function isIn(child, parent, failureMessage) {
|
|
34
|
+
if (typeof parent === 'string') {
|
|
35
|
+
if (!parent.includes(child)) {
|
|
36
|
+
throw new AssertionError(`${stringify(child)} is not in '${parent}'.`, failureMessage);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
hasValue(parent, child, failureMessage);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function isNotIn(child, parent, failureMessage) {
|
|
44
|
+
try {
|
|
45
|
+
isIn(child, parent);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
throw new AssertionError(`${stringify(child)} is not in ${stringify(parent)}.`, failureMessage);
|
|
51
|
+
}
|
|
52
|
+
function isEmpty(actual, failureMessage) {
|
|
53
|
+
const input = actual;
|
|
54
|
+
if (!input) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
else if (typeof input !== 'string' && typeof input !== 'object') {
|
|
58
|
+
throw new TypeError(`Cannot check if '${stringify(input)}' is empty.`);
|
|
59
|
+
}
|
|
60
|
+
else if ((typeof input === 'string' && input) ||
|
|
61
|
+
(Array.isArray(input) && input.length) ||
|
|
62
|
+
(input instanceof Map && input.size) ||
|
|
63
|
+
(input instanceof Set && input.size) ||
|
|
64
|
+
(input && typeof input === 'object' && Object.keys(input).length)) {
|
|
65
|
+
throw new AssertionError(`'${stringify(actual)}' is not empty.`, failureMessage);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function isNotEmpty(actual, failureMessage) {
|
|
69
|
+
try {
|
|
70
|
+
isEmpty(actual);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
throw new AssertionError(`'${stringify(actual)}' is empty.`, failureMessage);
|
|
76
|
+
}
|
|
77
|
+
const assertions = {
|
|
78
|
+
hasValue,
|
|
79
|
+
lacksValue,
|
|
80
|
+
hasValues,
|
|
81
|
+
lacksValues,
|
|
82
|
+
isIn,
|
|
83
|
+
isNotIn,
|
|
84
|
+
isEmpty,
|
|
85
|
+
isNotEmpty,
|
|
86
|
+
};
|
|
87
|
+
export const valueGuards = {
|
|
88
|
+
assertions,
|
|
89
|
+
checkOverrides: {
|
|
90
|
+
isIn: autoGuard(),
|
|
91
|
+
isNotIn: autoGuard(),
|
|
92
|
+
isEmpty: autoGuard(),
|
|
93
|
+
isNotEmpty: autoGuard(),
|
|
94
|
+
},
|
|
95
|
+
assertWrapOverrides: {
|
|
96
|
+
isIn: autoGuard(),
|
|
97
|
+
isNotIn: autoGuard(),
|
|
98
|
+
isEmpty: autoGuard(),
|
|
99
|
+
isNotEmpty: autoGuard(),
|
|
100
|
+
},
|
|
101
|
+
checkWrapOverrides: {
|
|
102
|
+
isIn: autoGuard(),
|
|
103
|
+
isNotIn: autoGuard(),
|
|
104
|
+
isEmpty: autoGuard(),
|
|
105
|
+
isNotEmpty: autoGuard(),
|
|
106
|
+
},
|
|
107
|
+
waitUntilOverrides: {
|
|
108
|
+
isIn: autoGuard(),
|
|
109
|
+
isNotIn: autoGuard(),
|
|
110
|
+
isEmpty: autoGuard(),
|
|
111
|
+
isNotEmpty: autoGuard(),
|
|
112
|
+
},
|
|
113
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { Falsy, FalsyValue, Truthy } from '../assertions/boolean.js';
|
|
2
|
+
export type { CustomOutputAsserter } from '../assertions/output.js';
|
|
3
|
+
export { RuntimeType, getRuntimeType } from '../assertions/runtime-type.js';
|
|
4
|
+
export type { ErrorMatchOptions } from '../assertions/throws.js';
|
|
5
|
+
export type { CanBeEmpty, Empty } from '../assertions/values.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { RuntimeType, getRuntimeType } from '../assertions/runtime-type.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error thrown by the `@augment-vir/assert` package when an assertion fails.
|
|
3
|
+
*
|
|
4
|
+
* This requires both a `baseMessage` (the default "this thing failed" error message defined inside
|
|
5
|
+
* `@augment-vir/assert` for each assert method), and a possibly-undefined `userCustomizedMessage`
|
|
6
|
+
* (the optional user-defined failure message for any assertion).
|
|
7
|
+
*
|
|
8
|
+
* @category Assert : Util
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {AssertionError} from '@augment-vir/assert';
|
|
13
|
+
*
|
|
14
|
+
* // the message from this error will be: `'User defined message: The assertion failed.'`
|
|
15
|
+
* throw new AssertionError('The assertion failed.', 'User defined message.');
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @package @augment-vir/assert
|
|
19
|
+
*/
|
|
20
|
+
export declare class AssertionError extends Error {
|
|
21
|
+
name: string;
|
|
22
|
+
constructor(baseMessage: string, userCustomizedMessage: string | undefined);
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { combineErrorMessages } from '@augment-vir/core';
|
|
2
|
+
/**
|
|
3
|
+
* An error thrown by the `@augment-vir/assert` package when an assertion fails.
|
|
4
|
+
*
|
|
5
|
+
* This requires both a `baseMessage` (the default "this thing failed" error message defined inside
|
|
6
|
+
* `@augment-vir/assert` for each assert method), and a possibly-undefined `userCustomizedMessage`
|
|
7
|
+
* (the optional user-defined failure message for any assertion).
|
|
8
|
+
*
|
|
9
|
+
* @category Assert : Util
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import {AssertionError} from '@augment-vir/assert';
|
|
14
|
+
*
|
|
15
|
+
* // the message from this error will be: `'User defined message: The assertion failed.'`
|
|
16
|
+
* throw new AssertionError('The assertion failed.', 'User defined message.');
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @package @augment-vir/assert
|
|
20
|
+
*/
|
|
21
|
+
export class AssertionError extends Error {
|
|
22
|
+
name = 'AssertionError';
|
|
23
|
+
constructor(baseMessage, userCustomizedMessage) {
|
|
24
|
+
super(combineErrorMessages(userCustomizedMessage, baseMessage) || 'Assertion failed.');
|
|
25
|
+
}
|
|
26
|
+
}
|