Skip to content

Commit a8f9e08

Browse files
🧪 test: Factor out str function definition.
1 parent f65a8c8 commit a8f9e08

File tree

6 files changed

+22
-33
lines changed

6 files changed

+22
-33
lines changed

test/src/_fixtures.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function str(any) {
2+
const s = JSON.stringify(any);
3+
if (s <= 40) return s;
4+
return s.slice(0, 19) + '..' + s.slice(-19);
5+
}

test/src/concat.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import test from 'ava';
22

33
import {list, range, map} from '@aureooms/js-itertools';
44

5+
import {str} from './_fixtures.js';
6+
57
import {from, concat, iter} from '../../src/index.js';
68

79
function macro(t, A, B) {
@@ -14,13 +16,7 @@ function macro(t, A, B) {
1416
t.deepEqual(expected, result);
1517
}
1618

17-
function string(array) {
18-
const s = JSON.stringify(array);
19-
if (s <= 40) return s;
20-
return s.slice(0, 19) + '..' + s.slice(-19);
21-
}
22-
23-
macro.title = (title, A, B) => title || `concat(${string(A)}, ${string(B)})`;
19+
macro.title = (title, A, B) => title || `concat(${str(A)}, ${str(B)})`;
2420

2521
test(macro, [], []);
2622
test(macro, [], [1]);

test/src/from.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import test from 'ava';
22

33
import {list, range, map} from '@aureooms/js-itertools';
44

5+
import {str} from './_fixtures.js';
6+
57
import {from, iter} from '../../src/index.js';
68

79
function macro(t, array) {
@@ -10,13 +12,7 @@ function macro(t, array) {
1012
t.deepEqual(array, result);
1113
}
1214

13-
function string(array) {
14-
const s = JSON.stringify(array);
15-
if (s <= 40) return s;
16-
return s.slice(0, 19) + '..' + s.slice(-19);
17-
}
18-
19-
macro.title = (title, array) => title || `from(${string(array)})`;
15+
macro.title = (title, array) => title || `from(${str(array)})`;
2016

2117
test(macro, []);
2218
test(macro, [27]);

test/src/len.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava';
22

33
import {list, range} from '@aureooms/js-itertools';
4+
import {str} from './_fixtures.js';
45
import {from, len} from '../../src/index.js';
56

67
const macro = (t, input) => {
@@ -10,12 +11,12 @@ const macro = (t, input) => {
1011
};
1112

1213
macro.title = (title, input) =>
13-
title || `len(from(${JSON.stringify(input)})) = ${input.length}`;
14+
title || `len(from(${str(input)})) = ${input.length}`;
1415

1516
test(macro, []);
1617
test(macro, '');
1718
test(macro, [{}]);
1819
test(macro, 'z');
1920
test(macro, [1, 2, 3]);
2021
test(macro, 'abracadabra');
21-
test('len(from(list(range(987)))) = 987', macro, list(range(987)));
22+
test(macro, list(range(987)));

test/src/pop.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import test from 'ava';
22

33
import {list, range, map} from '@aureooms/js-itertools';
44

5+
import {str} from './_fixtures.js';
6+
57
import {from, pop, iter} from '../../src/index.js';
68

79
function throws(t, array) {
@@ -10,7 +12,7 @@ function throws(t, array) {
1012
t.throws(() => pop(node), {message: /empty/});
1113
}
1214

13-
throws.title = (title, array) => title || `pop(${string(array)}) ~> throws`;
15+
throws.title = (title, array) => title || `pop(${str(array)}) ~> throws`;
1416

1517
function macro(t, array) {
1618
const node = from(array);
@@ -21,13 +23,7 @@ function macro(t, array) {
2123
}
2224

2325
macro.title = (title, array) =>
24-
title || `pop(${string(array)}) ~> ${array[array.length - 1]}`;
25-
26-
function string(array) {
27-
const s = JSON.stringify(array);
28-
if (s <= 40) return s;
29-
return s.slice(0, 19) + '..' + s.slice(-19);
30-
}
26+
title || `pop(${str(array)}) ~> ${array[array.length - 1]}`;
3127

3228
test(throws, []);
3329
test(macro, [27]);

test/src/shift.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import test from 'ava';
22

33
import {list, range, map} from '@aureooms/js-itertools';
44

5+
import {str} from './_fixtures.js';
6+
57
import {from, shift, iter} from '../../src/index.js';
68

79
function throws(t, array) {
@@ -10,7 +12,7 @@ function throws(t, array) {
1012
t.throws(() => shift(node), {message: /empty/});
1113
}
1214

13-
throws.title = (title, array) => title || `shift(${string(array)}) ~> throws`;
15+
throws.title = (title, array) => title || `shift(${str(array)}) ~> throws`;
1416

1517
function macro(t, array) {
1618
const node = from(array);
@@ -20,14 +22,7 @@ function macro(t, array) {
2022
t.deepEqual(array.slice(1), result);
2123
}
2224

23-
macro.title = (title, array) =>
24-
title || `shift(${string(array)}) ~> ${array[0]}`;
25-
26-
function string(array) {
27-
const s = JSON.stringify(array);
28-
if (s <= 40) return s;
29-
return s.slice(0, 19) + '..' + s.slice(-19);
30-
}
25+
macro.title = (title, array) => title || `shift(${str(array)}) ~> ${array[0]}`;
3126

3227
test(throws, []);
3328
test(macro, [27]);

0 commit comments

Comments
 (0)