Skip to content

Commit c3507c4

Browse files
🧪 test: Refactor.
1 parent f640b0a commit c3507c4

File tree

3 files changed

+158
-143
lines changed

3 files changed

+158
-143
lines changed

test/src/_fixtures.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const repr = (x) => JSON.stringify(x);

test/src/zip.js

Lines changed: 75 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,85 @@ import {increasing} from '@total-order/primitive';
99

1010
import {zip} from '../../src/index.js';
1111

12-
test('zip', (t) => {
13-
const x = function (iterables, out) {
14-
t.deepEqual(list(zip(...iterables)), out);
12+
import {repr} from './_fixtures.js';
1513

16-
const strip = function* (iterables) {
17-
// Makes all the inputs have the same length
18-
// (min length among all iterables)
14+
const macro = (t, iterables, out) => {
15+
t.deepEqual(list(zip(...iterables)), out);
1916

20-
if (len(iterables) === 0) {
21-
return;
22-
}
17+
const strip = function* (iterables) {
18+
// Makes all the inputs have the same length
19+
// (min length among all iterables)
2320

24-
const n = min(increasing, map(len, iterables));
21+
if (len(iterables) === 0) {
22+
return;
23+
}
2524

26-
for (const iterable of iterables) {
27-
yield take(iterable, n);
28-
}
29-
};
25+
const n = min(increasing, map(len, iterables));
3026

31-
const unzipped = list(map(list, strip(iterables)));
32-
33-
t.deepEqual(list(zip(...out)), unzipped);
27+
for (const iterable of iterables) {
28+
yield take(iterable, n);
29+
}
3430
};
3531

36-
x([], []);
37-
x([[1]], [[1]]);
38-
x([[1, 2, 3]], [[1], [2], [3]]);
39-
x(
40-
[
41-
[1, 2, 3],
42-
[4, 5, 6],
43-
],
44-
[
45-
[1, 4],
46-
[2, 5],
47-
[3, 6],
48-
],
49-
);
50-
x(
51-
[
52-
[1, 2, 3],
53-
[4, 5, 6],
54-
[7, 8, 9],
55-
],
56-
[
57-
[1, 4, 7],
58-
[2, 5, 8],
59-
[3, 6, 9],
60-
],
61-
);
62-
x(
63-
[
64-
[1, 2, 3],
65-
[4, 5, 6],
66-
[7, 8, 9, 10],
67-
],
68-
[
69-
[1, 4, 7],
70-
[2, 5, 8],
71-
[3, 6, 9],
72-
],
73-
);
74-
x(
75-
[
76-
[1, 2, 3, 4],
77-
[4, 5, 6],
78-
[7, 8, 9],
79-
],
80-
[
81-
[1, 4, 7],
82-
[2, 5, 8],
83-
[3, 6, 9],
84-
],
85-
);
86-
});
32+
const unzipped = list(map(list, strip(iterables)));
33+
34+
t.deepEqual(list(zip(...out)), unzipped);
35+
};
36+
37+
macro.title = (title, iterables, out) =>
38+
title ?? `zip(...${repr(iterables)}) is ${repr(out)}`;
39+
40+
test(macro, [], []);
41+
test(macro, [[1]], [[1]]);
42+
test(macro, [[1, 2, 3]], [[1], [2], [3]]);
43+
test(
44+
macro,
45+
[
46+
[1, 2, 3],
47+
[4, 5, 6],
48+
],
49+
[
50+
[1, 4],
51+
[2, 5],
52+
[3, 6],
53+
],
54+
);
55+
test(
56+
macro,
57+
[
58+
[1, 2, 3],
59+
[4, 5, 6],
60+
[7, 8, 9],
61+
],
62+
[
63+
[1, 4, 7],
64+
[2, 5, 8],
65+
[3, 6, 9],
66+
],
67+
);
68+
test(
69+
macro,
70+
[
71+
[1, 2, 3],
72+
[4, 5, 6],
73+
[7, 8, 9, 10],
74+
],
75+
[
76+
[1, 4, 7],
77+
[2, 5, 8],
78+
[3, 6, 9],
79+
],
80+
);
81+
test(
82+
macro,
83+
[
84+
[1, 2, 3, 4],
85+
[4, 5, 6],
86+
[7, 8, 9],
87+
],
88+
[
89+
[1, 4, 7],
90+
[2, 5, 8],
91+
[3, 6, 9],
92+
],
93+
);

test/src/ziplongest.js

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,86 +10,93 @@ import {increasing} from '@total-order/primitive';
1010

1111
import {ziplongest} from '../../src/index.js';
1212

13-
test('ziplongest', (t) => {
14-
const x = function (fillvalue, iterables, out) {
15-
t.deepEqual(list(ziplongest(fillvalue, ...iterables)), out);
13+
import {repr} from './_fixtures.js';
1614

17-
const extend = function* (fillvalue, iterables) {
18-
// Makes all the inputs have the same length
19-
// (max length among all iterables)
15+
const macro = (t, fillvalue, iterables, out) => {
16+
t.deepEqual(list(ziplongest(fillvalue, ...iterables)), out);
2017

21-
if (len(iterables) === 0) {
22-
return;
23-
}
18+
const extend = function* (fillvalue, iterables) {
19+
// Makes all the inputs have the same length
20+
// (max length among all iterables)
2421

25-
const n = max(increasing, map(len, iterables));
22+
if (len(iterables) === 0) {
23+
return;
24+
}
2625

27-
for (const iterable of iterables) {
28-
yield chain(iterable, nrepeat(fillvalue, n - iterable.length));
29-
}
30-
};
26+
const n = max(increasing, map(len, iterables));
3127

32-
const unzipped = list(map(list, extend(fillvalue, iterables)));
33-
34-
t.deepEqual(list(ziplongest(fillvalue, ...out)), unzipped);
28+
for (const iterable of iterables) {
29+
yield chain(iterable, nrepeat(fillvalue, n - iterable.length));
30+
}
3531
};
3632

37-
const w = Math.random();
33+
const unzipped = list(map(list, extend(fillvalue, iterables)));
34+
35+
t.deepEqual(list(ziplongest(fillvalue, ...out)), unzipped);
36+
};
37+
38+
macro.title = (title, fillvalue, iterables, out) =>
39+
title ?? `ziplongest(${fillvalue}, ...${repr(iterables)}) is ${repr(out)}`;
40+
41+
const w = Math.random();
3842

39-
x(w, [], []);
40-
x(w, [[1]], [[1]]);
41-
x(w, [[1, 2, 3]], [[1], [2], [3]]);
42-
x(
43-
w,
44-
[
45-
[1, 2, 3],
46-
[4, 5, 6],
47-
],
48-
[
49-
[1, 4],
50-
[2, 5],
51-
[3, 6],
52-
],
53-
);
54-
x(
55-
w,
56-
[
57-
[1, 2, 3],
58-
[4, 5, 6],
59-
[7, 8, 9],
60-
],
61-
[
62-
[1, 4, 7],
63-
[2, 5, 8],
64-
[3, 6, 9],
65-
],
66-
);
67-
x(
68-
w,
69-
[
70-
[1, 2, 3],
71-
[4, 5, 6],
72-
[7, 8, 9, 10],
73-
],
74-
[
75-
[1, 4, 7],
76-
[2, 5, 8],
77-
[3, 6, 9],
78-
[w, w, 10],
79-
],
80-
);
81-
x(
82-
w,
83-
[
84-
[1, 2, 3, 4],
85-
[4, 5, 6],
86-
[7, 8, 9],
87-
],
88-
[
89-
[1, 4, 7],
90-
[2, 5, 8],
91-
[3, 6, 9],
92-
[4, w, w],
93-
],
94-
);
95-
});
43+
test(macro, w, [], []);
44+
test(macro, w, [[1]], [[1]]);
45+
test(macro, w, [[1, 2, 3]], [[1], [2], [3]]);
46+
test(
47+
macro,
48+
w,
49+
[
50+
[1, 2, 3],
51+
[4, 5, 6],
52+
],
53+
[
54+
[1, 4],
55+
[2, 5],
56+
[3, 6],
57+
],
58+
);
59+
test(
60+
macro,
61+
w,
62+
[
63+
[1, 2, 3],
64+
[4, 5, 6],
65+
[7, 8, 9],
66+
],
67+
[
68+
[1, 4, 7],
69+
[2, 5, 8],
70+
[3, 6, 9],
71+
],
72+
);
73+
test(
74+
macro,
75+
w,
76+
[
77+
[1, 2, 3],
78+
[4, 5, 6],
79+
[7, 8, 9, 10],
80+
],
81+
[
82+
[1, 4, 7],
83+
[2, 5, 8],
84+
[3, 6, 9],
85+
[w, w, 10],
86+
],
87+
);
88+
test(
89+
macro,
90+
w,
91+
[
92+
[1, 2, 3, 4],
93+
[4, 5, 6],
94+
[7, 8, 9],
95+
],
96+
[
97+
[1, 4, 7],
98+
[2, 5, 8],
99+
[3, 6, 9],
100+
[4, w, w],
101+
],
102+
);

0 commit comments

Comments
 (0)