-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-sequence.test.js
180 lines (165 loc) · 5.89 KB
/
create-sequence.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
/** @typedef {import('./src/create-sequence').CreateSequence} CreateSequence */
/** @type {CreateSequence} */
const createSequence = require('../src/create-sequence');
describe('Create sequence', () => {
test('Sequence can be created from array', () => {
const sequence = createSequence(['a', 'b', 'c']);
let value = sequence.value;
let seq = Array.from(sequence);
expect(value).toEqual('a');
expect(seq).toEqual(['a', 'b', 'c']);
});
test('Sequence can be created from array-like object', () => {
const sequence = createSequence({0: 'a', 1: 'b', 2: 'c', length: 3});
let value = sequence.value;
let seq = Array.from(sequence);
expect(value).toEqual('a');
expect(seq).toEqual(['a', 'b', 'c']);
});
test('Sequence can be created from object with custom generator-based iterator', () => {
const sequence = createSequence({
* [Symbol.iterator]() {
yield 'a';
yield 'b';
yield 'c';
},
});
let value = sequence.value;
let seq = Array.from(sequence);
expect(value).toEqual('a');
expect(seq).toEqual(['a', 'b', 'c']);
});
test('Sequence can be created from object with custom iterator based on iterable protocol', () => {
const sequence = createSequence({
[Symbol.iterator]() {
const values = ['a', 'b', 'c'];
let i = 0;
return {
next() {
return {value: values[i], done: i++ === values.length};
},
};
},
});
let value = sequence.value;
let seq = Array.from(sequence);
expect(value).toEqual('a');
expect(seq).toEqual(['a', 'b', 'c']);
});
test('Sequence can be created from plain function which supports multiple calls', () => {
const values = ['a', 'b', 'c'];
const producer = function () {
return values.shift();
};
const sz = values.length;
const sequence = createSequence(producer);
let value = sequence.value;
let seq = [];
let n = sz;
for (let v of sequence) {
seq.push(v);
if (--n === 0) {
break;
}
}
expect(value).toEqual('a');
expect(seq).toEqual(['a', 'b', 'c']);
});
test('Sequence can be created from generator function', () => {
const producer = function* () {
yield 'a';
yield 'b';
yield 'c';
};
const sequence = createSequence(producer);
let value = sequence.value;
let seq = Array.from(sequence);
expect(value).toEqual('a');
expect(seq).toEqual(['a', 'b', 'c']);
});
test('Sequence value should be available directly, as a function and as iterator', () => {
const producer = () => 1;
const sequence = createSequence(producer);
expect(sequence()).toEqual(1);
expect(sequence.value).toEqual(1);
// noinspection LoopStatementThatDoesntLoopJS
for (let v of sequence) {
expect(v).toEqual(1);
break;
}
});
test('First sequence value should be available as a static value', () => {
let i = 42;
const producer = () => {
return i++;
};
const sequence = createSequence(producer);
expect(sequence.value).toEqual(42);
expect(sequence()).toEqual(42);
expect(sequence.value).toEqual(42);
expect(sequence()).toEqual(43);
expect(sequence.value).toEqual(42);
expect(sequence()).toEqual(44);
});
test('Sequence values should be available through repetitive calls', () => {
const values = ['a', 'b', 'c'];
const producer = function () {
return values.shift();
};
const sequence = createSequence(producer);
expect(sequence()).toEqual('a');
expect(sequence()).toEqual('b');
expect(sequence()).toEqual('c');
expect(sequence()).toBeUndefined();
});
test('Sequence values should be available through iterator', () => {
const producer = function* () {
yield 'a';
yield 'b';
yield 'c';
};
const sequence = createSequence(producer);
const received = [];
for (let v of sequence) {
received.push(v);
}
expect(received).toEqual(['a', 'b', 'c']);
});
test('Error thrown from producer should not be swallowed', () => {
const producer = function* () {
yield 'a';
yield 'b';
throw new Error('Thrown from producer');
// noinspection UnreachableCodeJS
yield 'c';
};
const sequence = createSequence(producer);
expect(sequence()).toEqual('a');
expect(sequence()).toEqual('b');
expect(() => sequence()).toThrow('Thrown from producer');
expect(sequence()).toBeUndefined();
});
test('It is allowed to mix access of sequence values', () => {
const sequence = createSequence(['a', 'b', 'c', 'd', 'e', 'f']);
const received = [];
received.push(sequence());
received.push(sequence());
let n = 2;
for (let v of sequence) {
received.push(v);
if (--n === 2) {
break;
}
}
received.push(sequence());
received.push(sequence());
expect(received).toEqual(['a', 'b', 'c', 'd', 'e', 'f']);
});
test('Setting value property of sequence generator is not allowed', () => {
const sequence = createSequence(() => 1);
expect(sequence.value).toEqual(1);
expect(() => sequence.value = 2).toThrow('This operation is not allowed');
expect(sequence.value).toEqual(1);
});
});