This repository has been archived by the owner on Aug 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.js
147 lines (117 loc) · 3.69 KB
/
tests.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
var fs = require("fs");
var cocb = require("./");
var path = require("path");
var test = require("tape");
var delay = cocb.wrap(function(time, err, data, callback){
setTimeout(function(){
callback(err, data);
}, time);
});
test("run", function(t){
cocb.run(function*(){
return yield delay(1, null, 3);
}, function(err, val){
if(err) return t.end(err);
t.equals(val, 3);
cocb.run(function*(){
return yield delay(1, "foo", 3);
}, function(err, val){
t.equals(err, "foo");
t.end();
});
});
});
test("order", function(t){
var events = [];
var delayE = cocb.wrap(function(id, time, err, data, callback){
events.push("start:" + id);
setTimeout(function(){
if(err) return callback(err);
events.push("done:" + id);
callback(null, data);
}, time);
});
cocb.run(function*(){
(yield delayE("a", 10, null, true )) || (yield delayE("b", 1, null, true));
(yield delayE("c", 10, null, false)) || (yield delayE("d", 1, null, true));
yield delayE(yield delayE("foo", 10, null, "bar"), 10, null, "");
yield delayE("baz", 10, "qux", "quux");
t.fail();
}, function(err){
t.deepEquals(err, "qux");
t.deepEquals(events, [
"start:a",
"done:a",
"start:c",
"done:c",
"start:d",
"done:d",
"start:foo",
"done:foo",
"start:bar",
"done:bar",
"start:baz",
]);
t.end();
});
});
test("throw", function(t){
t.plan(3);
var throwup = function(){
throw "foobar"
};
cocb.run(function*(){
throwup();
t.fail();
}, function(err){
t.deepEquals(err, "foobar");
});
var throwupYieldable = cocb.wrap(function(callback){
throw "baz";
});
cocb.run(function*(){
yield throwupYieldable();
t.fail();
}, function(err){
t.deepEquals(err, "baz");
});
var throwupCBYieldable = cocb.wrap(function(callback){
callback("qux");
});
cocb.run(function*(){
yield throwupCBYieldable();
t.fail();
}, function(err){
t.deepEquals(err, "qux");
});
});
test("wrap", function(t){
var fnWith2Args = cocb.wrap(function(a, b, callback){
callback(null, [a, b]);
});
var readFileYieldable = cocb.wrap(fs.readFile);
cocb.run(function*(){
t.deepEquals(yield fnWith2Args(1, 2), [1, 2]);
t.deepEquals(yield fnWith2Args(1), [1, void 0], "callback works if less than required args are given");
t.deepEquals(yield fnWith2Args(), [void 0, void 0], "callback works if no args are given");
t.deepEquals(yield fnWith2Args(3, 2, 1), [3, 2], "extra args are ignored, and callback still works");
var txt = yield readFileYieldable(path.resolve(__dirname, "package.json"));
t.ok(txt.length > 0);
}, t.end);
});
test("isGeneratorFunction", function(t){
t.equal(cocb.isGeneratorFunction(function*(){}), true);
t.equal(cocb.isGeneratorFunction(function(){}), false);
var f = function(){};
f.next = function(){};
f.throw = function(){};
t.equal(cocb.isGeneratorFunction(f), false);
t.equal(cocb.isGeneratorFunction(cocb.wrap(function*(){})), false);
t.equal(cocb.isGeneratorFunction(void 0), false);
t.equal(cocb.isGeneratorFunction(null), false);
t.equal(cocb.isGeneratorFunction(false), false);
t.equal(cocb.isGeneratorFunction(NaN), false);
t.equal(cocb.isGeneratorFunction([]), false);
t.equal(cocb.isGeneratorFunction({}), false);
t.end();
});