-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
96 lines (85 loc) · 2.03 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
var co = require('co');
var map = require('./');
var assert = require('assert');
var equal = assert.equal;
describe('map(read, fn)', function(){
it('should map', function(done){
co(function*(){
var read = prefix(twice(), 'data: ');
equal('data: 1', yield read());
equal('data: 2', yield read());
assert(!(yield read()));
})(done);
function prefix(source, str){
return map(source, function*(data){
return str + data;
});
}
});
it('should not end on falsy return values', function(done){
co(function*(){
var read = prefix(source(), 'data: ');
assert('data: 2' == (yield read()));
assert(!(yield read()));
})(done);
function prefix(source, str){
var first = true;
return map(source, function*(data){
if (first) {
first = false;
return;
}
return str + data;
});
}
function source(){
var i = 0;
return function*(){
if (++i <3) {
return ''+i;
}
}
}
});
it('should yield fns', function(done){
co(function*(){
var read = prefix(twice(), 'data: ');
equal('data: 1', yield read());
equal('data: 2', yield read());
assert(!(yield read()));
})(done);
function prefix(source, str){
var first = true;
return map(source, function*(data){
yield sleep(10);
return str + data;
});
}
});
it('should iterate over arrays', function(done){
co(function*(){
var read = prefix(['1', '2'], 'data: ');
equal('data: 1', yield read());
equal('data: 2', yield read());
assert(!(yield read()));
})(done);
function prefix(source, str){
var first = true;
return map(source, function*(data){
yield sleep(10);
return str + data;
});
}
});
});
function twice(){
var i = 0;
return function*(){
if (++i <3) return ''+i;
}
}
function sleep(time){
return function(cb){
setTimeout(cb, time);
}
}