deno.land / std@0.224.0 / expect / _asymmetric_matchers.ts

_asymmetric_matchers.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// deno-lint-ignore-file no-explicit-any
export abstract class AsymmetricMatcher<T> { constructor( protected value: T, ) {} abstract equals(other: unknown): boolean;}
export class Anything extends AsymmetricMatcher<void> { equals(other: unknown): boolean { return other !== null && other !== undefined; }}
export function anything(): Anything { return new Anything();}
export class Any extends AsymmetricMatcher<any> { constructor(value: unknown) { if (value === undefined) { throw new TypeError("Expected a constructor function"); } super(value); }
equals(other: unknown): boolean { if (typeof other === "object") { return other instanceof this.value; } else { if (this.value === Number) { return typeof other === "number"; }
if (this.value === String) { return typeof other === "string"; }
if (this.value === Number) { return typeof other === "number"; }
if (this.value === Function) { return typeof other === "function"; }
if (this.value === Boolean) { return typeof other === "boolean"; }
if (this.value === BigInt) { return typeof other === "bigint"; }
if (this.value === Symbol) { return typeof other === "symbol"; } } return false; }}
export function any(c: unknown): Any { return new Any(c);}
export class ArrayContaining extends AsymmetricMatcher<any[]> { constructor(arr: any[]) { super(arr); }
equals(other: any[]): boolean { return Array.isArray(other) && this.value.every((e) => other.includes(e)); }}
export function arrayContaining(c: any[]): ArrayContaining { return new ArrayContaining(c);}
export class CloseTo extends AsymmetricMatcher<number> { readonly #precision: number;
constructor(num: number, precision: number = 2) { super(num); this.#precision = precision; }
equals(other: number): boolean { if (typeof other !== "number") { return false; }
if ( (this.value === Number.POSITIVE_INFINITY && other === Number.POSITIVE_INFINITY) || (this.value === Number.NEGATIVE_INFINITY && other === Number.NEGATIVE_INFINITY) ) { return true; }
return Math.abs(this.value - other) < Math.pow(10, -this.#precision) / 2; }}
export function closeTo(num: number, numDigits?: number): CloseTo { return new CloseTo(num, numDigits);}
export class StringContaining extends AsymmetricMatcher<string> { constructor(str: string) { super(str); }
equals(other: string): boolean { if (typeof other !== "string") { return false; }
return other.includes(this.value); }}
export function stringContaining(str: string): StringContaining { return new StringContaining(str);}
export class StringMatching extends AsymmetricMatcher<RegExp> { constructor(pattern: string | RegExp) { super(new RegExp(pattern)); }
equals(other: string): boolean { if (typeof other !== "string") { return false; }
return this.value.test(other); }}
export function stringMatching(pattern: string | RegExp): StringMatching { return new StringMatching(pattern);}
std

Version Info

Tagged at
6 months ago