deno.land / std@0.224.0 / collections / chunk_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../assert/mod.ts";import { chunk } from "./chunk.ts";
function chunkTest<I>( input: [Array<I>, number], expected: Array<Array<I>>, message?: string,) { const actual = chunk(...input); assertEquals(actual, expected, message);}
const testArray = [1, 2, 3, 4, 5, 6];
Deno.test({ name: "chunk() handles no mutation", fn() { const array = [1, 2, 3, 4]; chunk(array, 2);
assertEquals(array, [1, 2, 3, 4]); },});
Deno.test({ name: "chunk() throws on non naturals", fn() { assertThrows(() => chunk([], +.5)); assertThrows(() => chunk([], -4.7)); assertThrows(() => chunk([], -2)); assertThrows(() => chunk([], +0)); assertThrows(() => chunk([], -0)); },});
Deno.test({ name: "chunk() handles empty input", fn() { chunkTest( [[], 1], [], ); },});
Deno.test({ name: "chunk() handles single element chunks", fn() { chunkTest( [testArray, 1], testArray.map((it) => [it]), ); chunkTest( [["foo"], 1], [["foo"]], ); },});
Deno.test({ name: "chunk() handles n chunks fitting", fn() { chunkTest( [testArray, 2], [[1, 2], [3, 4], [5, 6]], ); chunkTest( [testArray, 3], [[1, 2, 3], [4, 5, 6]], ); },});
Deno.test({ name: "chunk() handles n chunks not fitting", fn() { chunkTest( [testArray, 4], [[1, 2, 3, 4], [5, 6]], ); chunkTest( [testArray, 5], [[1, 2, 3, 4, 5], [6]], ); },});
Deno.test({ name: "chunk() handles chunks equal to length", fn() { chunkTest( [testArray, testArray.length], [testArray], ); chunkTest( [["foo"], 1], [["foo"]], ); },});
Version Info