assertron 10.0.0 → 10.4.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/README.md +2 -1
- package/cjs/assert-order/AssertOrder.d.ts +1 -1
- package/cjs/assert-order/AssertOrder.d.ts.map +1 -1
- package/cjs/assert-order/AssertOrder.js +103 -176
- package/cjs/assert-order/AssertOrder.js.map +1 -1
- package/cjs/assert-order/InvalidOrder.d.ts +1 -1
- package/cjs/assert-order/InvalidOrder.d.ts.map +1 -1
- package/cjs/assert-order/InvalidOrder.js +18 -34
- package/cjs/assert-order/InvalidOrder.js.map +1 -1
- package/cjs/assert-order/StateMachine.d.ts +1 -1
- package/cjs/assert-order/StateMachine.d.ts.map +1 -1
- package/cjs/assert-order/StateMachine.js +102 -125
- package/cjs/assert-order/StateMachine.js.map +1 -1
- package/cjs/assert-order/types.d.ts.map +1 -1
- package/cjs/assertron/assertron.d.ts +20 -19
- package/cjs/assertron/assertron.d.ts.map +1 -1
- package/cjs/assertron/assertron.js +22 -13
- package/cjs/assertron/assertron.js.map +1 -1
- package/cjs/assertron/falsy.d.ts +1 -1
- package/cjs/assertron/falsy.d.ts.map +1 -1
- package/cjs/assertron/falsy.js +2 -2
- package/cjs/assertron/falsy.js.map +1 -1
- package/cjs/assertron/index.d.ts +1 -0
- package/cjs/assertron/index.d.ts.map +1 -1
- package/cjs/assertron/isInstanceof.d.ts +3 -0
- package/cjs/assertron/isInstanceof.d.ts.map +1 -0
- package/cjs/assertron/isInstanceof.js +11 -0
- package/cjs/assertron/isInstanceof.js.map +1 -0
- package/cjs/assertron/pathEqual.d.ts.map +1 -1
- package/cjs/assertron/pathEqual.js +3 -3
- package/cjs/assertron/pathEqual.js.map +1 -1
- package/cjs/assertron/rejects.d.ts +1 -1
- package/cjs/assertron/rejects.d.ts.map +1 -1
- package/cjs/assertron/rejects.js +5 -3
- package/cjs/assertron/rejects.js.map +1 -1
- package/cjs/assertron/repeat.d.ts.map +1 -1
- package/cjs/assertron/repeat.js +6 -4
- package/cjs/assertron/repeat.js.map +1 -1
- package/cjs/assertron/resolves.d.ts.map +1 -1
- package/cjs/assertron/resolves.js +5 -3
- package/cjs/assertron/resolves.js.map +1 -1
- package/cjs/assertron/satisfies.d.ts +4 -3
- package/cjs/assertron/satisfies.d.ts.map +1 -1
- package/cjs/assertron/satisfies.js +6 -5
- package/cjs/assertron/satisfies.js.map +1 -1
- package/cjs/assertron/throws.d.ts +1 -1
- package/cjs/assertron/throws.d.ts.map +1 -1
- package/cjs/assertron/throws.js +15 -11
- package/cjs/assertron/throws.js.map +1 -1
- package/cjs/assertron/truthy.d.ts +1 -1
- package/cjs/assertron/truthy.d.ts.map +1 -1
- package/cjs/assertron/truthy.js +2 -2
- package/cjs/assertron/truthy.js.map +1 -1
- package/cjs/errors.d.ts +3 -3
- package/cjs/errors.d.ts.map +1 -1
- package/cjs/errors.js +12 -32
- package/cjs/errors.js.map +1 -1
- package/cjs/index.d.ts +2 -1
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +16 -2
- package/cjs/index.js.map +1 -1
- package/cjs/package.json +1 -0
- package/cjs/types.d.ts +2 -2
- package/cjs/types.d.ts.map +1 -1
- package/cjs/utils/index.js +2 -2
- package/cjs/utils/index.js.map +1 -1
- package/cjs/utils/isErrorConstructor.d.ts +1 -1
- package/cjs/utils/isErrorConstructor.d.ts.map +1 -1
- package/cjs/utils/isErrorConstructor.js.map +1 -1
- package/cjs/utils/notEqualMessage.js +1 -1
- package/cjs/utils/notEqualMessage.js.map +1 -1
- package/cjs/utils/notSatisfiedMessage.d.ts +1 -1
- package/cjs/utils/notSatisfiedMessage.d.ts.map +1 -1
- package/cjs/utils/notSatisfiedMessage.js +12 -16
- package/cjs/utils/notSatisfiedMessage.js.map +1 -1
- package/cjs/utils/notThrownMessage.js +2 -2
- package/cjs/utils/notThrownMessage.js.map +1 -1
- package/cjs/utils/promiseMessages.d.ts.map +1 -1
- package/cjs/utils/promiseMessages.js +9 -5
- package/cjs/utils/promiseMessages.js.map +1 -1
- package/cjs/utils/unexpectedErrorMessage.d.ts +1 -1
- package/cjs/utils/unexpectedErrorMessage.d.ts.map +1 -1
- package/cjs/utils/unexpectedErrorMessage.js +3 -4
- package/cjs/utils/unexpectedErrorMessage.js.map +1 -1
- package/esm/assert-order/AssertOrder.d.ts +1 -1
- package/esm/assert-order/AssertOrder.d.ts.map +1 -1
- package/esm/assert-order/AssertOrder.js +12 -8
- package/esm/assert-order/AssertOrder.js.map +1 -1
- package/esm/assert-order/InvalidOrder.d.ts +1 -1
- package/esm/assert-order/InvalidOrder.d.ts.map +1 -1
- package/esm/assert-order/InvalidOrder.js +4 -2
- package/esm/assert-order/InvalidOrder.js.map +1 -1
- package/esm/assert-order/StateMachine.d.ts +1 -1
- package/esm/assert-order/StateMachine.d.ts.map +1 -1
- package/esm/assert-order/StateMachine.js +20 -10
- package/esm/assert-order/StateMachine.js.map +1 -1
- package/esm/assert-order/types.d.ts.map +1 -1
- package/esm/assertron/assertron.d.ts +20 -19
- package/esm/assertron/assertron.d.ts.map +1 -1
- package/esm/assertron/assertron.js +10 -1
- package/esm/assertron/assertron.js.map +1 -1
- package/esm/assertron/falsy.d.ts +1 -1
- package/esm/assertron/falsy.d.ts.map +1 -1
- package/esm/assertron/falsy.js.map +1 -1
- package/esm/assertron/index.d.ts +1 -0
- package/esm/assertron/index.d.ts.map +1 -1
- package/esm/assertron/isInstanceof.d.ts +3 -0
- package/esm/assertron/isInstanceof.d.ts.map +1 -0
- package/esm/assertron/isInstanceof.js +7 -0
- package/esm/assertron/isInstanceof.js.map +1 -0
- package/esm/assertron/pathEqual.d.ts.map +1 -1
- package/esm/assertron/pathEqual.js.map +1 -1
- package/esm/assertron/rejects.d.ts +1 -1
- package/esm/assertron/rejects.d.ts.map +1 -1
- package/esm/assertron/rejects.js +3 -1
- package/esm/assertron/rejects.js.map +1 -1
- package/esm/assertron/repeat.d.ts.map +1 -1
- package/esm/assertron/repeat.js +3 -1
- package/esm/assertron/repeat.js.map +1 -1
- package/esm/assertron/resolves.d.ts.map +1 -1
- package/esm/assertron/resolves.js +3 -1
- package/esm/assertron/resolves.js.map +1 -1
- package/esm/assertron/satisfies.d.ts +4 -3
- package/esm/assertron/satisfies.d.ts.map +1 -1
- package/esm/assertron/satisfies.js +2 -1
- package/esm/assertron/satisfies.js.map +1 -1
- package/esm/assertron/throws.d.ts +1 -1
- package/esm/assertron/throws.d.ts.map +1 -1
- package/esm/assertron/throws.js +7 -3
- package/esm/assertron/throws.js.map +1 -1
- package/esm/assertron/truthy.d.ts +1 -1
- package/esm/assertron/truthy.d.ts.map +1 -1
- package/esm/assertron/truthy.js.map +1 -1
- package/esm/errors.d.ts +3 -3
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js.map +1 -1
- package/esm/index.d.ts +2 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -1
- package/esm/index.js.map +1 -1
- package/esm/types.d.ts +2 -2
- package/esm/types.d.ts.map +1 -1
- package/esm/utils/index.js.map +1 -1
- package/esm/utils/isErrorConstructor.d.ts +1 -1
- package/esm/utils/isErrorConstructor.d.ts.map +1 -1
- package/esm/utils/isErrorConstructor.js.map +1 -1
- package/esm/utils/notEqualMessage.js.map +1 -1
- package/esm/utils/notSatisfiedMessage.d.ts +1 -1
- package/esm/utils/notSatisfiedMessage.d.ts.map +1 -1
- package/esm/utils/notSatisfiedMessage.js +8 -3
- package/esm/utils/notSatisfiedMessage.js.map +1 -1
- package/esm/utils/notThrownMessage.js.map +1 -1
- package/esm/utils/promiseMessages.d.ts.map +1 -1
- package/esm/utils/promiseMessages.js +8 -4
- package/esm/utils/promiseMessages.js.map +1 -1
- package/esm/utils/unexpectedErrorMessage.d.ts +1 -1
- package/esm/utils/unexpectedErrorMessage.d.ts.map +1 -1
- package/esm/utils/unexpectedErrorMessage.js +1 -2
- package/esm/utils/unexpectedErrorMessage.js.map +1 -1
- package/package.json +56 -50
- package/ts/assert-order/AssertOrder.ts +139 -143
- package/ts/assert-order/InvalidOrder.ts +23 -19
- package/ts/assert-order/StateMachine.ts +97 -89
- package/ts/assert-order/index.ts +0 -1
- package/ts/assert-order/internalInterfaces.ts +3 -3
- package/ts/assert-order/types.ts +5 -5
- package/ts/assertron/assertron.ts +56 -18
- package/ts/assertron/falsy.ts +2 -3
- package/ts/assertron/index.ts +1 -0
- package/ts/assertron/isInstanceof.ts +11 -0
- package/ts/assertron/pathEqual.ts +3 -3
- package/ts/assertron/rejects.ts +7 -5
- package/ts/assertron/repeat.ts +27 -21
- package/ts/assertron/resolves.ts +7 -5
- package/ts/assertron/satisfies.ts +30 -22
- package/ts/assertron/throws.ts +76 -55
- package/ts/assertron/truthy.ts +2 -5
- package/ts/errors.ts +9 -9
- package/ts/index.ts +17 -1
- package/ts/testUtils.ts +52 -55
- package/ts/types.ts +0 -1
- package/ts/utils/index.ts +1 -1
- package/ts/utils/isErrorConstructor.ts +2 -2
- package/ts/utils/notEqualMessage.ts +1 -1
- package/ts/utils/notSatisfiedMessage.ts +18 -14
- package/ts/utils/notThrownMessage.ts +1 -1
- package/ts/utils/promiseMessages.ts +12 -5
- package/ts/utils/unexpectedErrorMessage.ts +4 -3
|
@@ -1,35 +1,39 @@
|
|
|
1
1
|
import { AssertionError } from '../errors.js'
|
|
2
|
-
import { State } from './types.js'
|
|
2
|
+
import type { State } from './types.js'
|
|
3
3
|
|
|
4
4
|
export class InvalidOrder extends AssertionError {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
constructor(
|
|
6
|
+
public state: State,
|
|
7
|
+
public method: string,
|
|
8
|
+
public args: any[],
|
|
9
|
+
options: AssertionError.Options
|
|
10
|
+
) {
|
|
11
|
+
super(method === 'end' ? getEndMessage(state) : getExpectingMessage(state, method, args), options)
|
|
12
|
+
}
|
|
8
13
|
}
|
|
9
14
|
|
|
10
15
|
function getEndMessage(state: State) {
|
|
11
|
-
|
|
16
|
+
return `Planned for ${state.maxStep} step but expecting step ${state.step} when 'end()' is called`
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
function getExpectingMessage(state: State, method: string, args: any[]) {
|
|
15
|
-
|
|
20
|
+
return `Expecting ${getExpectingCalls(state)}, but received '${method}(${getMethodArgs(args)})'`
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
function getExpectingCalls(state: State) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return message
|
|
24
|
+
let message: string
|
|
25
|
+
if (state.subStep === 0) {
|
|
26
|
+
const methods = ['is', 'once', 'any']
|
|
27
|
+
message = methods
|
|
28
|
+
.map(m => `'${m}(${['any'].indexOf(m) === -1 ? state.step : `[${state.step}]`})'`)
|
|
29
|
+
.join(', ')
|
|
30
|
+
} else {
|
|
31
|
+
const methods = ['exactly']
|
|
32
|
+
message = methods.map(m => `'${m}(${state.step}, ${state.maxSubStep})'`).join(', ')
|
|
33
|
+
}
|
|
34
|
+
return message
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
function getMethodArgs(args: any[]) {
|
|
34
|
-
|
|
38
|
+
return args.map(a => (Array.isArray(a) ? `[${a.join(', ')}]` : a)).join(', ')
|
|
35
39
|
}
|
|
@@ -1,95 +1,103 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { State } from './types.js'
|
|
1
|
+
import * as perf from 'perf_hooks'
|
|
2
|
+
import type { State } from './types.js'
|
|
3
3
|
|
|
4
|
-
let timeTracker: { start(): void
|
|
5
|
-
|
|
6
|
-
if (process && typeof process.hrtime === 'function') {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
4
|
+
let timeTracker: { start(): void; taken(): number }
|
|
5
|
+
|
|
6
|
+
if (global['process'] && typeof global['process'].hrtime === 'function') {
|
|
7
|
+
let tick: [number, number]
|
|
8
|
+
timeTracker = {
|
|
9
|
+
start() {
|
|
10
|
+
tick = global['process'].hrtime()
|
|
11
|
+
},
|
|
12
|
+
taken() {
|
|
13
|
+
const [second, nanoSecond] = global['process'].hrtime(tick)
|
|
14
|
+
return second * 1000 + nanoSecond / 1e6
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
} else if (perf.performance && typeof perf.performance.now === 'function') {
|
|
18
|
+
const now = perf.performance.now
|
|
19
|
+
let tick: number
|
|
20
|
+
timeTracker = {
|
|
21
|
+
start() {
|
|
22
|
+
tick = now()
|
|
23
|
+
},
|
|
24
|
+
taken() {
|
|
25
|
+
return now() - tick
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
let tick: number
|
|
30
|
+
timeTracker = {
|
|
31
|
+
start() {
|
|
32
|
+
tick = new Date().valueOf()
|
|
33
|
+
},
|
|
34
|
+
taken() {
|
|
35
|
+
return new Date().valueOf() - tick
|
|
36
|
+
}
|
|
37
|
+
}
|
|
29
38
|
}
|
|
30
39
|
|
|
31
40
|
export class StateMachine {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
listeners: Record<number, Array<() => void>> = {}
|
|
42
|
+
step = 1
|
|
43
|
+
maxStep?: number
|
|
35
44
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
45
|
+
subStep = 0
|
|
46
|
+
minSubStep?: number
|
|
47
|
+
maxSubStep?: number
|
|
48
|
+
constructor(maxStep?: number) {
|
|
49
|
+
this.maxStep = maxStep
|
|
50
|
+
timeTracker.start()
|
|
51
|
+
}
|
|
52
|
+
jump(step: number) {
|
|
53
|
+
this.step = step
|
|
54
|
+
this.subStep = 0
|
|
55
|
+
return this.step
|
|
56
|
+
}
|
|
57
|
+
move() {
|
|
58
|
+
const listeners = this.listeners[this.step]
|
|
59
|
+
this.step = this.step + 1
|
|
60
|
+
this.subStep = 0
|
|
61
|
+
if (listeners) {
|
|
62
|
+
listeners.forEach(l => l())
|
|
63
|
+
}
|
|
64
|
+
return this.step
|
|
65
|
+
}
|
|
66
|
+
moveSubStep() {
|
|
67
|
+
const subStep = ++this.subStep
|
|
68
|
+
if (this.maxSubStep === this.subStep) this.move()
|
|
69
|
+
return subStep
|
|
70
|
+
}
|
|
71
|
+
get(): State {
|
|
72
|
+
const { step, subStep, maxStep, minSubStep, maxSubStep } = this
|
|
73
|
+
return {
|
|
74
|
+
step,
|
|
75
|
+
maxStep,
|
|
76
|
+
subStep,
|
|
77
|
+
minSubStep,
|
|
78
|
+
maxSubStep
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
on(step: number, listener: () => void) {
|
|
82
|
+
this.listeners[step] = [listener]
|
|
83
|
+
}
|
|
84
|
+
isNotValid(step: number) {
|
|
85
|
+
return step !== this.step || (this.maxStep !== undefined && this.maxStep < step)
|
|
86
|
+
}
|
|
87
|
+
isValid(step: number) {
|
|
88
|
+
return step === this.step
|
|
89
|
+
}
|
|
90
|
+
isMaxStepDefined() {
|
|
91
|
+
return this.maxStep !== undefined
|
|
92
|
+
}
|
|
93
|
+
stopAccepting() {
|
|
94
|
+
this.maxStep = this.step - 1
|
|
95
|
+
}
|
|
96
|
+
isAccepting() {
|
|
97
|
+
// istanbul ignore next
|
|
98
|
+
return this.maxStep ? this.maxStep >= this.step : true
|
|
99
|
+
}
|
|
100
|
+
getTimeTaken() {
|
|
101
|
+
return timeTracker.taken()
|
|
102
|
+
}
|
|
95
103
|
}
|
package/ts/assert-order/index.ts
CHANGED
package/ts/assert-order/types.ts
CHANGED
|
@@ -1,29 +1,67 @@
|
|
|
1
|
+
import type { AnyConstructor } from 'type-plus'
|
|
1
2
|
import { AssertionError } from '../errors.js'
|
|
3
|
+
import type { ErrorConstructor, ErrorValidator } from '../types.js'
|
|
2
4
|
import { notEqualMessage } from '../utils/index.js'
|
|
3
5
|
import { falsy } from './falsy.js'
|
|
6
|
+
import { isInstanceof } from './isInstanceof.js'
|
|
4
7
|
import { pathEqual } from './pathEqual.js'
|
|
5
8
|
import { rejects } from './rejects.js'
|
|
6
9
|
import { repeat } from './repeat.js'
|
|
7
10
|
import { resolves } from './resolves.js'
|
|
8
|
-
import { satisfies } from './satisfies.js'
|
|
11
|
+
import { satisfies, type SatisfyExpectation } from './satisfies.js'
|
|
9
12
|
import { throws } from './throws.js'
|
|
10
13
|
import { truthy } from './truthy.js'
|
|
11
14
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
export type Assertron = {
|
|
16
|
+
false(value: unknown): asserts value is false
|
|
17
|
+
falsy(value: any): asserts value is false | 0 | '' | null | undefined
|
|
18
|
+
isInstanceof<T extends AnyConstructor>(value: unknown, type: T): asserts value is InstanceType<T>
|
|
19
|
+
pathEqual(actual: string, expected: string): void
|
|
20
|
+
rejects<R = any>(promise: Promise<any>): Promise<R>
|
|
21
|
+
repeat<R>(
|
|
22
|
+
fn: () => R | (() => Promise<R>),
|
|
23
|
+
times: number
|
|
24
|
+
): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R
|
|
25
|
+
resolves(promise: Promise<any>): Promise<void>
|
|
26
|
+
satisfies<Actual, Expected extends Actual>(actual: Actual, expected: SatisfyExpectation<Expected>): void
|
|
27
|
+
throws<E extends Error>(value: PromiseLike<any>, error?: ErrorValidator | ErrorConstructor<E>): Promise<E>
|
|
28
|
+
throws<E extends Error>(value: (...args: any[]) => never, error?: ErrorValidator | ErrorConstructor<E>): E
|
|
29
|
+
throws<E extends Error, R = any>(
|
|
30
|
+
value: (...args: any[]) => R,
|
|
31
|
+
error?: ErrorValidator | ErrorConstructor<E>
|
|
32
|
+
): R extends Promise<any> ? Promise<E> : E
|
|
33
|
+
throws<T, R = any>(
|
|
34
|
+
value: (...args: any[]) => R,
|
|
35
|
+
validator?: ErrorValidator
|
|
36
|
+
): R extends Promise<any> ? Promise<T> : T
|
|
37
|
+
true(value: unknown): asserts value is true
|
|
38
|
+
truthy(value: unknown): void
|
|
39
|
+
uuid(value: unknown): void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const assertron: Assertron = {
|
|
43
|
+
false(value: unknown): void {
|
|
44
|
+
if (value !== false) throw new AssertionError(notEqualMessage(value, false), { ssf: assertron.false })
|
|
45
|
+
},
|
|
46
|
+
falsy,
|
|
47
|
+
isInstanceof,
|
|
48
|
+
pathEqual,
|
|
49
|
+
rejects,
|
|
50
|
+
repeat,
|
|
51
|
+
resolves,
|
|
52
|
+
satisfies,
|
|
53
|
+
throws,
|
|
54
|
+
true(value: unknown): void {
|
|
55
|
+
if (value !== true) throw new AssertionError(notEqualMessage(value, true), { ssf: assertron.true })
|
|
56
|
+
},
|
|
57
|
+
truthy,
|
|
58
|
+
uuid(value: unknown) {
|
|
59
|
+
if (typeof value !== 'string')
|
|
60
|
+
throw new AssertionError(`Expected ${value} to be a string`, { ssf: assertron.uuid })
|
|
61
|
+
|
|
62
|
+
if (/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/.test(value))
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
throw new AssertionError(`Expected ${value} to be a valid UUID`, { ssf: assertron.uuid })
|
|
66
|
+
}
|
|
29
67
|
}
|
package/ts/assertron/falsy.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AssertionError } from '../errors.js'
|
|
2
2
|
|
|
3
|
-
export function falsy(value:
|
|
4
|
-
|
|
5
|
-
throw new AssertionError(`Expected value to be falsy, but received ${value}`, { ssf: falsy })
|
|
3
|
+
export function falsy(value: unknown): void {
|
|
4
|
+
if (value) throw new AssertionError(`Expected value to be falsy, but received ${value}`, { ssf: falsy })
|
|
6
5
|
}
|
package/ts/assertron/index.ts
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AnyConstructor } from 'type-plus'
|
|
2
|
+
import { AssertionError } from '../errors.js'
|
|
3
|
+
|
|
4
|
+
export function isInstanceof<T extends AnyConstructor>(
|
|
5
|
+
value: unknown,
|
|
6
|
+
type: T
|
|
7
|
+
): asserts value is InstanceType<T> {
|
|
8
|
+
if (value instanceof type) return
|
|
9
|
+
|
|
10
|
+
throw new AssertionError(`Expected ${value} to be an instance of ${type.name}`, { ssf: isInstanceof })
|
|
11
|
+
}
|
|
@@ -2,7 +2,7 @@ import { pathEqual as isPathEqual } from 'path-equal'
|
|
|
2
2
|
import { AssertionError } from '../errors.js'
|
|
3
3
|
import { notEqualMessage } from '../utils/index.js'
|
|
4
4
|
|
|
5
|
-
export function pathEqual(actual: string, expected: string) {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
export function pathEqual(actual: string, expected: string): void {
|
|
6
|
+
if (!isPathEqual(actual, expected))
|
|
7
|
+
throw new AssertionError(notEqualMessage(actual, expected), { ssf: pathEqual })
|
|
8
8
|
}
|
package/ts/assertron/rejects.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { AssertionError } from '../errors.js'
|
|
2
2
|
import { notRejectedMessage } from '../utils/index.js'
|
|
3
3
|
|
|
4
|
-
export function rejects<R = any>(promise: Promise<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export function rejects<R = any>(promise: Promise<unknown>): Promise<R> {
|
|
5
|
+
return promise.then(
|
|
6
|
+
value => {
|
|
7
|
+
throw new AssertionError(notRejectedMessage(value), { ssf: rejects })
|
|
8
|
+
},
|
|
9
|
+
err => err
|
|
10
|
+
)
|
|
9
11
|
}
|
package/ts/assertron/repeat.ts
CHANGED
|
@@ -5,28 +5,34 @@ import { FailOnOccurrence } from '../errors.js'
|
|
|
5
5
|
* Repeat the specified function n times and return the last result.
|
|
6
6
|
* If the result is a promise, it will run the function sequentially.
|
|
7
7
|
*/
|
|
8
|
-
export function repeat<R>(
|
|
9
|
-
|
|
8
|
+
export function repeat<R>(
|
|
9
|
+
fn: () => R | (() => Promise<R>),
|
|
10
|
+
times: number
|
|
11
|
+
): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R {
|
|
12
|
+
return repeatRecur(fn, 1, times)
|
|
10
13
|
}
|
|
11
14
|
|
|
12
|
-
function repeatRecur<R>(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
function repeatRecur<R>(
|
|
16
|
+
fn: () => R | (() => Promise<R>),
|
|
17
|
+
i: number,
|
|
18
|
+
times: number
|
|
19
|
+
): ReturnType<typeof fn> extends Promise<R> ? Promise<R> : R {
|
|
20
|
+
try {
|
|
21
|
+
const result = fn()
|
|
22
|
+
if (i === times) return result as any
|
|
16
23
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
if (isPromise(result)) {
|
|
25
|
+
return result.then(
|
|
26
|
+
() => repeatRecur(fn, i + 1, times),
|
|
27
|
+
e => {
|
|
28
|
+
throw new FailOnOccurrence(i, e, { ssf: repeat })
|
|
29
|
+
}
|
|
30
|
+
) as any
|
|
31
|
+
} else {
|
|
32
|
+
return repeatRecur(fn, i + 1, times)
|
|
33
|
+
}
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e instanceof FailOnOccurrence) throw e
|
|
36
|
+
throw new FailOnOccurrence(i, e, { ssf: repeat })
|
|
37
|
+
}
|
|
31
38
|
}
|
|
32
|
-
|
package/ts/assertron/resolves.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { AssertionError } from '../errors.js'
|
|
2
2
|
import { notResolvedMessage } from '../utils/index.js'
|
|
3
3
|
|
|
4
|
-
export function resolves(promise: Promise<any>) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export function resolves(promise: Promise<any>): Promise<void> {
|
|
5
|
+
return promise.then(
|
|
6
|
+
v => v,
|
|
7
|
+
error => {
|
|
8
|
+
throw new AssertionError(notResolvedMessage(error), { ssf: resolves })
|
|
9
|
+
}
|
|
10
|
+
)
|
|
9
11
|
}
|
|
@@ -1,31 +1,39 @@
|
|
|
1
1
|
import { createSatisfier } from 'satisfier'
|
|
2
|
-
import { If, IsExtend, NonComposableTypes } from 'type-plus'
|
|
2
|
+
import type { If, IsExtend, NonComposableTypes } from 'type-plus'
|
|
3
3
|
import { AssertionError } from '../errors.js'
|
|
4
4
|
import { notSatisfiedMessage } from '../utils/index.js'
|
|
5
5
|
|
|
6
|
-
export type SatisfyExpectation<T> =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
6
|
+
export type SatisfyExpectation<T> =
|
|
7
|
+
| If<
|
|
8
|
+
IsExtend<T, string>,
|
|
9
|
+
T | RegExp,
|
|
10
|
+
If<
|
|
11
|
+
IsExtend<T, NonComposableTypes>,
|
|
12
|
+
T,
|
|
13
|
+
If<
|
|
14
|
+
IsExtend<T, Array<any>>,
|
|
15
|
+
T extends Array<infer E> ? Array<SatisfyExpectation<E>> : never,
|
|
16
|
+
{
|
|
17
|
+
[P in keyof T]?: T[P] extends string
|
|
18
|
+
? SatisfyExpectation<T[P]> | RegExp
|
|
19
|
+
: SatisfyExpectation<T[P]>
|
|
20
|
+
}
|
|
21
|
+
>
|
|
22
|
+
>
|
|
23
|
+
>
|
|
24
|
+
| ((v: T) => boolean)
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* Check if `actual` satisfies criteria in `expected`.
|
|
24
|
-
* @param expected All properties can be a value which will be compared to the same property in `actual`,
|
|
28
|
+
* @param expected All properties can be a value which will be compared to the same property in `actual`,
|
|
29
|
+
* RegExp, or a predicate function that will be used to check against the property.
|
|
25
30
|
*/
|
|
26
|
-
export function satisfies<Actual, Expected extends Actual>(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
export function satisfies<Actual, Expected extends Actual>(
|
|
32
|
+
actual: Actual,
|
|
33
|
+
expected: SatisfyExpectation<Expected>
|
|
34
|
+
): void {
|
|
35
|
+
const diff = createSatisfier(expected as any).exec(actual)
|
|
36
|
+
if (diff) {
|
|
37
|
+
throw new AssertionError(notSatisfiedMessage(diff), { ssf: satisfies })
|
|
38
|
+
}
|
|
31
39
|
}
|