-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
99 lines (80 loc) · 1.87 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
require('mocha');
var assert = require('mocha').assert;
var atry = require('./atry');
it('should catch synchronous exception', function(cb) {
var objToThrow = {test: 42};
atry(function() {
throw objToThrow;
}).catch(function(err) {
if(err.test === 42)
cb();
else
cb(err);
});
});
it('should catch asynchronous exception', function(cb) {
var objToThrow = {test: 42};
atry(function() {
setTimeout(function() {
throw objToThrow;
}, 10);
}).catch(function(err) {
if(err.test === 42)
cb();
else
cb(err);
});
});
var net = require('net');
it('should catch asynchronous exception from foreign module', function(cb) {
atry(function() {
setTimeout(function() {
net.createServer().listen(1);
}, 10);
}).catch(function(err) {
cb();
});
});
it('should forward arguments to running function', function() {
atry(function(num1, num2) {
num1.should.equal(42);
num2.should.equal(33);
}, 42, 33);
});
var fs = require('fs');
describe('bind', function() {
it('should bind function and forward arguments', function(cb) {
fs.readFile('/dupa', atry.bind(function(err, data) {
setTimeout(function() {
throw err;
}, 10);
}).catch(function(err) {
if(err)
cb();
else
cb(err);
}));
});
it('should bind function and intercept error', function(cb) {
fs.readFile('/dupa/__notexistingfile', atry.intercept(function(data) {
}).catch(function(err) {
if(err)
cb();
else
cb(err);
}));
});
it('should bind function and forward other args', function(cb) {
var onData = function(cb) {
setTimeout(function() {
cb(null, 42);
}, 10);
}
onData(atry.intercept(function(data) {
if(data === 42)
cb();
}).catch(function(err) {
cb(err);
}));
});
});