deno.land / std@0.166.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// deno-lint-ignore-file ban-typesimport { AssertionError, AssertionErrorConstructorOptions,} 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_INVALID_RETURN_VALUE, ERR_MISSING_ARGS,} from "./internal/errors.ts";import { isDeepEqual } from "./internal/util/comparisons.ts";
function innerFail(obj: { actual?: unknown; expected?: unknown; message?: string | Error; operator?: string;}) { if (obj.message instanceof Error) { throw obj.message; }
throw new AssertionError({ actual: obj.actual, expected: obj.expected, message: obj.message, operator: obj.operator, });}
interface ExtendedAssertionErrorConstructorOptions extends AssertionErrorConstructorOptions { generatedMessage?: boolean;}
// TODO(uki00a): This function is a workaround for setting the `generatedMessage` property flexibly.function createAssertionError( options: ExtendedAssertionErrorConstructorOptions,): AssertionError { const error = new AssertionError(options); if (options.generatedMessage) { error.generatedMessage = true; } return error;}
/** 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,) { // 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 ( validateThrownError(e, error, message, { operator: throws, }) ) { return; } } 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,) { // 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 try { fn(); } catch (e) { gotUnwantedException(e, expected, message, doesNotThrow); } return;}
function equal( actual: unknown, expected: unknown, message?: string | Error,) { 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,) { 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,) { 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( actual: unknown, expected: unknown, message?: string | Error,) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
if (!isDeepEqual(actual, expected)) { innerFail({ actual, expected, message, operator: "deepEqual" }); }}function notDeepEqual( actual: unknown, expected: unknown, message?: string | Error,) { if (arguments.length < 2) { throw new ERR_MISSING_ARGS("actual", "expected"); }
if (isDeepEqual(actual, expected)) { innerFail({ actual, expected, message, operator: "notDeepEqual" }); }}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(message?: string | Error): never { if (typeof message === "string" || message == null) { throw createAssertionError({ message: message ?? "Failed", operator: "fail", generatedMessage: message == null, }); } else { throw message; }}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);}
function rejects( // deno-lint-ignore no-explicit-any asyncFn: Promise<any> | (() => Promise<any>), error?: RegExp | Function | Error,): Promise<void>;
function rejects( // deno-lint-ignore no-explicit-any asyncFn: Promise<any> | (() => Promise<any>), message?: string,): Promise<void>;
// Intentionally avoid using async/await because test-assert-async.js requires itfunction rejects( // deno-lint-ignore no-explicit-any asyncFn: Promise<any> | (() => Promise<any>), error?: RegExp | Function | Error | string, message?: string,) { let promise: Promise<void>; if (typeof asyncFn === "function") { try { promise = asyncFn(); } catch (err) { // If `asyncFn` throws an error synchronously, this function returns a rejected promise. return Promise.reject(err); }
if (!isValidThenable(promise)) { return Promise.reject( new ERR_INVALID_RETURN_VALUE( "instance of Promise", "promiseFn", promise, ), ); } } else if (!isValidThenable(asyncFn)) { return Promise.reject( new ERR_INVALID_ARG_TYPE("promiseFn", ["function", "Promise"], asyncFn), ); } else { promise = asyncFn; }
function onFulfilled() { let message = "Missing expected rejection"; if (typeof error === "string") { message += `: ${error}`; } else if (typeof error === "function" && error.prototype !== undefined) { message += ` (${error.name}).`; } else { message += "."; } return Promise.reject(createAssertionError({ message, operator: "rejects", generatedMessage: true, })); }
// deno-lint-ignore camelcase function rejects_onRejected(e: Error) { // TODO(uki00a): In order to `test-assert-async.js` pass, intentionally adds `rejects_` as a prefix. if ( validateThrownError(e, error, message, { operator: rejects, validationFunctionName: "validate", }) ) { return; } }
return promise.then(onFulfilled, rejects_onRejected);}
function doesNotReject( // deno-lint-ignore no-explicit-any asyncFn: Promise<any> | (() => Promise<any>), error?: RegExp | Function,): Promise<void>;
function doesNotReject( // deno-lint-ignore no-explicit-any asyncFn: Promise<any> | (() => Promise<any>), message?: string,): Promise<void>;
// Intentionally avoid using async/await because test-assert-async.js requires itfunction doesNotReject( // deno-lint-ignore no-explicit-any asyncFn: Promise<any> | (() => Promise<any>), error?: RegExp | Function | string, message?: string,) { // deno-lint-ignore no-explicit-any let promise: Promise<any>; if (typeof asyncFn === "function") { try { const value = asyncFn(); if (!isValidThenable(value)) { return Promise.reject( new ERR_INVALID_RETURN_VALUE( "instance of Promise", "promiseFn", value, ), ); } promise = value; } catch (e) { // If `asyncFn` throws an error synchronously, this function returns a rejected promise. return Promise.reject(e); } } else if (!isValidThenable(asyncFn)) { return Promise.reject( new ERR_INVALID_ARG_TYPE("promiseFn", ["function", "Promise"], asyncFn), ); } else { promise = asyncFn; }
return promise.then( () => {}, (e) => gotUnwantedException(e, error, message, doesNotReject), );}
function gotUnwantedException( // deno-lint-ignore no-explicit-any e: any, expected: RegExp | Function | string | null | undefined, message: string | Error | null | undefined, operator: Function,): never { if (typeof expected === "string") { // The use case of doesNotThrow(fn, message); throw new AssertionError({ message: `Got unwanted exception: ${expected}\nActual message: "${e.message}"`, operator: operator.name, }); } else if ( typeof expected === "function" && expected.prototype !== undefined ) { // The use case of doesNotThrow(fn, Error, message); if (e instanceof expected) { let msg = `Got unwanted exception: ${e.constructor?.name}`; if (message) { msg += ` ${String(message)}`; } throw new AssertionError({ message: msg, operator: operator.name, }); } else if (expected.prototype instanceof Error) { throw e; } else { const result = expected(e); if (result === true) { let msg = `Got unwanted rejection.\nActual message: "${e.message}"`; if (message) { msg += ` ${String(message)}`; } throw new AssertionError({ message: msg, operator: operator.name, }); } } throw e; } else { if (message) { throw new AssertionError({ message: `Got unwanted exception: ${message}\nActual message: "${ e ? e.message : String(e) }"`, operator: operator.name, }); } throw new AssertionError({ message: `Got unwanted exception.\nActual message: "${ e ? e.message : String(e) }"`, operator: operator.name, }); }}
/** * Throws `value` if the value is not `null` or `undefined`. * * @param err */// deno-lint-ignore no-explicit-anyfunction ifError(err: any) { if (err !== null && err !== undefined) { let message = "ifError got unwanted exception: ";
if (typeof err === "object" && typeof err.message === "string") { if (err.message.length === 0 && err.constructor) { message += err.constructor.name; } else { message += err.message; } } else { message += inspect(err); }
const newErr = new AssertionError({ actual: err, expected: null, operator: "ifError", message, stackStartFn: ifError, });
// Make sure we actually have a stack trace! const origStack = err.stack;
if (typeof origStack === "string") { // This will remove any duplicated frames from the error frames taken // from within `ifError` and add the original error frames to the newly // created ones. const tmp2 = origStack.split("\n"); tmp2.shift();
// Filter all frames existing in err.stack. let tmp1 = newErr!.stack?.split("\n");
for (const errFrame of tmp2) { // Find the first occurrence of the frame. const pos = tmp1?.indexOf(errFrame);
if (pos !== -1) { // Only keep new frames. tmp1 = tmp1?.slice(0, pos);
break; } }
newErr.stack = `${tmp1?.join("\n")}\n${tmp2.join("\n")}`; }
throw newErr; }}
interface ValidateThrownErrorOptions { operator: Function; validationFunctionName?: string;}
function validateThrownError( // deno-lint-ignore no-explicit-any e: any, error: RegExp | Function | Error | string | null | undefined, message: string | undefined | null, options: ValidateThrownErrorOptions,): boolean { if (typeof error === "string") { if (message != null) { 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.prototype instanceof Error ) { // error is a constructor if (e instanceof error) { return true; } throw createAssertionError({ 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: options.operator.name, generatedMessage: true, }); } if (error instanceof Function) { const received = error(e); if (received === true) { return true; } throw createAssertionError({ message: `The ${ options.validationFunctionName ? `"${options.validationFunctionName}" validation` : "validation" } function is expected to return "true". Received ${ inspect(received) }\n\nCaught error:\n\n${e}`, actual: e, expected: error, operator: options.operator.name, generatedMessage: true, }); } if (error instanceof RegExp) { if (error.test(String(e))) { return true; } throw createAssertionError({ message: `The input did not match the regular expression ${error.toString()}. Input:\n\n'${ String(e) }'\n`, actual: e, expected: error, operator: options.operator.name, generatedMessage: true, }); } 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 createAssertionError({ message: message || "object is expected to thrown, but got null", actual: e, expected: error, operator: options.operator.name, generatedMessage: message == null, }); }
if (typeof e === "string") { throw createAssertionError({ message: message || `object is expected to thrown, but got string: ${e}`, actual: e, expected: error, operator: options.operator.name, generatedMessage: message == null, }); } if (typeof e === "number") { throw createAssertionError({ message: message || `object is expected to thrown, but got number: ${e}`, actual: e, expected: error, operator: options.operator.name, generatedMessage: message == null, }); } if (!(k in e)) { throw createAssertionError({ message: message || `A key in the expected object is missing: ${k}`, actual: e, expected: error, operator: options.operator.name, generatedMessage: message == null, }); } 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 true; } if (typeof error === "undefined") { return true; } throw createAssertionError({ message: `Invalid expectation: ${error}`, operator: options.operator.name, generatedMessage: true, });}
// deno-lint-ignore no-explicit-anyfunction isValidThenable(maybeThennable: any): boolean { if (!maybeThennable) { return false; }
if (maybeThennable instanceof Promise) { return true; }
const isThenable = typeof maybeThennable.then === "function" && typeof maybeThennable.catch === "function";
return isThenable && typeof maybeThennable !== "function";}
Object.assign(strict, { AssertionError, deepEqual: deepStrictEqual, deepStrictEqual, doesNotMatch, doesNotReject, doesNotThrow, equal: strictEqual, fail, ifError, match, notDeepEqual: notDeepStrictEqual, notDeepStrictEqual, notEqual: notStrictEqual, notStrictEqual, ok, rejects, strict, strictEqual, throws,});
export default Object.assign(assert, { AssertionError, deepEqual, deepStrictEqual, doesNotMatch, doesNotReject, doesNotThrow, equal, fail, ifError, match, notDeepEqual, notDeepStrictEqual, notEqual, notStrictEqual, ok, rejects, strict, strictEqual, throws,});
export { AssertionError, deepEqual, deepStrictEqual, doesNotMatch, doesNotReject, doesNotThrow, equal, fail, ifError, match, notDeepEqual, notDeepStrictEqual, notEqual, notStrictEqual, ok, rejects, strict, strictEqual, throws,};
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago