1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var Sync = require('..'),
|
7 | assert = require('assert');
|
8 |
|
9 |
|
10 | function asyncFunction(a, b, callback) {
|
11 | process.nextTick(function(){
|
12 | callback(null, a + b);
|
13 | })
|
14 | }
|
15 |
|
16 |
|
17 | function asyncFunctionSync(a, b, callback) {
|
18 | callback(null, a + b);
|
19 | }
|
20 |
|
21 |
|
22 | function asyncFunctionReturningValue(a, b, callback) {
|
23 | process.nextTick(function(){
|
24 | callback(null, a + b);
|
25 | })
|
26 | return 123;
|
27 | }
|
28 |
|
29 |
|
30 | function asyncFunctionReturningValueMultipleArguments(a, b, callback) {
|
31 | process.nextTick(function(){
|
32 | callback(null, a, b);
|
33 | })
|
34 | return 123;
|
35 | }
|
36 |
|
37 |
|
38 | function asyncFunctionThrowsException(a, b, callback) {
|
39 | process.nextTick(function(){
|
40 | callback('something went wrong');
|
41 | })
|
42 | }
|
43 |
|
44 |
|
45 | function asyncFunctionThrowsExceptionSync(a, b, callback) {
|
46 | callback('something went wrong');
|
47 | }
|
48 |
|
49 |
|
50 | function asyncFunctionReturningValueThrowsException(a, b, callback) {
|
51 | process.nextTick(function(){
|
52 | callback('something went wrong');
|
53 | })
|
54 | return 123;
|
55 | }
|
56 |
|
57 |
|
58 | function asyncFunctionCallbackTwice(a, b, callback) {
|
59 | process.nextTick(function(){
|
60 | callback(null, a + b);
|
61 | callback(null, a - b);
|
62 | })
|
63 | }
|
64 |
|
65 |
|
66 | function asyncFunctionMultipleArguments(a, b, callback) {
|
67 | process.nextTick(function(){
|
68 | callback(null, a, b);
|
69 | })
|
70 | }
|
71 |
|
72 |
|
73 | var testObject = {
|
74 |
|
75 | property : 2,
|
76 |
|
77 | asyncMethod : function someAsyncMethod(b, callback) {
|
78 | var self = this;
|
79 | process.nextTick(function(){
|
80 | callback(null, self.property + b);
|
81 | })
|
82 | },
|
83 |
|
84 | asyncMethodThrowsException : function someAsyncMethodThrowsException(b, callback) {
|
85 | process.nextTick(function(){
|
86 | callback('something went wrong');
|
87 | })
|
88 | },
|
89 |
|
90 | asyncMethodMultipleArguments : function asyncMethodMultipleArguments(b, callback) {
|
91 | var self = this;
|
92 | process.nextTick(function(){
|
93 | callback(null, self.property, b);
|
94 | })
|
95 | }
|
96 | }
|
97 |
|
98 | var runTest = module.exports = function(callback)
|
99 | {
|
100 | Sync(function(){
|
101 |
|
102 |
|
103 | var result = asyncFunction.sync(null, 2, 3);
|
104 | assert.equal(result, 2 + 3);
|
105 |
|
106 |
|
107 | var result = asyncFunctionSync.sync(null, 2, 3);
|
108 | assert.equal(result, 2 + 3);
|
109 |
|
110 |
|
111 | assert.throws(function(){
|
112 | var result = asyncFunctionThrowsException.sync(null, 2, 3);
|
113 | }, 'something went wrong');
|
114 |
|
115 |
|
116 | assert.throws(function(){
|
117 | var result = asyncFunctionThrowsExceptionSync.sync(null, 2, 3);
|
118 | }, 'something went wrong');
|
119 |
|
120 |
|
121 | var result = asyncFunctionReturningValue.sync(null, 2, 3);
|
122 | assert.equal(result, 2 + 3);
|
123 |
|
124 |
|
125 | var result = asyncFunctionMultipleArguments.sync(null, 2, 3);
|
126 | assert.deepEqual(result, [2, 3]);
|
127 |
|
128 |
|
129 | var result = asyncFunctionReturningValueMultipleArguments.sync(null, 2, 3);
|
130 | assert.deepEqual(result, [2, 3]);
|
131 |
|
132 |
|
133 | assert.throws(function(){
|
134 | var result = asyncFunctionReturningValueThrowsException.sync(null, 2, 3);
|
135 | }, 'something went wrong');
|
136 |
|
137 |
|
138 | var result = asyncFunctionCallbackTwice.sync(null, 2, 3);
|
139 | assert.equal(result, 2 + 3);
|
140 |
|
141 |
|
142 | var result = testObject.asyncMethod.sync(testObject, 3);
|
143 | assert.equal(result, testObject.property + 3);
|
144 |
|
145 |
|
146 | assert.throws(function(){
|
147 | var result = testObject.asyncMethodThrowsException.sync(testObject, 2);
|
148 | }, 'something went wrong');
|
149 |
|
150 |
|
151 | var result = testObject.asyncMethodMultipleArguments.sync(testObject, 3);
|
152 | assert.deepEqual(result, [testObject.property, 3]);
|
153 |
|
154 |
|
155 | var result = function(callback) {
|
156 | callback(null, 1);
|
157 | callback(null, 2);
|
158 | }.sync()
|
159 |
|
160 | assert.strictEqual(1, result);
|
161 |
|
162 |
|
163 |
|
164 | var result = function(callback) {
|
165 | callback();
|
166 | }.sync()
|
167 |
|
168 | assert.strictEqual(undefined, result);
|
169 |
|
170 | }, function(e){
|
171 | if (e) {
|
172 | console.error(e.stack);
|
173 | }
|
174 | if (callback) {
|
175 | callback(e);
|
176 | }
|
177 | })
|
178 | }
|
179 |
|
180 | if (!module.parent) {
|
181 | runTest(function(){
|
182 | console.log('%s done', __filename);
|
183 | });
|
184 | }
|