|
1 |
| -var async = require('../lib'); |
2 |
| -var expect = require('chai').expect; |
3 |
| -var assert = require('assert'); |
4 |
| - |
5 |
| -describe('ensureAsync', function() { |
6 |
| - var passContext = function(cb) { |
7 |
| - cb(this); |
8 |
| - }; |
9 |
| - |
10 |
| - it('defer sync functions', function(done) { |
11 |
| - var sync = true; |
12 |
| - async.ensureAsync(function (arg1, arg2, cb) { |
13 |
| - expect(arg1).to.equal(1); |
14 |
| - expect(arg2).to.equal(2); |
15 |
| - cb(null, 4, 5); |
16 |
| - })(1, 2, function (err, arg4, arg5) { |
17 |
| - expect(err).to.equal(null); |
18 |
| - expect(arg4).to.equal(4); |
19 |
| - expect(arg5).to.equal(5); |
20 |
| - assert(!sync, 'callback called on same tick'); |
21 |
| - done(); |
22 |
| - }); |
23 |
| - sync = false; |
24 |
| - }); |
25 |
| - |
26 |
| - it('do not defer async functions', function(done) { |
27 |
| - var sync = false; |
28 |
| - async.ensureAsync(function (arg1, arg2, cb) { |
29 |
| - expect(arg1).to.equal(1); |
30 |
| - expect(arg2).to.equal(2); |
31 |
| - async.setImmediate(function () { |
32 |
| - sync = true; |
33 |
| - cb(null, 4, 5); |
34 |
| - sync = false; |
35 |
| - }); |
36 |
| - })(1, 2, function (err, arg4, arg5) { |
37 |
| - expect(err).to.equal(null); |
38 |
| - expect(arg4).to.equal(4); |
39 |
| - expect(arg5).to.equal(5); |
40 |
| - assert(sync, 'callback called on next tick'); |
41 |
| - done(); |
42 |
| - }); |
43 |
| - }); |
44 |
| - |
45 |
| - it('double wrapping', function(done) { |
46 |
| - var sync = true; |
47 |
| - async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) { |
48 |
| - expect(arg1).to.equal(1); |
49 |
| - expect(arg2).to.equal(2); |
50 |
| - cb(null, 4, 5); |
51 |
| - }))(1, 2, function (err, arg4, arg5) { |
52 |
| - expect(err).to.equal(null); |
53 |
| - expect(arg4).to.equal(4); |
54 |
| - expect(arg5).to.equal(5); |
55 |
| - assert(!sync, 'callback called on same tick'); |
56 |
| - done(); |
57 |
| - }); |
58 |
| - sync = false; |
59 |
| - }); |
60 |
| - |
61 |
| - |
62 |
| - it('should propely bind context to the wrapped function', function(done) { |
63 |
| - |
64 |
| - // call bind after wrapping with ensureAsync |
65 |
| - var context = {context: "post"}; |
66 |
| - var postBind = async.ensureAsync(passContext); |
67 |
| - postBind = postBind.bind(context); |
68 |
| - postBind(function(ref) { |
69 |
| - expect(ref).to.equal(context); |
70 |
| - done(); |
71 |
| - }); |
72 |
| - }); |
73 |
| - |
74 |
| - it('should not override the bound context of a function when wrapping', function(done) { |
75 |
| - |
76 |
| - // call bind before wrapping with ensureAsync |
77 |
| - var context = {context: "pre"}; |
78 |
| - var preBind = passContext.bind(context); |
79 |
| - preBind = async.ensureAsync(preBind); |
80 |
| - preBind(function(ref) { |
81 |
| - expect(ref).to.equal(context); |
82 |
| - done(); |
83 |
| - }); |
84 |
| - }); |
85 |
| -}); |
| 1 | +var async = require('../lib'); |
| 2 | +var expect = require('chai').expect; |
| 3 | +var assert = require('assert'); |
| 4 | + |
| 5 | +describe('ensureAsync', function() { |
| 6 | + var passContext = function(cb) { |
| 7 | + cb(this); |
| 8 | + }; |
| 9 | + |
| 10 | + it('defer sync functions', function(done) { |
| 11 | + var sync = true; |
| 12 | + async.ensureAsync(function (arg1, arg2, cb) { |
| 13 | + expect(arg1).to.equal(1); |
| 14 | + expect(arg2).to.equal(2); |
| 15 | + cb(null, 4, 5); |
| 16 | + })(1, 2, function (err, arg4, arg5) { |
| 17 | + expect(err).to.equal(null); |
| 18 | + expect(arg4).to.equal(4); |
| 19 | + expect(arg5).to.equal(5); |
| 20 | + assert(!sync, 'callback called on same tick'); |
| 21 | + done(); |
| 22 | + }); |
| 23 | + sync = false; |
| 24 | + }); |
| 25 | + |
| 26 | + it('do not defer async functions', function(done) { |
| 27 | + var sync = false; |
| 28 | + async.ensureAsync(function (arg1, arg2, cb) { |
| 29 | + expect(arg1).to.equal(1); |
| 30 | + expect(arg2).to.equal(2); |
| 31 | + async.setImmediate(function () { |
| 32 | + sync = true; |
| 33 | + cb(null, 4, 5); |
| 34 | + sync = false; |
| 35 | + }); |
| 36 | + })(1, 2, function (err, arg4, arg5) { |
| 37 | + expect(err).to.equal(null); |
| 38 | + expect(arg4).to.equal(4); |
| 39 | + expect(arg5).to.equal(5); |
| 40 | + assert(sync, 'callback called on next tick'); |
| 41 | + done(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('double wrapping', function(done) { |
| 46 | + var sync = true; |
| 47 | + async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) { |
| 48 | + expect(arg1).to.equal(1); |
| 49 | + expect(arg2).to.equal(2); |
| 50 | + cb(null, 4, 5); |
| 51 | + }))(1, 2, function (err, arg4, arg5) { |
| 52 | + expect(err).to.equal(null); |
| 53 | + expect(arg4).to.equal(4); |
| 54 | + expect(arg5).to.equal(5); |
| 55 | + assert(!sync, 'callback called on same tick'); |
| 56 | + done(); |
| 57 | + }); |
| 58 | + sync = false; |
| 59 | + }); |
| 60 | + |
| 61 | + |
| 62 | + it('should propely bind context to the wrapped function', function(done) { |
| 63 | + |
| 64 | + // call bind after wrapping with ensureAsync |
| 65 | + var context = {context: "post"}; |
| 66 | + var postBind = async.ensureAsync(passContext); |
| 67 | + postBind = postBind.bind(context); |
| 68 | + postBind(function(ref) { |
| 69 | + expect(ref).to.equal(context); |
| 70 | + done(); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should not override the bound context of a function when wrapping', function(done) { |
| 75 | + |
| 76 | + // call bind before wrapping with ensureAsync |
| 77 | + var context = {context: "pre"}; |
| 78 | + var preBind = passContext.bind(context); |
| 79 | + preBind = async.ensureAsync(preBind); |
| 80 | + preBind(function(ref) { |
| 81 | + expect(ref).to.equal(context); |
| 82 | + done(); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments