This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unexpectedGenerator.js
71 lines (66 loc) · 2 KB
/
unexpectedGenerator.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
export default {
name: 'unexpected-generators',
installInto: (expect) => {
expect.addType({
name: 'iterator',
identify: (subject) =>
subject && subject.next && typeof subject.next === 'function',
inspect: () => 'iterator',
});
expect.addAssertion(
'<iterator> to yield item <any>',
(expect, subject, pattern) => {
const yielded = subject.next();
if (yielded.done) {
expect.errorMode = 'bubble';
expect.fail(
'Iterator was expected to yield {0} but was done',
pattern,
);
}
expect(yielded.value, 'to satisfy', pattern);
},
);
expect.addAssertion(
'<iterator> to yield items <array>',
(expect, subject, pattern) => {
const yieldedValues = [];
for (let i = 0; i < pattern.length; i += 1) {
const yielded = subject.next();
if (yielded.done) {
expect.errorMode = 'bubble';
expect.fail(
'Iterator was expected to yield at least {0} values but was done after {1}',
pattern.length,
i,
);
}
yieldedValues.push(yielded.value);
}
expect(yieldedValues, 'to satisfy', pattern);
},
);
expect.addAssertion(
'<iterator> yielding with <any> <assertion?>',
(expect, subject, arg) => {
const { done, value } = subject.next(arg);
if (done) {
expect.errorMode = 'bubble';
expect.fail('Iterator was expected to yield but was done');
}
return expect.shift(value);
},
);
expect.addAssertion('<iterator> [not] to be done', (expect, subject) => {
const yielded = subject.next();
return expect(yielded.done, '[not] to be true');
});
expect.addAssertion(
'<iterator> to be done with <any>',
(expect, subject, arg) => {
const { done } = subject.next(arg);
return expect(done, 'to be true');
},
);
},
};