bupkis 0.1.2 → 0.2.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/CHANGELOG.md +13 -0
- package/README.md +8 -8
- package/dist/commonjs/assertion/assertion-types.d.ts +33 -82
- package/dist/commonjs/assertion/assertion-types.d.ts.map +1 -1
- package/dist/commonjs/assertion/create.d.ts +5 -33
- package/dist/commonjs/assertion/create.d.ts.map +1 -1
- package/dist/commonjs/assertion/create.js +17 -6
- package/dist/commonjs/assertion/create.js.map +1 -1
- package/dist/commonjs/assertion/impl/async.d.ts +5 -5
- package/dist/commonjs/assertion/impl/async.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/async.js +2 -2
- package/dist/commonjs/assertion/impl/async.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync-parametric.d.ts +24 -24
- package/dist/commonjs/assertion/impl/sync-parametric.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/sync-parametric.js +3 -3
- package/dist/commonjs/assertion/impl/sync-parametric.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync.d.ts +50 -50
- package/dist/commonjs/assertion/impl/sync.d.ts.map +1 -1
- package/dist/commonjs/bootstrap.d.ts +71 -61
- package/dist/commonjs/bootstrap.d.ts.map +1 -1
- package/dist/commonjs/bootstrap.js +20 -10
- package/dist/commonjs/bootstrap.js.map +1 -1
- package/dist/commonjs/error.d.ts +12 -5
- package/dist/commonjs/error.d.ts.map +1 -1
- package/dist/commonjs/error.js +19 -4
- package/dist/commonjs/error.js.map +1 -1
- package/dist/commonjs/expect.d.ts +130 -3
- package/dist/commonjs/expect.d.ts.map +1 -1
- package/dist/commonjs/expect.js +115 -0
- package/dist/commonjs/expect.js.map +1 -1
- package/dist/commonjs/guards.d.ts +22 -15
- package/dist/commonjs/guards.d.ts.map +1 -1
- package/dist/commonjs/guards.js +34 -20
- package/dist/commonjs/guards.js.map +1 -1
- package/dist/commonjs/index.d.ts +113 -62
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +44 -42
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/metadata.d.ts +1 -27
- package/dist/commonjs/metadata.d.ts.map +1 -1
- package/dist/commonjs/metadata.js +16 -15
- package/dist/commonjs/metadata.js.map +1 -1
- package/dist/commonjs/schema.d.ts +59 -26
- package/dist/commonjs/schema.d.ts.map +1 -1
- package/dist/commonjs/schema.js +57 -24
- package/dist/commonjs/schema.js.map +1 -1
- package/dist/commonjs/types.d.ts +449 -39
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/commonjs/types.js +12 -2
- package/dist/commonjs/types.js.map +1 -1
- package/dist/commonjs/util.d.ts +7 -0
- package/dist/commonjs/util.d.ts.map +1 -1
- package/dist/commonjs/util.js +7 -0
- package/dist/commonjs/util.js.map +1 -1
- package/dist/esm/assertion/assertion-types.d.ts +33 -82
- package/dist/esm/assertion/assertion-types.d.ts.map +1 -1
- package/dist/esm/assertion/create.d.ts +5 -33
- package/dist/esm/assertion/create.d.ts.map +1 -1
- package/dist/esm/assertion/create.js +14 -4
- package/dist/esm/assertion/create.js.map +1 -1
- package/dist/esm/assertion/impl/async.d.ts +5 -5
- package/dist/esm/assertion/impl/async.d.ts.map +1 -1
- package/dist/esm/assertion/impl/async.js +2 -2
- package/dist/esm/assertion/impl/async.js.map +1 -1
- package/dist/esm/assertion/impl/sync-parametric.d.ts +24 -24
- package/dist/esm/assertion/impl/sync-parametric.d.ts.map +1 -1
- package/dist/esm/assertion/impl/sync-parametric.js +3 -3
- package/dist/esm/assertion/impl/sync-parametric.js.map +1 -1
- package/dist/esm/assertion/impl/sync.d.ts +50 -50
- package/dist/esm/assertion/impl/sync.d.ts.map +1 -1
- package/dist/esm/bootstrap.d.ts +71 -61
- package/dist/esm/bootstrap.d.ts.map +1 -1
- package/dist/esm/bootstrap.js +19 -9
- package/dist/esm/bootstrap.js.map +1 -1
- package/dist/esm/error.d.ts +12 -5
- package/dist/esm/error.d.ts.map +1 -1
- package/dist/esm/error.js +19 -4
- package/dist/esm/error.js.map +1 -1
- package/dist/esm/expect.d.ts +130 -3
- package/dist/esm/expect.d.ts.map +1 -1
- package/dist/esm/expect.js +115 -0
- package/dist/esm/expect.js.map +1 -1
- package/dist/esm/guards.d.ts +22 -15
- package/dist/esm/guards.d.ts.map +1 -1
- package/dist/esm/guards.js +29 -13
- package/dist/esm/guards.js.map +1 -1
- package/dist/esm/index.d.ts +113 -62
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +46 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/metadata.d.ts +1 -27
- package/dist/esm/metadata.d.ts.map +1 -1
- package/dist/esm/metadata.js +2 -1
- package/dist/esm/metadata.js.map +1 -1
- package/dist/esm/schema.d.ts +59 -26
- package/dist/esm/schema.d.ts.map +1 -1
- package/dist/esm/schema.js +57 -24
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/types.d.ts +449 -39
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +12 -2
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/util.d.ts +7 -0
- package/dist/esm/util.d.ts.map +1 -1
- package/dist/esm/util.js +7 -0
- package/dist/esm/util.js.map +1 -1
- package/package.json +89 -15
- package/src/assertion/assertion-types.ts +38 -41
- package/src/assertion/create.ts +16 -65
- package/src/assertion/impl/async.ts +2 -2
- package/src/assertion/impl/sync-parametric.ts +3 -3
- package/src/bootstrap.ts +21 -10
- package/src/error.ts +21 -4
- package/src/expect.ts +269 -18
- package/src/guards.ts +29 -51
- package/src/index.ts +71 -11
- package/src/metadata.ts +3 -4
- package/src/schema.ts +59 -26
- package/src/types.ts +593 -72
- package/src/util.ts +7 -0
- package/dist/commonjs/api.d.ts +0 -93
- package/dist/commonjs/api.d.ts.map +0 -1
- package/dist/commonjs/api.js +0 -8
- package/dist/commonjs/api.js.map +0 -1
- package/dist/esm/api.d.ts +0 -93
- package/dist/esm/api.d.ts.map +0 -1
- package/dist/esm/api.js +0 -7
- package/dist/esm/api.js.map +0 -1
- package/src/api.ts +0 -149
- package/src/schema.md +0 -15
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,6CAAmE;AAEnE,+CAGuB;AACvB,2CAAkC;AAElC
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,6CAAmE;AAEnE,+CAGuB;AACvB,2CAAkC;AAElC;;;GAGG;AACH,MAAa,cAAe,SAAQ,4BAAkB;IACpD;;OAEG;IACH,CAAC,mCAAqB,CAAC,GAAG,IAAI,CAAC;IAEtB,IAAI,GAAG,gBAAgB,CAAC;IAEjC;;OAEG;IACH,YAAY,OAA6D;QACvE,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,gBAAgB,CAAC,KAAc;QACpC,OAAO,CACL,IAAA,eAAG,EAAC,KAAK,EAAE,4BAAkB,CAAC;YAC9B,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,mCAAqB,CAAC,CAC5C,CAAC;IACJ,CAAC;CACF;AA3BD,wCA2BC;AAED;;;;GAIG;AACH,MAAa,qBAAsB,SAAQ,cAAc;IACvD,CAAC,0CAA4B,CAAC,GAAG,IAAI,CAAC;IAEtC,MAAM,CAAC,uBAAuB,CAAC,GAAY;QACzC,OAAO,CACL,IAAA,eAAG,EAAC,GAAG,EAAE,cAAc,CAAC;YACxB,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,0CAA4B,CAAC,CACjD,CAAC;IACJ,CAAC;CACF;AATD,sDASC"}
|
|
@@ -1,9 +1,136 @@
|
|
|
1
|
-
import { type Expect, type ExpectAsync, type ExpectAsyncFunction, type ExpectAsyncProps, type ExpectFunction, type ExpectSyncProps } from './api.js';
|
|
2
1
|
import { type AnyAsyncAssertions, type AnySyncAssertions } from './assertion/assertion-types.js';
|
|
2
|
+
import { type Expect, type ExpectAsync, type ExpectAsyncFunction, type ExpectAsyncProps, type ExpectFunction, type ExpectSyncProps } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an asynchronous expect function by extending a parent expectAsync
|
|
5
|
+
* function with additional assertions.
|
|
6
|
+
*
|
|
7
|
+
* This overload combines assertions from an existing parent expectAsync
|
|
8
|
+
* function with new assertions, creating a unified expectAsync function that
|
|
9
|
+
* supports both sets of assertions. The resulting function inherits all type
|
|
10
|
+
* information from both the parent and new assertions, providing complete
|
|
11
|
+
* TypeScript intellisense and type safety for Promise-based testing scenarios.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const baseExpectAsync = createExpectAsyncFunction(basicAsyncAssertions);
|
|
17
|
+
* const extendedExpectAsync = createExpectAsyncFunction(
|
|
18
|
+
* customAsyncAssertions,
|
|
19
|
+
* baseExpectAsync,
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* // Can use both basic and custom async assertions
|
|
23
|
+
* await extendedExpectAsync(promise, 'to resolve'); // From basic assertions
|
|
24
|
+
* await extendedExpectAsync(promise, 'to resolve with custom data'); // From custom assertions
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param assertions - Array of new asynchronous assertion objects to add
|
|
28
|
+
* @param expect - Parent expectAsync function whose assertions will be
|
|
29
|
+
* inherited
|
|
30
|
+
* @returns ExpectAsync function with combined assertion types from both parent
|
|
31
|
+
* and new assertions
|
|
32
|
+
* @throws {@link AssertionError} When an assertion fails in normal
|
|
33
|
+
* (non-negated) mode
|
|
34
|
+
* @throws {@link NegatedAssertionError} When a negated assertion fails
|
|
35
|
+
* @throws {Error} When no matching assertion can be found for the provided
|
|
36
|
+
* arguments
|
|
37
|
+
*/
|
|
3
38
|
export declare function createExpectAsyncFunction<T extends AnyAsyncAssertions, U extends ExpectAsync<AnyAsyncAssertions>>(assertions: T, expect: U): ExpectAsyncFunction<T & U['assertions']>;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new asynchronous expect function with the provided assertions.
|
|
41
|
+
*
|
|
42
|
+
* This overload creates a standalone expectAsync function from the provided
|
|
43
|
+
* assertions without inheriting from any parent function. This is typically
|
|
44
|
+
* used to create the initial expectAsync function or when you want a clean
|
|
45
|
+
* slate without any inherited assertions for Promise-based testing.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
*
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const expectAsync = createExpectAsyncFunction(asyncAssertions);
|
|
51
|
+
* await expectAsync(promise, 'to resolve');
|
|
52
|
+
* await expectAsync(rejectedPromise, 'to reject');
|
|
53
|
+
* await expectAsync(promise, 'to resolve to', expectedValue);
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @param assertions - Array of asynchronous assertion objects that define the
|
|
57
|
+
* available assertion phrases and Promise-based logic
|
|
58
|
+
* @returns An asynchronous expect function that can execute the provided
|
|
59
|
+
* assertions using natural language syntax
|
|
60
|
+
* @throws {@link AssertionError} When an assertion fails in normal
|
|
61
|
+
* (non-negated) mode
|
|
62
|
+
* @throws {@link NegatedAssertionError} When a negated assertion fails
|
|
63
|
+
* @throws {Error} When no matching assertion can be found for the provided
|
|
64
|
+
* arguments
|
|
65
|
+
*/
|
|
4
66
|
export declare function createExpectAsyncFunction<T extends AnyAsyncAssertions>(assertions: T): ExpectAsyncFunction<T>;
|
|
5
|
-
|
|
6
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Creates a synchronous expect function by extending a parent expect function
|
|
69
|
+
* with additional assertions.
|
|
70
|
+
*
|
|
71
|
+
* This overload combines assertions from an existing parent expect function
|
|
72
|
+
* with new assertions, creating a unified expect function that supports both
|
|
73
|
+
* sets of assertions. The resulting function inherits all type information from
|
|
74
|
+
* both the parent and new assertions, providing complete TypeScript
|
|
75
|
+
* intellisense and type safety.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
*
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const baseExpect = createExpectSyncFunction(basicAssertions);
|
|
81
|
+
* const extendedExpect = createExpectSyncFunction(
|
|
82
|
+
* customAssertions,
|
|
83
|
+
* baseExpect,
|
|
84
|
+
* );
|
|
85
|
+
*
|
|
86
|
+
* // Can use both basic and custom assertions
|
|
87
|
+
* extendedExpect(42, 'to be a number'); // From basic assertions
|
|
88
|
+
* extendedExpect(obj, 'to have custom prop'); // From custom assertions
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @param assertions - Array of new synchronous assertion objects to add
|
|
92
|
+
* @param expect - Parent expect function whose assertions will be inherited
|
|
93
|
+
* @returns Expect function with combined assertion types from both parent and
|
|
94
|
+
* new assertions
|
|
95
|
+
* @throws {@link AssertionError} When an assertion fails in normal
|
|
96
|
+
* (non-negated) mode
|
|
97
|
+
* @throws {@link NegatedAssertionError} When a negated assertion fails
|
|
98
|
+
* @throws {Error} When no matching assertion can be found for the provided
|
|
99
|
+
* arguments
|
|
100
|
+
*/
|
|
101
|
+
export declare function createExpectSyncFunction<Assertions extends AnySyncAssertions, ParentExpect extends Expect<AnySyncAssertions>>(assertions: Assertions, expect: ParentExpect): ExpectFunction<Assertions & ParentExpect['assertions']>;
|
|
102
|
+
/**
|
|
103
|
+
* Creates a new synchronous expect function with the provided assertions.
|
|
104
|
+
*
|
|
105
|
+
* This overload creates a standalone expect function from the provided
|
|
106
|
+
* assertions without inheriting from any parent function. This is typically
|
|
107
|
+
* used to create the initial expect function or when you want a clean slate
|
|
108
|
+
* without any inherited assertions.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
*
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const expect = createExpectSyncFunction(basicAssertions);
|
|
114
|
+
* expect(42, 'to be a number');
|
|
115
|
+
* expect('hello', 'to be a string');
|
|
116
|
+
* expect([], 'to be empty');
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @param assertions - Array of synchronous assertion objects that define the
|
|
120
|
+
* available assertion phrases and logic
|
|
121
|
+
* @returns A synchronous expect function that can execute the provided
|
|
122
|
+
* assertions using natural language syntax
|
|
123
|
+
* @throws {@link AssertionError} When an assertion fails in normal
|
|
124
|
+
* (non-negated) mode
|
|
125
|
+
* @throws {@link NegatedAssertionError} When a negated assertion fails
|
|
126
|
+
* @throws {Error} When no matching assertion can be found for the provided
|
|
127
|
+
* arguments
|
|
128
|
+
*/
|
|
129
|
+
export declare function createExpectSyncFunction<Assertions extends AnySyncAssertions>(assertions: Assertions): ExpectFunction<Assertions>;
|
|
130
|
+
/**
|
|
131
|
+
* Used by a {@link UseFn} to create base properties of the {@link Expect} and
|
|
132
|
+
* {@link ExpectAsync} functions.
|
|
133
|
+
*/
|
|
7
134
|
export declare function createBaseExpect<T extends AnySyncAssertions, U extends AnyAsyncAssertions>(syncAssertions: T, asyncAssertions: U, type: 'sync'): ExpectSyncProps<T, U>;
|
|
8
135
|
export declare function createBaseExpect<T extends AnySyncAssertions, U extends AnyAsyncAssertions>(syncAssertions: T, asyncAssertions: U, type: 'async'): ExpectAsyncProps<U, T>;
|
|
9
136
|
//# sourceMappingURL=expect.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expect.d.ts","sourceRoot":"","sources":["../../src/expect.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,MAAM,EACX,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,eAAe,
|
|
1
|
+
{"version":3,"file":"expect.d.ts","sourceRoot":"","sources":["../../src/expect.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,kBAAkB,EAEvB,KAAK,iBAAiB,EASvB,MAAM,gCAAgC,CAAC;AAIxC,OAAO,EACL,KAAK,MAAM,EACX,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB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,EAAE,eAAe,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,GAAG,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
package/dist/commonjs/expect.js
CHANGED
|
@@ -13,6 +13,68 @@ const error_js_1 = require("./error.js");
|
|
|
13
13
|
const guards_js_1 = require("./guards.js");
|
|
14
14
|
const use_js_1 = require("./use.js");
|
|
15
15
|
const debug = (0, debug_1.default)('bupkis:expect');
|
|
16
|
+
/**
|
|
17
|
+
* Implementation function that creates an asynchronous expect function with
|
|
18
|
+
* optional parent inheritance.
|
|
19
|
+
*
|
|
20
|
+
* This is the concrete implementation that handles both overload cases for
|
|
21
|
+
* Promise-based assertions. It creates an expectAsync function that uses a
|
|
22
|
+
* two-phase matching algorithm: first seeking exact phrase matches for optimal
|
|
23
|
+
* performance, then falling back to partial matches if needed. The function
|
|
24
|
+
* processes negation keywords, combines parent assertions with new ones, and
|
|
25
|
+
* ensures all operations are properly awaited.
|
|
26
|
+
*
|
|
27
|
+
* The matching algorithm prioritizes exact matches to minimize performance
|
|
28
|
+
* overhead, but provides flexibility through partial matching when exact
|
|
29
|
+
* phrases don't align. This enables natural language flexibility while
|
|
30
|
+
* maintaining execution speed for common async assertion patterns.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* The function performs async assertion matching in the following order:
|
|
34
|
+
*
|
|
35
|
+
* 1. Awaits `Promise.resolve()` to ensure the function is always asynchronous
|
|
36
|
+
* 2. Processes negation keywords ('not', 'to not') to determine assertion mode
|
|
37
|
+
* 3. Combines parent assertions (if provided) with new assertions in execution
|
|
38
|
+
* order
|
|
39
|
+
* 4. Attempts to parse arguments against each assertion's expected phrase pattern
|
|
40
|
+
* using `parseValuesAsync`
|
|
41
|
+
* 5. Prioritizes exact phrase matches over partial matches for performance
|
|
42
|
+
* 6. Executes the first successful match using {@link executeAsync} or throws an
|
|
43
|
+
* error if none found
|
|
44
|
+
*
|
|
45
|
+
* Performance considerations: The function loops through all available
|
|
46
|
+
* assertions for each call, but uses early termination when exact matches are
|
|
47
|
+
* found. For performance-critical code, consider using assertion functions with
|
|
48
|
+
* fewer total assertions or more specific phrase patterns to reduce matching
|
|
49
|
+
* overhead.
|
|
50
|
+
*
|
|
51
|
+
* All assertion execution is properly awaited to handle Promise-based
|
|
52
|
+
* validation logic, error handling, and negation scenarios in asynchronous
|
|
53
|
+
* contexts.
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* ```typescript
|
|
57
|
+
* // Used internally by both public overloads
|
|
58
|
+
* const expectAsync1 = createExpectAsyncFunction(assertions); // No parent
|
|
59
|
+
* const expectAsync2 = createExpectAsyncFunction(assertions, parent); // With parent
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @param assertions - Array of asynchronous assertion objects to make available
|
|
63
|
+
* @param expect - Optional parent expectAsync function to inherit assertions
|
|
64
|
+
* from
|
|
65
|
+
* @returns Asynchronous expect function that processes natural language
|
|
66
|
+
* assertions with Promise support
|
|
67
|
+
* @throws {@link AssertionError} When an assertion fails in normal
|
|
68
|
+
* (non-negated) mode
|
|
69
|
+
* @throws {@link NegatedAssertionError} When a negated assertion fails (e.g.,
|
|
70
|
+
* `await expectAsync(promise, 'not to resolve')`)
|
|
71
|
+
* @throws {Error} When no matching assertion can be found for the provided
|
|
72
|
+
* arguments
|
|
73
|
+
* @internal This is the concrete implementation used by the public overloads
|
|
74
|
+
* @see {@link createExpectSyncFunction} for creating synchronous expect functions
|
|
75
|
+
* @see {@link createAsyncAssertion} for creating individual async assertion objects
|
|
76
|
+
* @see {@link ExpectAsync} for the main expectAsync interface
|
|
77
|
+
*/
|
|
16
78
|
function createExpectAsyncFunction(assertions, expect) {
|
|
17
79
|
debug('Creating expectAsync function with %d assertions', assertions.length + (expect?.assertions.length ?? 0));
|
|
18
80
|
const expectAsyncFunction = async (...args) => {
|
|
@@ -39,6 +101,59 @@ function createExpectAsyncFunction(assertions, expect) {
|
|
|
39
101
|
};
|
|
40
102
|
return expectAsyncFunction;
|
|
41
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Implementation function that creates a synchronous expect function with
|
|
106
|
+
* optional parent inheritance.
|
|
107
|
+
*
|
|
108
|
+
* This is the concrete implementation that handles both overload cases. It
|
|
109
|
+
* creates an expect function that uses a two-phase matching algorithm: first
|
|
110
|
+
* seeking exact phrase matches for optimal performance, then falling back to
|
|
111
|
+
* partial matches if needed. The function processes negation keywords and
|
|
112
|
+
* combines parent assertions with new ones.
|
|
113
|
+
*
|
|
114
|
+
* The matching algorithm prioritizes exact matches to minimize performance
|
|
115
|
+
* overhead, but provides flexibility through partial matching when exact
|
|
116
|
+
* phrases don't align. This enables natural language flexibility while
|
|
117
|
+
* maintaining execution speed for common assertion patterns.
|
|
118
|
+
*
|
|
119
|
+
* @remarks
|
|
120
|
+
* The function performs assertion matching in the following order:
|
|
121
|
+
*
|
|
122
|
+
* 1. Processes negation keywords ('not', 'to not') to determine assertion mode
|
|
123
|
+
* 2. Combines parent assertions (if provided) with new assertions in execution
|
|
124
|
+
* order
|
|
125
|
+
* 3. Attempts to parse arguments against each assertion's expected phrase pattern
|
|
126
|
+
* 4. Prioritizes exact phrase matches over partial matches for performance
|
|
127
|
+
* 5. Executes the first successful match or throws an error if none found
|
|
128
|
+
*
|
|
129
|
+
* Performance considerations: The function loops through all available
|
|
130
|
+
* assertions for each call, but uses early termination when exact matches are
|
|
131
|
+
* found. For performance-critical code, consider using assertion functions with
|
|
132
|
+
* fewer total assertions or more specific phrase patterns to reduce matching
|
|
133
|
+
* overhead.
|
|
134
|
+
* @example
|
|
135
|
+
*
|
|
136
|
+
* ```typescript
|
|
137
|
+
* // Used internally by both public overloads
|
|
138
|
+
* const expect1 = createExpectSyncFunction(assertions); // No parent
|
|
139
|
+
* const expect2 = createExpectSyncFunction(assertions, parent); // With parent
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @param assertions - Array of synchronous assertion objects to make available
|
|
143
|
+
* @param expect - Optional parent expect function to inherit assertions from
|
|
144
|
+
* @returns Synchronous expect function that processes natural language
|
|
145
|
+
* assertions
|
|
146
|
+
* @throws {@link AssertionError} When an assertion fails in normal
|
|
147
|
+
* (non-negated) mode
|
|
148
|
+
* @throws {@link NegatedAssertionError} When a negated assertion fails (e.g.,
|
|
149
|
+
* `expect(42, 'not to be a number')`)
|
|
150
|
+
* @throws {Error} When no matching assertion can be found for the provided
|
|
151
|
+
* arguments
|
|
152
|
+
* @internal This is the concrete implementation used by the public overloads
|
|
153
|
+
* @see {@link createExpectAsyncFunction} for creating asynchronous expect functions
|
|
154
|
+
* @see {@link createAssertion} for creating individual assertion objects
|
|
155
|
+
* @see {@link Expect} for the main expect interface
|
|
156
|
+
*/
|
|
42
157
|
function createExpectSyncFunction(assertions, expect) {
|
|
43
158
|
debug('Creating expect function with %d assertions', assertions.length + (expect?.assertions.length ?? 0));
|
|
44
159
|
const expectFunction = (...args) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expect.js","sourceRoot":"","sources":["../../src/expect.ts"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"expect.js","sourceRoot":"","sources":["../../src/expect.ts"],"names":[],"mappings":";;;;;AAwKA,8DAgDC;AAgID,4DA+CC;AA4MD,4CAmBC;AAtmBD,kDAA0B;AAC1B,+BAA+B;AAgB/B,qDAA8E;AAC9E,yCAAmE;AACnE,2CAA2D;AAY3D,qCAAqC;AAErC,MAAM,KAAK,GAAG,IAAA,eAAK,EAAC,eAAe,CAAC,CAAC;AAyErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AACH,SAAgB,yBAAyB,CAGvC,UAAa,EAAE,MAAU;IACzB,KAAK,CACH,kDAAkD,EAClD,UAAU,CAAC,MAAM,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,MAAM,IAAI,CAAC,CAAC,CACrD,CAAC;IACF,MAAM,mBAAmB,G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;YAC1B,eAAe,EAAf,2BAAe;YACf,oBAAoB,EAApB,gCAAoB;YACpB,IAAI;YACJ,GAAG,EAAE,IAAA,kBAAS,EAAC,cAAc,EAAE,eAAe,CAAC;SAChD;QACH,CAAC,CAAC;YACE,UAAU,EAAE,eAAe;YAC3B,eAAe,EAAf,2BAAe;YACf,oBAAoB,EAApB,gCAAoB;YACpB,IAAI;YACJ,GAAG,EAAE,IAAA,kBAAS,EAAC,cAAc,EAAE,eAAe,CAAC;SAChD,CAAC;AACR,CAAC"}
|
|
@@ -2,26 +2,32 @@
|
|
|
2
2
|
* Type guard functions and runtime type checking utilities.
|
|
3
3
|
*
|
|
4
4
|
* This module provides various type guard functions for runtime type checking,
|
|
5
|
-
* including guards for Zod schemas, constructors,
|
|
6
|
-
* assertion parts. These are used throughout the library for safe type
|
|
5
|
+
* including guards for Zod schemas, constructors, {@link PromiseLike} objects,
|
|
6
|
+
* and assertion parts. These are used throughout the library for safe type
|
|
7
7
|
* narrowing and validation.
|
|
8
8
|
*
|
|
9
|
+
* @category API
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import * as guards from 'bupkis/guards';
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
9
16
|
* @packageDocumentation
|
|
10
17
|
*/
|
|
11
18
|
import { type Primitive } from 'type-fest';
|
|
12
|
-
import { z } from 'zod';
|
|
13
|
-
import type { AssertionFailure } from './assertion/assertion-types.js';
|
|
19
|
+
import { z } from 'zod/v4';
|
|
14
20
|
import type { Constructor } from './types.js';
|
|
15
21
|
/**
|
|
16
|
-
* Returns true if the given value looks like a Zod schema
|
|
17
|
-
* the presence of an internal
|
|
22
|
+
* Returns `true` if the given value looks like a Zod v4 schema, determined by
|
|
23
|
+
* the presence of an internal {@link z.core.$ZodTypeDef} field.
|
|
18
24
|
*
|
|
19
25
|
* Note: This relies on Zod's internal shape and is intended for runtime
|
|
20
26
|
* discrimination within this library.
|
|
21
27
|
*
|
|
22
28
|
* @template T
|
|
23
29
|
* @param value - Value to test
|
|
24
|
-
* @returns Whether the value is
|
|
30
|
+
* @returns Whether the value is `ZodType`-like
|
|
25
31
|
*/
|
|
26
32
|
export declare const isZodType: (value: unknown) => value is z.ZodType;
|
|
27
33
|
/**
|
|
@@ -39,11 +45,17 @@ export declare const isZodPromise: (value: unknown) => value is z.ZodPromise;
|
|
|
39
45
|
*/
|
|
40
46
|
export declare const isPromiseLike: (value: unknown) => value is PromiseLike<unknown>;
|
|
41
47
|
/**
|
|
42
|
-
* Returns true if the given value is a constructable function (i.e., a
|
|
48
|
+
* Returns `true` if the given value is a constructable function (i.e., a
|
|
49
|
+
* class).
|
|
43
50
|
*
|
|
44
|
-
* This
|
|
45
|
-
*
|
|
51
|
+
* This works by wrapping `fn` in a {@link Proxy}, attaching a no-op
|
|
52
|
+
* {@link ProxyHandler.construct} trap to it, then attempting to construct the
|
|
53
|
+
* proxy via `new`.
|
|
46
54
|
*
|
|
55
|
+
* @privateRemarks
|
|
56
|
+
* This may be the only way we can determine, at runtime, if a function is a
|
|
57
|
+
* constructor without actually calling it. I am unsure if this only works for
|
|
58
|
+
* classes.
|
|
47
59
|
* @param fn - Function to test
|
|
48
60
|
* @returns Whether the function is constructable
|
|
49
61
|
*/
|
|
@@ -62,8 +74,6 @@ export declare const isBoolean: (value: unknown) => value is boolean;
|
|
|
62
74
|
* @returns `true` if the value is a function, `false` otherwise
|
|
63
75
|
*/
|
|
64
76
|
export declare const isFunction: (value: unknown) => value is (...args: any[]) => any;
|
|
65
|
-
export declare const isAssertionFailure: (value: unknown) => value is AssertionFailure;
|
|
66
|
-
export declare const isAsyncFunction: (value: unknown) => value is (...args: any[]) => Promise<any>;
|
|
67
77
|
/**
|
|
68
78
|
* Type guard for a string value
|
|
69
79
|
*
|
|
@@ -85,9 +95,6 @@ export declare const isNonNullObject: (value: unknown) => value is object;
|
|
|
85
95
|
* @returns `true` if the value is null or not an object, `false` otherwise
|
|
86
96
|
*/
|
|
87
97
|
export declare const isNullOrNonObject: (value: unknown) => value is null | Primitive;
|
|
88
|
-
export type PrimitiveTypeName = 'bigint' | 'boolean' | 'function' | 'null' | 'number' | 'object' | 'string' | 'symbol' | 'undefined';
|
|
89
|
-
export type PrimitiveTypeNameToType<T extends PrimitiveTypeName> = T extends 'undefined' ? undefined : T extends 'object' ? null | object : T extends 'function' ? (...args: any[]) => any : T extends 'string' ? string : T extends 'number' ? number : T extends 'boolean' ? boolean : T extends 'bigint' ? bigint : T extends 'symbol' ? symbol : never;
|
|
90
|
-
export declare const isType: <T extends PrimitiveTypeName>(a: unknown, b: T) => a is PrimitiveTypeNameToType<T>;
|
|
91
98
|
export declare const isA: <T extends Constructor>(value: unknown, ctor: T) => value is InstanceType<T>;
|
|
92
99
|
export declare const isError: (value: unknown) => value is Error;
|
|
93
100
|
//# sourceMappingURL=guards.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAC;AAO3B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,CAAC,CAAC,OAQnD,CAAC;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,CAAC,CAAC,UACP,CAAC;AAEnD;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,WAAW,CAAC,OAAO,CAMxE,CAAC;AAEJ;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,eAAe,GAAI,IAAI,OAAO,KAAG,EAAE,IAAI,WAYnD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,OACxB,CAAC;AAE7B;;;;;GAKG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAC5C,CAAC;AA6B9B;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MACxB,CAAC;AAE5B;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MACb,CAAC;AAE9C;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,IAAI,GAAG,SACtB,CAAC;AAyB9C,eAAO,MAAM,GAAG,GAAI,CAAC,SAAS,WAAW,EACvC,OAAO,OAAO,EACd,MAAM,CAAC,KACN,KAAK,IAAI,YAAY,CAAC,CAAC,CAEzB,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,KAA0B,CAAC"}
|
package/dist/commonjs/guards.js
CHANGED
|
@@ -3,25 +3,32 @@
|
|
|
3
3
|
* Type guard functions and runtime type checking utilities.
|
|
4
4
|
*
|
|
5
5
|
* This module provides various type guard functions for runtime type checking,
|
|
6
|
-
* including guards for Zod schemas, constructors,
|
|
7
|
-
* assertion parts. These are used throughout the library for safe type
|
|
6
|
+
* including guards for Zod schemas, constructors, {@link PromiseLike} objects,
|
|
7
|
+
* and assertion parts. These are used throughout the library for safe type
|
|
8
8
|
* narrowing and validation.
|
|
9
9
|
*
|
|
10
|
+
* @category API
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import * as guards from 'bupkis/guards';
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
10
17
|
* @packageDocumentation
|
|
11
18
|
*/
|
|
12
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.isError = exports.isA = exports.
|
|
14
|
-
const
|
|
20
|
+
exports.isError = exports.isA = exports.isPhraseLiteral = exports.isPhraseLiteralChoice = exports.isNullOrNonObject = exports.isNonNullObject = exports.isString = exports.isAssertionFailure = exports.isFunction = exports.isBoolean = exports.isConstructable = exports.isPromiseLike = exports.isZodPromise = exports.isZodType = void 0;
|
|
21
|
+
const v4_1 = require("zod/v4");
|
|
15
22
|
/**
|
|
16
|
-
* Returns true if the given value looks like a Zod schema
|
|
17
|
-
* the presence of an internal
|
|
23
|
+
* Returns `true` if the given value looks like a Zod v4 schema, determined by
|
|
24
|
+
* the presence of an internal {@link z.core.$ZodTypeDef} field.
|
|
18
25
|
*
|
|
19
26
|
* Note: This relies on Zod's internal shape and is intended for runtime
|
|
20
27
|
* discrimination within this library.
|
|
21
28
|
*
|
|
22
29
|
* @template T
|
|
23
30
|
* @param value - Value to test
|
|
24
|
-
* @returns Whether the value is
|
|
31
|
+
* @returns Whether the value is `ZodType`-like
|
|
25
32
|
*/
|
|
26
33
|
const isZodType = (value) => !!(value &&
|
|
27
34
|
typeof value === 'object' &&
|
|
@@ -50,11 +57,17 @@ const isPromiseLike = (value) => !!(value &&
|
|
|
50
57
|
typeof value.then === 'function');
|
|
51
58
|
exports.isPromiseLike = isPromiseLike;
|
|
52
59
|
/**
|
|
53
|
-
* Returns true if the given value is a constructable function (i.e., a
|
|
60
|
+
* Returns `true` if the given value is a constructable function (i.e., a
|
|
61
|
+
* class).
|
|
54
62
|
*
|
|
55
|
-
* This
|
|
56
|
-
*
|
|
63
|
+
* This works by wrapping `fn` in a {@link Proxy}, attaching a no-op
|
|
64
|
+
* {@link ProxyHandler.construct} trap to it, then attempting to construct the
|
|
65
|
+
* proxy via `new`.
|
|
57
66
|
*
|
|
67
|
+
* @privateRemarks
|
|
68
|
+
* This may be the only way we can determine, at runtime, if a function is a
|
|
69
|
+
* constructor without actually calling it. I am unsure if this only works for
|
|
70
|
+
* classes.
|
|
58
71
|
* @param fn - Function to test
|
|
59
72
|
* @returns Whether the function is constructable
|
|
60
73
|
*/
|
|
@@ -89,24 +102,29 @@ exports.isBoolean = isBoolean;
|
|
|
89
102
|
*/
|
|
90
103
|
const isFunction = (value) => typeof value === 'function';
|
|
91
104
|
exports.isFunction = isFunction;
|
|
92
|
-
const AssertionFailureSchema =
|
|
93
|
-
actual:
|
|
105
|
+
const AssertionFailureSchema = v4_1.z.object({
|
|
106
|
+
actual: v4_1.z
|
|
94
107
|
.unknown()
|
|
95
108
|
.optional()
|
|
96
109
|
.describe('The actual value or description of what actually occurred'),
|
|
97
|
-
expected:
|
|
110
|
+
expected: v4_1.z
|
|
98
111
|
.unknown()
|
|
99
112
|
.optional()
|
|
100
113
|
.describe('The expected value or description of what was expected to occur'),
|
|
101
|
-
message:
|
|
114
|
+
message: v4_1.z
|
|
102
115
|
.string()
|
|
103
116
|
.optional()
|
|
104
117
|
.describe('A human-readable message describing the failure'),
|
|
105
118
|
});
|
|
119
|
+
/**
|
|
120
|
+
* Type guard for a {@link AssertionFailure} object
|
|
121
|
+
*
|
|
122
|
+
* @param value Value to check
|
|
123
|
+
* @returns `true` if the value is an `AssertionFailure`, `false` otherwise
|
|
124
|
+
* @internal
|
|
125
|
+
*/
|
|
106
126
|
const isAssertionFailure = (value) => AssertionFailureSchema.safeParse(value).success;
|
|
107
127
|
exports.isAssertionFailure = isAssertionFailure;
|
|
108
|
-
const isAsyncFunction = (value) => (0, exports.isFunction)(value) && value.constructor.name === 'AsyncFunction';
|
|
109
|
-
exports.isAsyncFunction = isAsyncFunction;
|
|
110
128
|
/**
|
|
111
129
|
* Type guard for a string value
|
|
112
130
|
*
|
|
@@ -150,10 +168,6 @@ exports.isPhraseLiteralChoice = isPhraseLiteralChoice;
|
|
|
150
168
|
*/
|
|
151
169
|
const isPhraseLiteral = (value) => (0, exports.isString)(value) && !value.startsWith('not ');
|
|
152
170
|
exports.isPhraseLiteral = isPhraseLiteral;
|
|
153
|
-
const isType = (a, b) => {
|
|
154
|
-
return typeof a === b;
|
|
155
|
-
};
|
|
156
|
-
exports.isType = isType;
|
|
157
171
|
const isA = (value, ctor) => {
|
|
158
172
|
return (0, exports.isNonNullObject)(value) && value instanceof ctor;
|
|
159
173
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guards.js","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"guards.js","sourceRoot":"","sources":["../../src/guards.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;GAgBG;;;AAGH,+BAA2B;AAS3B;;;;;;;;;;GAUG;AACI,MAAM,SAAS,GAAG,CAAC,KAAc,EAAsB,EAAE,CAC9D,CAAC,CAAC,CACA,KAAK;IACL,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,IAAI,KAAK;IACd,KAAK,CAAC,GAAG;IACT,OAAO,KAAK,CAAC,GAAG,KAAK,QAAQ;IAC7B,MAAM,IAAI,KAAK,CAAC,GAAG,CACpB,CAAC;AARS,QAAA,SAAS,aAQlB;AAEJ;;;;;GAKG;AACI,MAAM,YAAY,GAAG,CAAC,KAAc,EAAyB,EAAE,CACpE,IAAA,iBAAS,EAAC,KAAK,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC;AADtC,QAAA,YAAY,gBAC0B;AAEnD;;;;;GAKG;AACI,MAAM,aAAa,GAAG,CAAC,KAAc,EAAiC,EAAE,CAC7E,CAAC,CAAC,CACA,KAAK;IACL,OAAO,KAAK,KAAK,QAAQ;IACzB,sEAAsE;IACtE,OAAQ,KAAa,CAAC,IAAI,KAAK,UAAU,CAC1C,CAAC;AANS,QAAA,aAAa,iBAMtB;AAEJ;;;;;;;;;;;;;;GAcG;AACI,MAA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