All files / expect / _asymmetric_matchers.ts

86.49% Branches 32/37
92.21% Lines 142/154
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
 
 
 
x106
x106
 
x106
x106
x321
x321
x321
 
x106
 
x106
x106
x114
x114
x106
 
x106
x114
x114
 
x106
x106
 
 
 
x124
x124
 
x106
x124
x127
x124
x139
x144
x144
 
x151
x153
x153
 
 
 
 
 
x151
x153
x153
 
x151
x153
x153
 
x151
x153
x153
 
x153
x153
x153
x139
 
 
x106
 
x106
x124
x124
 
x106
x106
x123
x123
 
x106
x123
x123
x123
x475
 
 
x123
x123
x106
 
x106
x119
x119
 
x106
x110
x110
 
x106
x106
 
x106
x115
x115
x115
 
x106
 
 
 
 
x115
x115
x115
x115
x115
x115
x117
x117
 
x122
x115
x106
 
x106
x115
x115
 
x106
x106
x127
x127
 
x106
x127
x127
x127
x106
 
x106
x118
x118
 
x106
x115
x115
 
x106
x106
x124
x124
 
x106
 
x124
x124
x106
 
x106
x118
x118
 
x106
x112
x112
 
x106
x106
x106
x124
x124
 
x106
x126
x126
 
x126
x144
x144
x144
x144
x153
x153
x144
 
x126
x126
x106
 
x106
x106
 
x121
x121
 
x106
x106
 
x109
x109

























I

















I


















I









































I













































I













































// Copyright 2018-2025 the Deno authors. MIT license.
// deno-lint-ignore-file no-explicit-any

import { getCustomEqualityTesters } from "./_custom_equality_tester.ts";
import { equal } from "./_equal.ts";

export abstract class AsymmetricMatcher<T> {
  constructor(
    protected value: T,
    protected inverse: boolean = false,
  ) {}
  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[], inverse = false) {
    super(arr, inverse);
  }

  equals(other: any[]): boolean {
    const res = Array.isArray(other) &&
      this.value.every((e) =>
        other.some((another) =>
          equal(e, another, { customTesters: getCustomEqualityTesters() })
        )
      );
    return this.inverse ? !res : res;
  }
}

export function arrayContaining(c: any[]): ArrayContaining {
  return new ArrayContaining(c);
}

export function arrayNotContaining(c: any[]): ArrayContaining {
  return new ArrayContaining(c, true);
}

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, inverse = false) {
    super(str, inverse);
  }

  equals(other: string): boolean {
    const res = typeof other !== "string" ? false : other.includes(this.value);
    return this.inverse ? !res : res;
  }
}

export function stringContaining(str: string): StringContaining {
  return new StringContaining(str);
}

export function stringNotContaining(str: string): StringContaining {
  return new StringContaining(str, true);
}

export class StringMatching extends AsymmetricMatcher<RegExp> {
  constructor(pattern: string | RegExp, inverse = false) {
    super(new RegExp(pattern), inverse);
  }

  equals(other: string): boolean {
    const res = typeof other !== "string" ? false : this.value.test(other);
    return this.inverse ? !res : res;
  }
}

export function stringMatching(pattern: string | RegExp): StringMatching {
  return new StringMatching(pattern);
}

export function stringNotMatching(pattern: string | RegExp): StringMatching {
  return new StringMatching(pattern, true);
}

export class ObjectContaining
  extends AsymmetricMatcher<Record<string, unknown>> {
  constructor(obj: Record<string, unknown>, inverse = false) {
    super(obj, inverse);
  }

  equals(other: Record<string, unknown>): boolean {
    const keys = Object.keys(this.value);
    let res = true;

    for (const key of keys) {
      if (
        !Object.hasOwn(other, key) ||
        !equal(this.value[key], other[key])
      ) {
        res = false;
      }
    }

    return this.inverse ? !res : res;
  }
}

export function objectContaining(
  obj: Record<string, unknown>,
): ObjectContaining {
  return new ObjectContaining(obj);
}

export function objectNotContaining(
  obj: Record<string, unknown>,
): ObjectContaining {
  return new ObjectContaining(obj, true);
}