deno.land / std@0.224.0 / collections / find_single_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";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({ name: "findSingle() handles no mutation", fn() { const array = [1, 2, 3]; findSingle(array, (it) => it % 2 === 0);
assertEquals(array, [1, 2, 3]); },});
Deno.test({ name: "findSingle() handles empty input", fn() { findSingleTest( [[], (_) => true], undefined, ); },});
Deno.test({ name: "findSingle() handles only one element", fn() { findSingleTest( [[42], (_it) => true], 42, ); findSingleTest( [["foo"], (_it) => true], "foo", ); findSingleTest( [[null], (_it) => true], null, ); findSingleTest( [[undefined], (_it) => true], undefined, ); },});
Deno.test({ name: "findSingle() handles no matches", 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({ name: "findSingle() handles only match", 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({ name: "findSingle() handles multiple matches", fn() { findSingleTest( [[9, 12, 13, 14], (it) => it % 2 === 0], undefined, ); findSingleTest( [["zap", "foo", "bar", "zee"], (it) => it.startsWith("z")], undefined, ); },});
Version Info