Skip to content

Commit 6898695

Browse files
author
Jocelyn Badgley (Twipped)
committed
Added tests for iterator and promise functions
1 parent e622b02 commit 6898695

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

tests/iterators.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
import tap from 'tap';
3+
import { mapMode, isIterator, isIterable, isGenerator, ensureIterable, MAPMODE, map } from '../src/index';
4+
5+
const array = [ 1, 2, 3 ];
6+
const object = { a: 1, b: 2, c: 3 };
7+
const generator = function* () {
8+
let i = 3;
9+
while (i) yield i--;
10+
};
11+
12+
const emptyIterator = { next: () => ({ done: true }) };
13+
const emptyIterable = { [Symbol.iterator]: () => emptyIterator };
14+
15+
tap.equal(mapMode(array), MAPMODE.ARRAY);
16+
tap.equal(mapMode(generator), false);
17+
tap.equal(mapMode(generator()), MAPMODE.ITERATOR);
18+
tap.equal(mapMode(emptyIterator), MAPMODE.ITERATOR);
19+
tap.equal(mapMode(emptyIterable), MAPMODE.ITERABLE);
20+
tap.equal(mapMode(object), MAPMODE.OBJECT);
21+
22+
tap.equal(isIterator(emptyIterable), false);
23+
tap.equal(isIterator(emptyIterator), true);
24+
tap.equal(isIterator(array), false);
25+
tap.equal(isIterator(generator), false);
26+
tap.equal(isIterator(generator()), true);
27+
tap.equal(isIterator(object), false);
28+
29+
tap.equal(isIterable(emptyIterable), true);
30+
tap.equal(isIterable(emptyIterator), false);
31+
tap.equal(isIterable(array), true);
32+
tap.equal(isIterable(generator), false);
33+
tap.equal(isIterable(generator()), true);
34+
tap.equal(isIterable(object), false);
35+
36+
tap.equal(isGenerator(emptyIterable), false);
37+
tap.equal(isGenerator(emptyIterator), false);
38+
tap.equal(isGenerator(array), false);
39+
tap.equal(isGenerator(generator), true);
40+
tap.equal(isGenerator(generator()), false);
41+
tap.equal(isGenerator(object), false);
42+
43+
tap.ok(isIterable(ensureIterable(emptyIterator)));
44+
tap.ok(isIterable(ensureIterable(emptyIterable)));
45+
tap.ok(isIterator(ensureIterable(emptyIterator)));
46+
tap.notOk(isIterator(ensureIterable(emptyIterable)));
47+
tap.notOk(isGenerator(ensureIterable(emptyIterator)));
48+
49+
const mappedGenerator = map(generator());
50+
tap.type(mappedGenerator, Array);
51+
tap.same(mappedGenerator, [ 3, 2, 1 ]);
52+

tests/promises.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
import tap from 'tap';
3+
import { pmap, pdelay, prace, preduce } from '../src/index';
4+
5+
const array = [ 100, 110, 70, 10, 11, 150, 220, 250 ];
6+
7+
tap.test('promise delay', async (t) => {
8+
const now = Date.now();
9+
await pdelay(500)();
10+
t.ok(Date.now() - now > 500, 'It took more than half a second to resolve');
11+
});
12+
13+
tap.test('promise race', async (t) => {
14+
15+
const result = await prace(
16+
pdelay(100)(100),
17+
pdelay(110)(110),
18+
pdelay(70)(70),
19+
pdelay(10)(10),
20+
pdelay(11)(11),
21+
);
22+
23+
t.equal(result, 10, 'The fastest promise resolved');
24+
});
25+
26+
27+
tap.test('promise map no concurrency', async (t) => {
28+
29+
const result = await pmap(array, (v) => pdelay(v)(v));
30+
31+
t.same(result, array, 'Non concurrent results.');
32+
});
33+
34+
tap.test('promise map with concurrency', async (t) => {
35+
36+
const result = await pmap(array, (v) => pdelay(v)(v), { concurrency: 3 });
37+
38+
t.same(result, array, 'Concurrent results.');
39+
});
40+
41+
tap.test('promise reduce', async (t) => {
42+
43+
const result = await preduce(array, (prev, v) => pdelay(v)(prev + v), 0);
44+
45+
t.same(result, 921, 'Reduced result.');
46+
});

0 commit comments

Comments
 (0)