-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
127 lines (115 loc) · 3.2 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const test = require('ava');
const {range, enumerate, zip, zipLongest, items, Iterator} = require('.');
test('range with stop', t => {
t.deepEqual(range(3).map(x => x + 1), [1, 2, 3]);
t.deepEqual(range(3).filter(x => x > 0), [1, 2]);
});
test('range with start, stop and step', t => {
t.deepEqual(range(-10, 20, 10).toArray(), [-10, 0, 10]);
});
test('enumerate', t => {
for (const [index, value] of enumerate([0, 1, 2])) {
t.is(index, value);
}
});
test('zip', t => {
let scenarios = [
{
iterFirst: ['one', 'two', 'three'],
iterSecond: [1, 2, 3]
},
{
iterFirst: ['one', 'two', 'three', 'four'],
iterSecond: [1, 2, 3]
},
{
iterFirst: ['one', 'two', 'three'],
iterSecond: [1, 2, 3, 4]
},
{
iterFirst: [],
iterSecond: []
}
];
let index;
for (const {iterFirst, iterSecond} of scenarios) {
index = 0;
for (const [first, second] of zip(iterFirst, iterSecond)) {
t.is(first, iterFirst[index]);
t.is(second, iterSecond[index]);
index++;
}
}
scenarios = [
{
iterFirst: ['one', 'two', 'three'],
iterSecond: [1, 2, 3],
iterThird: ['I', 'II', 'III']
},
{
iterFirst: ['one', 'two', 'three', 'four'],
iterSecond: [1, 2, 3],
iterThird: ['I', 'II']
},
{
iterFirst: ['one', 'two', 'three'],
iterSecond: [1, 2, 3, 4],
iterThird: ['I', 'II', 'III', 'IV', 'V']
},
{
iterFirst: [],
iterSecond: [],
iterThird: []
}
];
for (const {iterFirst, iterSecond, iterThird} of scenarios) {
for (const zipMethod of [zip, zipLongest]) {
index = 0;
for (const [first, second, third] of zipMethod(iterFirst, iterSecond, iterThird)) {
t.is(first, iterFirst[index]);
t.is(second, iterSecond[index]);
t.is(third, iterThird[index]);
index++;
}
}
}
});
test('items', t => {
const scenarios = [
{
one: 1,
two: 2,
three: 3
},
new Map([
[1, 'one'],
[2, 'two'],
[3, 'three']
])
];
for (const obj of scenarios) {
let {get} = obj;
if (obj instanceof Map) {
get = get.bind(obj);
} else {
get = key => obj[key];
}
for (const [key, value] of items(obj)) {
t.is(value, get(key));
}
}
});
test('reduce', t => {
t.is(range(5).reduce((accumulator, current) => accumulator + current), 10);
t.is(range(5).reduce((accumulator, current) => accumulator + current, 5), 15);
});
test('async Iterator', async t => {
for await (const [index, value] of enumerate([0, 1, 2])) {
t.is(index, value);
}
});
test('some and every', t => {
const numbers = Iterator.fromIterable([1, 2, 3]);
t.is(numbers.every(value => value > 0), true);
t.is(numbers.some(value => value > 3), false);
});