/
captureFn.js
75 lines (63 loc) · 1.97 KB
/
captureFn.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
'use strict';
var tape = require('../');
var tap = require('tap');
var concat = require('concat-stream');
var inspect = require('object-inspect');
var forEach = require('for-each');
var v = require('es-value-fixtures');
var stripFullStack = require('./common').stripFullStack;
tap.test('captureFn: output', function (tt) {
tt.plan(1);
var test = tape.createHarness();
var count = 0;
test.createStream().pipe(concat(function (body) {
tt.same(stripFullStack(body.toString('utf8')), [].concat(
'TAP version 13',
'# argument validation',
v.nonFunctions.map(function (x) {
return 'ok ' + ++count + ' ' + inspect(x) + ' is not a function';
}),
'# captured fn calls',
'ok ' + ++count + ' return value is passed through',
'ok ' + ++count + ' throwing implementation throws',
'ok ' + ++count + ' should be deeply equivalent',
'ok ' + ++count + ' should be deeply equivalent',
'',
'1..' + count,
'# tests ' + count,
'# pass ' + count,
'',
'# ok',
''
));
}));
test('argument validation', function (t) {
forEach(v.nonFunctions, function (nonFunction) {
t.throws(
function () { t.captureFn(nonFunction); },
TypeError,
inspect(nonFunction) + ' is not a function'
);
});
t.end();
});
test('captured fn calls', function (t) {
var sentinel = { sentinel: true, inspect: function () { return '{ SENTINEL OBJECT }'; } };
var wrappedSentinelThunk = t.captureFn(function () { return sentinel; });
var up = new SyntaxError('foo');
var wrappedThrower = t.captureFn(function () { throw up; });
t.equal(wrappedSentinelThunk(1, 2), sentinel, 'return value is passed through');
t.throws(
function () { wrappedThrower.call(sentinel, 1, 2, 3); },
SyntaxError,
'throwing implementation throws'
);
t.deepEqual(wrappedSentinelThunk.calls, [
{ args: [1, 2], receiver: undefined, returned: sentinel }
]);
t.deepEqual(wrappedThrower.calls, [
{ args: [1, 2, 3], receiver: sentinel, threw: true }
]);
t.end();
});
});