All files / expect / _asymmetric_matchers.ts

91.94% Branches 57/62
100.00% Functions 27/27
92.86% Lines 143/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
 
 
 
x108
x108
 
x108
x108
x113
x113
x113
 
x108
 
x108
x108
x8
x8
x108
 
x108
x8
x8
 
x108
x108
 
 
 
x22
x22
 
x108
x20
x3
x20
x17
x7
x7
 
x12
x2
x2
 
 
 
 
 
x12
x2
x2
 
x12
x2
x2
 
x12
x2
x2
 
x2
x2
x2
x17
 
 
x108
 
x108
x22
x22
 
x108
x108
x17
x17
 
x108
x17
x17
x17
x77
 
 
x17
x17
x108
 
x108
x13
x13
 
x108
x4
x4
 
x108
x108
 
x108
x9
x9
x9
 
x108
 
 
 
 
x9
x9
x9
x9
x9
x9
x2
x2
 
x7
x9
x108
 
x108
x9
x9
 
x108
x108
x21
x21
 
x108
x21
x21
x21
x108
 
x108
x12
x12
 
x108
x9
x9
 
x108
x108
x18
x18
 
x108
x18
x18
x18
x108
 
x108
x12
x12
 
x108
x6
x6
 
x108
x108
x108
x18
x18
 
x108
x20
x20
 
x20
x18
x18
x18
x18
x9
x9
x18
 
x20
x20
x108
 
x108
x108
 
x15
x15
 
x108
x108
 
x3
x3

























I

















I


















I









































I













































I













































// Copyright 2018-2026 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);
}