All files / text / _test_util.ts

50.00% Branches 1/2
96.15% Lines 25/26
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
 
x3
x3
 
x3
x15
x5
x5
x5
 
x3
x36
x9
x21
x21
 
x21
x21
x9
x9
x9
 
 
x9
x9
x15
x33
x33
x15
x9
x9














I














// Copyright 2018-2025 the Deno authors. MIT license.
import { stub } from "@std/testing/mock";
import { stubProperty } from "@std/testing/unstable-stub-property";

export function generateRandomString(min: number, max: number): string {
  return Array.from({ length: Math.floor(Math.random() * (max - min) + min) })
    .map(() => String.fromCharCode(Math.floor(Math.random() * 26) + 97))
    .join("");
}

export function stubIntlFunctions(defaultLocale: string) {
  const fnNames = ["toLocaleLowerCase", "toLocaleUpperCase"] as const;
  const stubs: { [Symbol.dispose](): void }[] = fnNames.map((fnName) => {
    const fn = String.prototype[fnName];
    const stubbed: typeof fn = function (this: string, locale) {
      return fn.call(this, locale ?? defaultLocale);
    };
    return stub(String.prototype, fnName, stubbed);
  });
  stubs.push(
    stubProperty(navigator, "language", defaultLocale),
  );

  return {
    [Symbol.dispose]() {
      for (const stub of stubs) {
        stub[Symbol.dispose]();
      }
    },
  };
}