|
1 | 1 | /**
|
2 | 2 | * @license Apache-2.0
|
3 | 3 | *
|
4 |
| -* Copyright (c) 2018 The Stdlib Authors. |
| 4 | +* Copyright (c) 2023 The Stdlib Authors. |
5 | 5 | *
|
6 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | // MODULES //
|
22 | 22 |
|
23 | 23 | var tape = require( 'tape' );
|
24 |
| -var noop = require( '@stdlib/utils-noop' ); |
25 |
| -var whileAsync = require( './../../dist' ); |
| 24 | +var main = require( './../../dist' ); |
26 | 25 |
|
27 | 26 |
|
28 | 27 | // TESTS //
|
29 | 28 |
|
30 |
| -tape( 'main export is a function', function test( t ) { |
| 29 | +tape( 'main export is defined', function test( t ) { |
31 | 30 | t.ok( true, __filename );
|
32 |
| - t.strictEqual( typeof whileAsync, 'function', 'main export is a function' ); |
| 31 | + t.strictEqual( main !== void 0, true, 'main export is defined' ); |
33 | 32 | t.end();
|
34 | 33 | });
|
35 |
| - |
36 |
| -tape( 'the function throws an error if not provided a predicate function', function test( t ) { |
37 |
| - var values; |
38 |
| - var i; |
39 |
| - |
40 |
| - values = [ |
41 |
| - '5', |
42 |
| - 5, |
43 |
| - NaN, |
44 |
| - true, |
45 |
| - false, |
46 |
| - null, |
47 |
| - void 0, |
48 |
| - {}, |
49 |
| - [], |
50 |
| - /.*/, |
51 |
| - new Date() |
52 |
| - ]; |
53 |
| - |
54 |
| - for ( i = 0; i < values.length; i++ ) { |
55 |
| - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); |
56 |
| - } |
57 |
| - t.end(); |
58 |
| - |
59 |
| - function badValue( value ) { |
60 |
| - return function badValue() { |
61 |
| - whileAsync( value, noop, noop ); |
62 |
| - }; |
63 |
| - } |
64 |
| -}); |
65 |
| - |
66 |
| -tape( 'the function throws an error if not provided a function to invoke', function test( t ) { |
67 |
| - var values; |
68 |
| - var i; |
69 |
| - |
70 |
| - values = [ |
71 |
| - '5', |
72 |
| - 5, |
73 |
| - NaN, |
74 |
| - true, |
75 |
| - false, |
76 |
| - null, |
77 |
| - void 0, |
78 |
| - {}, |
79 |
| - [], |
80 |
| - /.*/, |
81 |
| - new Date() |
82 |
| - ]; |
83 |
| - |
84 |
| - for ( i = 0; i < values.length; i++ ) { |
85 |
| - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); |
86 |
| - } |
87 |
| - t.end(); |
88 |
| - |
89 |
| - function badValue( value ) { |
90 |
| - return function badValue() { |
91 |
| - whileAsync( noop, value, noop ); |
92 |
| - }; |
93 |
| - } |
94 |
| -}); |
95 |
| - |
96 |
| -tape( 'the function throws an error if not provided a callback function', function test( t ) { |
97 |
| - var values; |
98 |
| - var i; |
99 |
| - |
100 |
| - values = [ |
101 |
| - '5', |
102 |
| - 5, |
103 |
| - NaN, |
104 |
| - true, |
105 |
| - false, |
106 |
| - null, |
107 |
| - void 0, |
108 |
| - {}, |
109 |
| - [], |
110 |
| - /.*/, |
111 |
| - new Date() |
112 |
| - ]; |
113 |
| - |
114 |
| - for ( i = 0; i < values.length; i++ ) { |
115 |
| - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); |
116 |
| - } |
117 |
| - t.end(); |
118 |
| - |
119 |
| - function badValue( value ) { |
120 |
| - return function badValue() { |
121 |
| - whileAsync( noop, noop, value ); |
122 |
| - }; |
123 |
| - } |
124 |
| -}); |
125 |
| - |
126 |
| -tape( 'while a test condition is true, the function invokes a provided function', function test( t ) { |
127 |
| - var indices1 = [ 0, 1, 2, 3, 4, 5 ]; |
128 |
| - var indices2 = [ 0, 1, 2, 3, 4 ]; |
129 |
| - var j = -1; |
130 |
| - var k = -1; |
131 |
| - |
132 |
| - whileAsync( predicate, fcn, done ); |
133 |
| - |
134 |
| - function predicate( i, clbk ) { |
135 |
| - j += 1; |
136 |
| - t.strictEqual( i, indices1[ j ], 'provides expected value' ); |
137 |
| - clbk( null, i < 5 ); |
138 |
| - } |
139 |
| - |
140 |
| - function fcn( i, next ) { |
141 |
| - k += 1; |
142 |
| - t.strictEqual( i, indices2[ k ], 'provides expected value' ); |
143 |
| - setTimeout( onTimeout, 0 ); |
144 |
| - function onTimeout() { |
145 |
| - next(); |
146 |
| - } |
147 |
| - } |
148 |
| - |
149 |
| - function done( error ) { |
150 |
| - if ( error ) { |
151 |
| - t.fail( error.message ); |
152 |
| - } else { |
153 |
| - t.ok( true, 'does not return an error' ); |
154 |
| - } |
155 |
| - t.end(); |
156 |
| - } |
157 |
| -}); |
158 |
| - |
159 |
| -tape( 'while a test condition is true, the function invokes a provided function (always false)', function test( t ) { |
160 |
| - var indices = [ 0 ]; |
161 |
| - var j = -1; |
162 |
| - |
163 |
| - whileAsync( predicate, fcn, done ); |
164 |
| - |
165 |
| - function predicate( i, clbk ) { |
166 |
| - j += 1; |
167 |
| - t.strictEqual( i, indices[ j ], 'provides expected value' ); |
168 |
| - clbk( null, false ); |
169 |
| - } |
170 |
| - |
171 |
| - function fcn( i, next ) { |
172 |
| - t.fail( 'should never be invoked' ); |
173 |
| - setTimeout( onTimeout, 0 ); |
174 |
| - function onTimeout() { |
175 |
| - next(); |
176 |
| - } |
177 |
| - } |
178 |
| - |
179 |
| - function done( error ) { |
180 |
| - if ( error ) { |
181 |
| - t.fail( error.message ); |
182 |
| - } else { |
183 |
| - t.ok( true, 'does not return an error' ); |
184 |
| - } |
185 |
| - t.end(); |
186 |
| - } |
187 |
| -}); |
188 |
| - |
189 |
| -tape( 'the function supports providing an execution context', function test( t ) { |
190 |
| - var ctx = { |
191 |
| - 'count': 0 |
192 |
| - }; |
193 |
| - |
194 |
| - whileAsync( predicate, fcn, done, ctx ); |
195 |
| - |
196 |
| - function predicate( i, clbk ) { |
197 |
| - clbk( null, i < 5 ); |
198 |
| - } |
199 |
| - |
200 |
| - function fcn( i, next ) { |
201 |
| - this.count += 1; // eslint-disable-line no-invalid-this |
202 |
| - setTimeout( onTimeout, 0 ); |
203 |
| - function onTimeout() { |
204 |
| - next(); |
205 |
| - } |
206 |
| - } |
207 |
| - |
208 |
| - function done( error ) { |
209 |
| - if ( error ) { |
210 |
| - t.fail( error.message ); |
211 |
| - } else { |
212 |
| - t.ok( true, 'does not return an error' ); |
213 |
| - } |
214 |
| - t.strictEqual( ctx.count, 5, 'updates context' ); |
215 |
| - t.end(); |
216 |
| - } |
217 |
| -}); |
218 |
| - |
219 |
| -tape( 'the function provides any results to a `done` callback', function test( t ) { |
220 |
| - whileAsync( predicate, fcn, done ); |
221 |
| - |
222 |
| - function predicate( i, clbk ) { |
223 |
| - clbk( null, i < 5 ); |
224 |
| - } |
225 |
| - |
226 |
| - function fcn( i, next ) { |
227 |
| - setTimeout( onTimeout, 0 ); |
228 |
| - function onTimeout() { |
229 |
| - next( null, 'beep'+i, 'boop'+i, 'woot'+i ); |
230 |
| - } |
231 |
| - } |
232 |
| - |
233 |
| - function done( error, str1, str2, str3 ) { |
234 |
| - if ( error ) { |
235 |
| - t.fail( error.message ); |
236 |
| - } else { |
237 |
| - t.ok( true, 'does not return an error' ); |
238 |
| - } |
239 |
| - t.strictEqual( str1, 'beep4', 'returns expected value' ); |
240 |
| - t.strictEqual( str2, 'boop4', 'returns expected value' ); |
241 |
| - t.strictEqual( str3, 'woot4', 'returns expected value' ); |
242 |
| - t.end(); |
243 |
| - } |
244 |
| -}); |
245 |
| - |
246 |
| -tape( 'if an error is encountered when invoking the predicate function, the function suspends execution and immediately returns the `error` to the provided callback', function test( t ) { |
247 |
| - var k = 0; |
248 |
| - whileAsync( predicate, fcn, done ); |
249 |
| - |
250 |
| - function predicate( i, clbk ) { |
251 |
| - k += 1; |
252 |
| - if ( i === 1 ) { |
253 |
| - return clbk( new Error( 'beep' ) ); |
254 |
| - } |
255 |
| - clbk( null, i < 5 ); |
256 |
| - } |
257 |
| - |
258 |
| - function fcn( i, next ) { |
259 |
| - setTimeout( onTimeout, 0 ); |
260 |
| - function onTimeout() { |
261 |
| - next(); |
262 |
| - } |
263 |
| - } |
264 |
| - |
265 |
| - function done( error ) { |
266 |
| - if ( error ) { |
267 |
| - t.pass( error.message ); |
268 |
| - } else { |
269 |
| - t.fail( 'should return an error' ); |
270 |
| - } |
271 |
| - t.strictEqual( k, 2, 'expected number of invocations' ); |
272 |
| - t.end(); |
273 |
| - } |
274 |
| -}); |
275 |
| - |
276 |
| -tape( 'if an error is encountered when invoking the provided function, the function suspends execution and immediately returns the `error` to the provided callback', function test( t ) { |
277 |
| - var k = 0; |
278 |
| - whileAsync( predicate, fcn, done ); |
279 |
| - |
280 |
| - function predicate( i, clbk ) { |
281 |
| - k += 1; |
282 |
| - clbk( null, i < 5 ); |
283 |
| - } |
284 |
| - |
285 |
| - function fcn( i, next ) { |
286 |
| - setTimeout( onTimeout, 0 ); |
287 |
| - function onTimeout() { |
288 |
| - if ( i === 2 ) { |
289 |
| - return next( new Error( 'boop' ) ); |
290 |
| - } |
291 |
| - next(); |
292 |
| - } |
293 |
| - } |
294 |
| - |
295 |
| - function done( error ) { |
296 |
| - if ( error ) { |
297 |
| - t.pass( error.message ); |
298 |
| - } else { |
299 |
| - t.fail( 'should return an error' ); |
300 |
| - } |
301 |
| - t.strictEqual( k, 3, 'expected number of invocations' ); |
302 |
| - t.end(); |
303 |
| - } |
304 |
| -}); |
305 |
| - |
306 |
| -tape( 'the function does not guarantee asynchronous execution', function test( t ) { |
307 |
| - var k = 0; |
308 |
| - whileAsync( predicate, fcn, done ); |
309 |
| - k = 1; |
310 |
| - |
311 |
| - function predicate( i, clbk ) { |
312 |
| - clbk( null, i < 5 ); |
313 |
| - } |
314 |
| - |
315 |
| - function fcn( i, next ) { |
316 |
| - next(); |
317 |
| - } |
318 |
| - |
319 |
| - function done( error ) { |
320 |
| - if ( error ) { |
321 |
| - t.fail( error.message ); |
322 |
| - } else { |
323 |
| - t.ok( true, 'does not return an error' ); |
324 |
| - } |
325 |
| - t.strictEqual( k, 0, 'releases the zalgo' ); |
326 |
| - t.end(); |
327 |
| - } |
328 |
| -}); |