-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunked_test.ts
41 lines (36 loc) · 1.6 KB
/
chunked_test.ts
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
32
33
34
35
36
37
38
39
40
41
import { test } from "@cross/test";
import { assertEquals, assertThrows } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { chunked } from "./chunked.ts";
await test("chunked the length is divisible by the size", () => {
const result = chunked([1, 2, 3, 4, 5, 6], 2);
const expected = [[1, 2], [3, 4], [5, 6]];
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number[]>>>(true);
});
await test("chunked the length is not divisible by the size", () => {
const result = chunked([1, 2, 3, 4, 5], 2);
const expected = [[1, 2], [3, 4], [5]];
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number[]>>>(true);
});
await test("chunked the length is equal to the size", () => {
const result = chunked([1, 2, 3, 4, 5], 5);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number[]>>>(true);
});
await test("chunked the length is less than the size", () => {
const result = chunked([1, 2, 3, 4, 5], 6);
const expected = [[1, 2, 3, 4, 5]];
assertEquals(Array.from(result), expected);
assertType<IsExact<typeof result, Iterable<number[]>>>(true);
});
await test("chunked throws RangeError", () => {
assertThrows(() => chunked([], NaN), RangeError);
assertThrows(() => chunked([], Infinity), RangeError);
assertThrows(() => chunked([], -Infinity), RangeError);
assertThrows(() => chunked([], -1), RangeError);
assertThrows(() => chunked([], 1.1), RangeError);
assertThrows(() => chunked([], 0), RangeError);
});