2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-12 15:04:15 +00:00
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2021-09-12 15:04:15 +00:00
|
|
|
import { findSingle } from "./find_single.ts";
|
|
|
|
|
|
|
|
function findSingleTest<I>(
|
|
|
|
input: [Array<I>, (element: I) => boolean],
|
|
|
|
expected: I | undefined,
|
|
|
|
message?: string,
|
|
|
|
) {
|
|
|
|
const actual = findSingle(...input);
|
|
|
|
assertEquals(actual, expected, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "findSingle() handles no mutation",
|
2021-09-12 15:04:15 +00:00
|
|
|
fn() {
|
|
|
|
const array = [1, 2, 3];
|
|
|
|
findSingle(array, (it) => it % 2 === 0);
|
|
|
|
|
|
|
|
assertEquals(array, [1, 2, 3]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "findSingle() handles empty input",
|
2021-09-12 15:04:15 +00:00
|
|
|
fn() {
|
|
|
|
findSingleTest(
|
|
|
|
[[], (_) => true],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "findSingle() handles only one element",
|
2021-09-12 15:04:15 +00:00
|
|
|
fn() {
|
|
|
|
findSingleTest(
|
|
|
|
[[42], (_it) => true],
|
|
|
|
42,
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[["foo"], (_it) => true],
|
|
|
|
"foo",
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[[null], (_it) => true],
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[[undefined], (_it) => true],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "findSingle() handles no matches",
|
2021-09-12 15:04:15 +00:00
|
|
|
fn() {
|
|
|
|
findSingleTest(
|
|
|
|
[[9, 11, 13], (it) => it % 2 === 0],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[["foo", "bar"], (it) => it.startsWith("z")],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[[{ done: false }], (it) => it.done],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "findSingle() handles only match",
|
2021-09-12 15:04:15 +00:00
|
|
|
fn() {
|
|
|
|
findSingleTest(
|
|
|
|
[[9, 12, 13], (it) => it % 2 === 0],
|
|
|
|
12,
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[["zap", "foo", "bar"], (it) => it.startsWith("z")],
|
|
|
|
"zap",
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[[{ done: false }, { done: true }], (it) => it.done],
|
|
|
|
{ done: true },
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-20 09:48:02 +00:00
|
|
|
name: "findSingle() handles multiple matches",
|
2021-09-12 15:04:15 +00:00
|
|
|
fn() {
|
|
|
|
findSingleTest(
|
|
|
|
[[9, 12, 13, 14], (it) => it % 2 === 0],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
findSingleTest(
|
|
|
|
[["zap", "foo", "bar", "zee"], (it) => it.startsWith("z")],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|