UNPKG

5.65 kBJavaScriptView Raw
1
2/**
3 * Tests for Function.prototype.sync
4 */
5
6var Sync = require('..'),
7 assert = require('assert');
8
9// Simple asynchronous function
10function asyncFunction(a, b, callback) {
11 process.nextTick(function(){
12 callback(null, a + b);
13 })
14}
15
16// Simple asynchronous function which invokes callback in the same tick
17function asyncFunctionSync(a, b, callback) {
18 callback(null, a + b);
19}
20
21// Simple asynchronous function returning a value synchronously
22function asyncFunctionReturningValue(a, b, callback) {
23 process.nextTick(function(){
24 callback(null, a + b);
25 })
26 return 123;
27}
28
29// Asynchronous which returns multiple arguments to a callback and returning a value synchronously
30function asyncFunctionReturningValueMultipleArguments(a, b, callback) {
31 process.nextTick(function(){
32 callback(null, a, b);
33 })
34 return 123;
35}
36
37// Simple asynchronous function which throws an exception
38function asyncFunctionThrowsException(a, b, callback) {
39 process.nextTick(function(){
40 callback('something went wrong');
41 })
42}
43
44// Simple asynchronous function which throws an exception in the same tick
45function asyncFunctionThrowsExceptionSync(a, b, callback) {
46 callback('something went wrong');
47}
48
49// Simple asynchronous function which throws an exception and returning a value synchronously
50function asyncFunctionReturningValueThrowsException(a, b, callback) {
51 process.nextTick(function(){
52 callback('something went wrong');
53 })
54 return 123;
55}
56
57// Wrong asynchronous which calls callback twice
58function asyncFunctionCallbackTwice(a, b, callback) {
59 process.nextTick(function(){
60 callback(null, a + b);
61 callback(null, a - b);
62 })
63}
64
65// Asynchronous which returns multiple arguments to a callback
66function asyncFunctionMultipleArguments(a, b, callback) {
67 process.nextTick(function(){
68 callback(null, a, b);
69 })
70}
71
72// test object
73var 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
98var runTest = module.exports = function(callback)
99{
100 Sync(function(){
101
102 // test on returning value
103 var result = asyncFunction.sync(null, 2, 3);
104 assert.equal(result, 2 + 3);
105
106 // test on returning value in the same tick
107 var result = asyncFunctionSync.sync(null, 2, 3);
108 assert.equal(result, 2 + 3);
109
110 // test on throws exception
111 assert.throws(function(){
112 var result = asyncFunctionThrowsException.sync(null, 2, 3);
113 }, 'something went wrong');
114
115 // test on throws exception in the same tick
116 assert.throws(function(){
117 var result = asyncFunctionThrowsExceptionSync.sync(null, 2, 3);
118 }, 'something went wrong');
119
120 // test asynchronous function should not return a synchronous value
121 var result = asyncFunctionReturningValue.sync(null, 2, 3);
122 assert.equal(result, 2 + 3);
123
124 // test returning multiple arguments
125 var result = asyncFunctionMultipleArguments.sync(null, 2, 3);
126 assert.deepEqual(result, [2, 3]);
127
128 // test asynchronous function should not return a synchronous value (multiple arguments)
129 var result = asyncFunctionReturningValueMultipleArguments.sync(null, 2, 3);
130 assert.deepEqual(result, [2, 3]);
131
132 // test asynchronous function should not return a synchronous value (throwing exception)
133 assert.throws(function(){
134 var result = asyncFunctionReturningValueThrowsException.sync(null, 2, 3);
135 }, 'something went wrong');
136
137 // test asynchronous which calls callback twice (should not be called twice)
138 var result = asyncFunctionCallbackTwice.sync(null, 2, 3);
139 assert.equal(result, 2 + 3);
140
141 // test on returning value with object context
142 var result = testObject.asyncMethod.sync(testObject, 3);
143 assert.equal(result, testObject.property + 3);
144
145 // test on throws exception with object context
146 assert.throws(function(){
147 var result = testObject.asyncMethodThrowsException.sync(testObject, 2);
148 }, 'something went wrong');
149
150 // test returning multiple arguments with object context
151 var result = testObject.asyncMethodMultipleArguments.sync(testObject, 3);
152 assert.deepEqual(result, [testObject.property, 3]);
153
154 // test double-callback
155 var result = function(callback) {
156 callback(null, 1);
157 callback(null, 2);
158 }.sync()
159
160 assert.strictEqual(1, result);
161
162 // test undefined result
163 // test double-callback
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
180if (!module.parent) {
181 runTest(function(){
182 console.log('%s done', __filename);
183 });
184}