2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { equal } from "./equal.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { format } from "@std/internal/format";
|
2023-07-13 07:04:30 +00:00
|
|
|
import { AssertionError } from "./assertion_error.ts";
|
|
|
|
|
2023-12-08 08:04:06 +00:00
|
|
|
/** An array-like object (`Array`, `Uint8Array`, `NodeList`, etc.) that is not a string */
|
2023-12-09 02:56:09 +00:00
|
|
|
export type ArrayLikeArg<T> = ArrayLike<T> & object;
|
2023-12-08 08:04:06 +00:00
|
|
|
|
2023-07-13 07:04:30 +00:00
|
|
|
/**
|
2023-12-06 17:13:38 +00:00
|
|
|
* Make an assertion that `actual` includes the `expected` values. If not then
|
|
|
|
* an error will be thrown.
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* Type parameter can be specified to ensure values under comparison have the
|
|
|
|
* same type.
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2024-05-30 02:38:16 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertArrayIncludes } from "@std/assert";
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* assertArrayIncludes([1, 2], [2]); // Doesn't throw
|
|
|
|
* assertArrayIncludes([1, 2], [3]); // Throws
|
2023-07-13 07:04:30 +00:00
|
|
|
* ```
|
2024-05-30 02:38:16 +00:00
|
|
|
*
|
|
|
|
* @typeParam T The type of the elements in the array to compare.
|
|
|
|
* @param actual The array-like object to check for.
|
|
|
|
* @param expected The array-like object to check for.
|
|
|
|
* @param msg The optional message to display if the assertion fails.
|
2023-07-13 07:04:30 +00:00
|
|
|
*/
|
|
|
|
export function assertArrayIncludes<T>(
|
2023-12-08 08:04:06 +00:00
|
|
|
actual: ArrayLikeArg<T>,
|
|
|
|
expected: ArrayLikeArg<T>,
|
2023-07-13 07:04:30 +00:00
|
|
|
msg?: string,
|
2024-01-08 02:36:07 +00:00
|
|
|
) {
|
2023-07-13 07:04:30 +00:00
|
|
|
const missing: unknown[] = [];
|
|
|
|
for (let i = 0; i < expected.length; i++) {
|
|
|
|
let found = false;
|
|
|
|
for (let j = 0; j < actual.length; j++) {
|
|
|
|
if (equal(expected[i], actual[j])) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
missing.push(expected[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (missing.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
|
|
msg = `Expected actual: "${format(actual)}" to include: "${
|
|
|
|
format(expected)
|
|
|
|
}"${msgSuffix}\nmissing: ${format(missing)}`;
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|