-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
83 lines (69 loc) · 2.09 KB
/
test.js
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
const assert = require("assert");
const { groupArrayElements } = require("./index.js");
function test(description, fn) {
try {
fn();
console.log("\x1b[32m%s\x1b[0m", `✓ ${description}: passed`);
} catch (e) {
console.log("\x1b[31m%s\x1b[0m", `✘ ${description}: failed. ${e.message}`);
}
}
test("empty undefined array throws error", () =>
assert.throws(() => groupArrayElements(undefined, 2), {
message: "Invalid arr argument",
}));
test("empty array input returns empty", () =>
assert.deepStrictEqual([], groupArrayElements([], 2)));
test("invalid size throws error", () =>
assert.throws(() => groupArrayElements([1, 2]), {
message: "Invalid size argument",
}));
test("negative size throws error", () =>
assert.throws(() => groupArrayElements([1, 2], -1), {
message: "Size argument cannot be less than 1",
}));
test("zero size throws error", () =>
assert.throws(() => groupArrayElements([1, 2], 0), {
message: "Size argument cannot be less than 1",
}));
test("group with no remainder", () =>
assert.deepStrictEqual(
[
[1, 2],
[3, 4],
],
groupArrayElements([1, 2, 3, 4], 2)
));
test("group with remainder", () =>
assert.deepStrictEqual(
[[1, 2], [3, 4], [5]],
groupArrayElements([1, 2, 3, 4, 5], 2)
));
test("group length shorter than size returns original", () =>
assert.deepStrictEqual(
[[1, 2, 3, 4, 5]],
groupArrayElements([1, 2, 3, 4, 5], 7)
));
test("group length equal to size returns original", () =>
assert.deepStrictEqual(
[[1, 2, 3, 4, 5]],
groupArrayElements([1, 2, 3, 4, 5], 5)
));
test("group preserves ordering", () => {
assert.deepStrictEqual(
[[7, 3], [2, 1], [5]],
groupArrayElements([7, 3, 2, 1, 5], 2)
);
});
test("works with strings", () => {
assert.deepStrictEqual(
[["foo", "bar"], ["baz"]],
groupArrayElements(["foo", "bar", "baz"], 2)
);
});
test("works with arrays as elements", () => {
assert.deepStrictEqual(
[[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10]]],
groupArrayElements([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], 2)
);
});