-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathassertions.ts
More file actions
226 lines (207 loc) · 5.54 KB
/
Copy pathassertions.ts
File metadata and controls
226 lines (207 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { printable, throwInternalError } from "@ark/util"
import type { type } from "arktype"
import * as assert from "node:assert/strict"
import type { TypeRelationshipAssertionData } from "../cache/writeAssertionCache.ts"
import type { AssertionContext } from "./attest.ts"
export type ThrowAssertionErrorContext = {
message: string
expected?: unknown
actual?: unknown
stack: string
}
export const throwAssertionError = ({
stack,
...errorArgs
}: ThrowAssertionErrorContext): never => {
const e = new assert.AssertionError(errorArgs)
e.stack = stack
throw e
}
export class MissingSnapshotError extends Error {}
export type AssertFn = (
expected: unknown,
actual: unknown,
ctx: AssertionContext
) => void
export type MappedTypeAssertionResult = {
actual: unknown
expected?: unknown
} | null
export type TypeAssertionMapper = (
data: TypeRelationshipAssertionData,
ctx: AssertionContext
) => MappedTypeAssertionResult
export class TypeAssertionMapping {
fn: TypeAssertionMapper
constructor(fn: TypeAssertionMapper) {
this.fn = fn
}
}
export const versionableAssertion =
(fn: AssertFn): AssertFn =>
(expected, actual, ctx) => {
if (actual instanceof TypeAssertionMapping) {
if (!ctx.typeRelationshipAssertionEntries) {
throwInternalError(
`Unexpected missing typeAssertionEntries when passed a TypeAssertionMapper`
)
}
for (const [version, data] of ctx.typeRelationshipAssertionEntries) {
let errorMessage = ""
try {
const mapped = actual.fn(data, ctx)
if (mapped !== null) {
fn(
"expected" in mapped ? mapped.expected : expected,
mapped.actual,
ctx
)
}
} catch (e) {
errorMessage += `❌TypeScript@${version}:${e}\n`
}
if (errorMessage) {
throwAssertionError({
stack: ctx.assertionStack,
message: errorMessage
})
}
}
} else fn(expected, actual, ctx)
}
const unversionedAssertEquals: AssertFn = (expected, actual, ctx) => {
if (expected === actual) return
try {
if (
typeof expected === "object" &&
expected !== null &&
typeof actual === "object" &&
actual !== null
) {
if (expected.constructor === actual.constructor)
assert.deepStrictEqual(actual, expected)
else {
const serializedExpected = printable(expected)
const serializedActual = printable(actual)
throw new assert.AssertionError({
message: `Objects did not have the same constructor:
Expected: ${serializedExpected}
Actual: ${serializedActual}`,
expected: serializedExpected,
actual: serializedActual
})
}
} else if (
typeof expected === "object" ||
typeof expected === "function" ||
typeof actual === "function" ||
typeof actual === "function"
) {
const serializedExpected = printable(expected)
const serializedActual = printable(actual)
throw new assert.AssertionError({
message: `Assertion including at least one function or object was not between reference equal items
Expected: ${serializedExpected}
Actual: ${serializedActual}`,
expected: serializedExpected,
actual: serializedActual
})
// guaranteed to be two primitives at this point
} else assert.equal(actual, expected)
} catch (e: any) {
// some nonsense to get a good stack trace
e.stack = ctx.assertionStack
throw e
}
}
export const assertEquals: AssertFn = versionableAssertion(
unversionedAssertEquals
)
const unversionedAssertSatisfies = (
t: type.Any,
data: unknown,
ctx: AssertionContext
) => {
try {
t.assert(data)
} catch (e: any) {
e.stack = ctx.assertionStack
throw e
}
}
export const assertSatisfies = versionableAssertion(
unversionedAssertSatisfies as never
)
export const typeEqualityMapping: TypeAssertionMapping =
new TypeAssertionMapping(data => {
const expected = data.typeArgs[0]
const actual = data.typeArgs[1] ?? data.args[0]
if (!expected || !actual)
throwInternalError(`Unexpected type data ${printable(data)}`)
if (actual.relationships.typeArgs[0] !== "equality") {
return {
expected: expected.type,
actual:
expected.type === actual.type ?
"(serializes to same value)"
: actual.type
}
}
return null
})
export const assertEqualOrMatching: AssertFn = versionableAssertion(
(expected, actual, ctx) => {
const assertionArgs = { actual, expected, stack: ctx.assertionStack }
if (typeof actual !== "string") {
throwAssertionError({
message: `Value was of type ${typeof actual} (expected a string).`,
...assertionArgs
})
} else if (typeof expected === "string") {
if (!actual.includes(expected)) {
throwAssertionError({
message: `Expected string '${expected}' did not appear in actual string '${actual}'.`,
...assertionArgs
})
}
} else if (expected instanceof RegExp) {
if (!expected.test(actual)) {
throwAssertionError({
message: `Actual string '${actual}' did not match regex '${expected.source}'.`,
...assertionArgs
})
}
} else {
throw new Error(
`Expected value for this assertion should be a string or RegExp.`
)
}
}
)
export type AssertedFnCallResult = {
returned?: unknown
threw?: string
}
export const getThrownMessage = (
result: AssertedFnCallResult,
ctx: AssertionContext
): string | undefined => {
if (!("threw" in result)) {
throwAssertionError({
message: "Function didn't throw",
stack: ctx.assertionStack
})
}
return result.threw
}
export const callAssertedFunction = (
asserted: Function
): AssertedFnCallResult => {
const result: AssertedFnCallResult = {}
try {
result.returned = asserted()
} catch (error) {
result.threw = String(error)
}
return result
}