deno.land / std@0.91.0 / node / assert.ts

نووسراو ببینە
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.// deno-lint-ignore-file ban-typesimport { AssertionError } from "./assertion_error.ts";import * as asserts from "../testing/asserts.ts";import { inspect } from "./util.ts";import { ERR_AMBIGUOUS_ARGUMENT, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_MISSING_ARGS,} from "./_errors.ts";
/** Converts the std assertion error to node.js assertion error */function toNode( fn: () => void, opts?: { actual: unknown; expected: unknown; message?: string | Error; operator?: string; },) { const { operator, message, actual, expected } = opts || {}; try { fn(); } catch (e) { if (e instanceof asserts.AssertionError) { if (typeof message === "string") { throw new AssertionError({ operator, message, actual, expected, }); } else if (message instanceof Error) { throw message; } else { throw new AssertionError({ operator, message: e.message, actual, expected, }); } } throw e; }}
function assert(actual: unknown, message?: string | Error): asserts actual { if (arguments.length === 0) { throw new AssertionError({ message: "No value argument passed to `assert.ok()`", }); } toNode( () => asserts.assert(actual), { message, actual, expected: true }, );}const ok = assert;
function throws( fn: () => void, error?: RegExp | Function | Error, message?: string,): void { // Check arg types if (typeof fn !== "function") { throw new ERR_INVALID_ARG_TYPE("fn", "function", fn); } if ( typeof error === "object" && error !== null && Object.getPrototypeOf(error) === Object.prototype && Object.keys(error).length === 0 ) { // error is an empty object throw new ERR_INVALID_ARG_VALUE( "error", error, "may not be an empty object", ); } if (typeof message === "string") { if ( !(error instanceof RegExp) && typeof error !== "function" && !(error instanceof Error) && typeof error !== "object" ) { throw new ERR_INVALID_ARG_TYPE("error", [ "Function", "Error", "RegExp", "Object", ], error); } } else { if ( typeof error !== "undefined" && typeof error !== "string" && !(error instanceof RegExp) && typeof error !== "function" && !(error instanceof Error) && typeof error !== "object" ) { throw new ERR_INVALID_ARG_TYPE("error", [ "Function", "Error", "RegExp", "Object", ], error); } }
// Checks test function try { fn(); } catch (e) { if (typeof error === "string") { if (arguments.length === 3) { throw new ERR_INVALID_ARG_TYPE( "error", ["Object", "Error", "Function", "RegExp"], error, ); } else if (typeof e === "object" && e !== null) { if (e.message === error) { throw new ERR_AMBIGUOUS_ARGUMENT( "error/message", `The error message "${e.message}" is identical to the message.`, ); } } else if (e === error) { throw new ERR_AMBIGUOUS_ARGUMENT( "error/message", `The error "${e}" is identical to the message.`, ); } message = error; error = undefined; } if (error instanceof Function && error.prototype !== undefined) { // error is a constructor if (e instanceof error) { return; } throw new AssertionError({ message: `The error is expected to be an instance of "${error.name}". Received "${e ?.constructor?.name}"\n\nError message:\n\n${e.message}`, actual: e, expected: error, operator: "throws", }); } if (error instanceof Function) { const received = error(e); if (received === true) { return; } throw new AssertionError({ message: `The validation function is expected to return "true": received ${received}`, actual: e, expected: error, operator: "throws", }); } if (error instanceof RegExp) { if (error.test(String(e))) { return; } throw new AssertionError({ message: `The input did not match the regular expression ${error.toString()}. Input:\n\n'${ String(e) }'\n`, actual: e, expected: error, operator: "throws", }); } if (typeof error === "object" && error !== null) { const keys = Object.keys(error); if (error instanceof Error) { keys.push("name", "message"); } for (const k of keys) { if (e == null) { throw new AssertionError({ message: message || "object is expected to thrown, but got null", actual: e, expected: error, operator: "throws", }); } if (typeof e === "string") { throw new AssertionError({ message: message || `object is expected to thrown, but got string: ${e}`, actual: e, expected: error, operator: "throws", }); } if (!(k in e)) { throw new AssertionError({ message: message || `A key in the expected object is missing: ${k}`, actual: e, expected: error, operator: "throws", }); } const actual = e[k]; // deno-lint-ignore no-explicit-any const expected = (error as any)[k]; if (typeof actual === "string" && expected instanceof RegExp) { match(actual, expected); } else { deepStrictEqual(actual, expected); } } return; } if (typeof error === "undefined") { return; } throw new Error(`Invalid expectation: ${error}`); } if (message) { let msg = `Missing expected exception: ${message}`; if (typeof error === "function" && error?.name) { msg = `Missing expected exception (${error.name}): ${message}`; } throw new AssertionError({ message: msg, operator: "throws", actual: undefined, expected: error, }); } else if (typeof error === "string") { // Use case of throws(fn, message) throw new AssertionError({ message: `Missing expected exception: ${error}`, operator: "throws", actual: undefined, expected: undefined, }); } else if (typeof error === "function" && error?.prototype !== undefined) { throw new AssertionError({ message: `Missing expected exception (${error.name}).`, operator: "throws", actual: undefined, expected: error, }); } else { throw new AssertionError({ message: "Missing expected exception.", operator: "throws", actual: undefined, expected: error, }); }}
function doesNotThrow( fn: () => void, message?: string,): void;function doesNotThrow( fn: () => void, error?: Function, message?: string | Error,): void;function doesNotThrow( fn: () => void, error?: RegExp, message?: string,): void;function doesNotThrow( fn: () => void, expected?: Function | RegExp | string, message?: string | Error,): void { // Check arg type if (typeof fn !== "function") { throw new ERR_INVALID_ARG_TYPE("fn", "function", fn); } else if ( !(expected instanceof RegExp) && typeof expected !== "function" && typeof expected !== "string" && typeof expected !== "undefined" ) { throw new ERR_INVALID_ARG_TYPE("expected", ["Function", "RegExp"], fn); }
// Checks test function if (typeof expected === "string") { // The use case of doesNotThrow(fn, message); try { fn(); } catch (e) { throw new AssertionError({ message: `Got unwanted exception: ${expected}\nActual message: "${e.message}"`, operator: "doesNotThrow", }); } return; } else if ( typeof expected === "function" && expected.prototype !== undefined ) { // The use case of doesNotThrow(fn, Error, message); try { fn(); } catch (e) { if (e instanceof expected) { let msg = `Got unwanted exception: ${e.constructor?.name}`; if (message) { msg += ` ${String(message)}`; } throw new AssertionError({ message: msg, operator: "doesNotThrow", }); } throw e; } return; } else { try { fn(); } catch (e) { if (message) { throw new AssertionError({ message: `Got unwanted exception: ${message}\nActual message: "${ e ? e.message : String(e) }"`, operator: "doesNotThrow", }); } throw new AssertionError({ message: `Got unwanted exception.\nActual message: "${ e ? e.message : String(e) }"`, operator: "doesNotThrow", }); } return; }}
function equal( actual: unknown, expected: unknown, message?: string | Error,): void { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
if (actual == expected) { return; }
if (Number.isNaN(actual) && Number.isNaN(expected)) { return; }
if (typeof message === "string") { throw new AssertionError({ message, }); } else if (message instanceof Error) { throw message; }
toNode( () => asserts.assertStrictEquals(actual, expected), { message: message || `${actual} == ${expected}`, operator: "==", actual, expected, }, );}function notEqual( actual: unknown, expected: unknown, message?: string | Error,): void { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
if (Number.isNaN(actual) && Number.isNaN(expected)) { throw new AssertionError({ message: `${actual} != ${expected}`, operator: "!=", actual, expected, }); } if (actual != expected) { return; }
if (typeof message === "string") { throw new AssertionError({ message, }); } else if (message instanceof Error) { throw message; }
toNode( () => asserts.assertNotStrictEquals(actual, expected), { message: message || `${actual} != ${expected}`, operator: "!=", actual, expected, }, );}function strictEqual( actual: unknown, expected: unknown, message?: string | Error,): void { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
toNode( () => asserts.assertStrictEquals(actual, expected), { message, operator: "strictEqual", actual, expected }, );}function notStrictEqual( actual: unknown, expected: unknown, message?: string | Error,) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
toNode( () => asserts.assertNotStrictEquals(actual, expected), { message, actual, expected, operator: "notStrictEqual" }, );}
function deepEqual() { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
// TODO(kt3k): Implement deepEqual throw new Error("Not implemented");}function notDeepEqual() { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
// TODO(kt3k): Implement notDeepEqual throw new Error("Not implemented");}function deepStrictEqual( actual: unknown, expected: unknown, message?: string | Error,) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
toNode( () => asserts.assertEquals(actual, expected), { message, actual, expected, operator: "deepStrictEqual" }, );}function notDeepStrictEqual( actual: unknown, expected: unknown, message?: string | Error,) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
toNode( () => asserts.assertNotEquals(actual, expected), { message, actual, expected, operator: "deepNotStrictEqual" }, );}
function fail() { toNode(() => fail());}function match(actual: string, regexp: RegExp, message?: string | Error) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "regexp"); } if (!(regexp instanceof RegExp)) { throw new ERR_INVALID_ARG_TYPE("regexp", "RegExp", regexp); }
toNode( () => asserts.assertMatch(actual, regexp), { message, actual, expected: regexp, operator: "match" }, );}
function doesNotMatch( string: string, regexp: RegExp, message?: string | Error,) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("string", "regexp"); } if (!(regexp instanceof RegExp)) { throw new ERR_INVALID_ARG_TYPE("regexp", "RegExp", regexp); } if (typeof string !== "string") { if (message instanceof Error) { throw message; } throw new AssertionError({ message: message || `The "string" argument must be of type string. Received type ${typeof string} (${ inspect(string) })`, actual: string, expected: regexp, operator: "doesNotMatch", }); }
toNode( () => asserts.assertNotMatch(string, regexp), { message, actual: string, expected: regexp, operator: "doesNotMatch" }, );}
function strict(actual: unknown, message?: string | Error): asserts actual { if (arguments.length === 0) { throw new AssertionError({ message: "No value argument passed to `assert.ok()`", }); } assert(actual, message);}
Object.assign(strict, { AssertionError, deepEqual: deepStrictEqual, deepStrictEqual, doesNotMatch, doesNotThrow, equal: strictEqual, fail, match, notDeepEqual: notDeepStrictEqual, notDeepStrictEqual, notEqual: notStrictEqual, notStrictEqual, ok, strict, strictEqual, throws,});
export default Object.assign(assert, { AssertionError, deepEqual, deepStrictEqual, doesNotMatch, doesNotThrow, equal, fail, match, notDeepEqual, notDeepStrictEqual, notEqual, notStrictEqual, ok, strict, strictEqual, throws,});
export { AssertionError, deepEqual, deepStrictEqual, doesNotMatch, doesNotThrow, equal, fail, match, notDeepEqual, notDeepStrictEqual, notEqual, notStrictEqual, ok, strict, strictEqual, throws,};
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉