/
chunk_test.ts
102 lines (91 loc) · 1.83 KB
/
chunk_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../testing/asserts.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: "[collections/chunk] no mutation",
fn() {
const array = [1, 2, 3, 4];
chunk(array, 2);
assertEquals(array, [1, 2, 3, 4]);
},
});
Deno.test({
name: "[collections/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: "[collections/chunk] empty input",
fn() {
chunkTest(
[[], 1],
[],
);
},
});
Deno.test({
name: "[collections/chunk] single element chunks",
fn() {
chunkTest(
[testArray, 1],
testArray.map((it) => [it]),
);
chunkTest(
[["foo"], 1],
[["foo"]],
);
},
});
Deno.test({
name: "[collections/chunk] 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: "[collections/chunk] 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: "[collections/chunk] chunks equal to length",
fn() {
chunkTest(
[testArray, testArray.length],
[testArray],
);
chunkTest(
[["foo"], 1],
[["foo"]],
);
},
});