|
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 funseqAsync = 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 funseqAsync, '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 multiple functions to execute sequentially', function test( t ) { |
37 | | - t.throws( foo, Error, 'throws an error' ); |
38 | | - t.throws( bar, Error, 'throws an error' ); |
39 | | - t.end(); |
40 | | - |
41 | | - function foo() { |
42 | | - funseqAsync(); |
43 | | - } |
44 | | - |
45 | | - function bar() { |
46 | | - funseqAsync( noop ); |
47 | | - } |
48 | | -}); |
49 | | - |
50 | | -tape( 'the function throws an error if not provided a function', function test( t ) { |
51 | | - var values; |
52 | | - var i; |
53 | | - |
54 | | - values = [ |
55 | | - '5', |
56 | | - 5, |
57 | | - NaN, |
58 | | - null, |
59 | | - void 0, |
60 | | - true, |
61 | | - [], |
62 | | - {} |
63 | | - ]; |
64 | | - |
65 | | - for ( i = 0; i < values.length; i++ ) { |
66 | | - t.throws( badValue1( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
67 | | - t.throws( badValue2( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
68 | | - t.throws( badValue3( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
69 | | - } |
70 | | - t.end(); |
71 | | - |
72 | | - function badValue1( value ) { |
73 | | - return function badValue() { |
74 | | - funseqAsync( value, noop ); |
75 | | - }; |
76 | | - } |
77 | | - |
78 | | - function badValue2( value ) { |
79 | | - return function badValue() { |
80 | | - funseqAsync( noop, value ); |
81 | | - }; |
82 | | - } |
83 | | - |
84 | | - function badValue3( value ) { |
85 | | - return function badValue() { |
86 | | - funseqAsync( noop, noop, value ); |
87 | | - }; |
88 | | - } |
89 | | -}); |
90 | | - |
91 | | -tape( 'the function returns a function', function test( t ) { |
92 | | - var fcn = funseqAsync( noop, noop ); |
93 | | - t.strictEqual( typeof fcn, 'function', 'returns a function' ); |
94 | | - t.end(); |
95 | | -}); |
96 | | - |
97 | | -tape( 'the function returns a pipeline function', function test( t ) { |
98 | | - var f; |
99 | | - |
100 | | - function a( x, next ) { |
101 | | - setTimeout( onTimeout, 0 ); |
102 | | - function onTimeout() { |
103 | | - next( null, x*3 ); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - function b( z, next ) { |
108 | | - setTimeout( onTimeout, 0 ); |
109 | | - function onTimeout() { |
110 | | - next( null, z+5 ); |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - function c( r, next ) { |
115 | | - setTimeout( onTimeout, 0 ); |
116 | | - function onTimeout() { |
117 | | - next( null, r/10 ); |
118 | | - } |
119 | | - } |
120 | | - |
121 | | - f = funseqAsync( a, b, c ); |
122 | | - f( 5, done ); |
123 | | - |
124 | | - function done( error, v ) { |
125 | | - if ( error ) { |
126 | | - t.fail( error.message ); |
127 | | - } else { |
128 | | - t.strictEqual( v, 2, 'returns pipeline result' ); |
129 | | - } |
130 | | - t.end(); |
131 | | - } |
132 | | -}); |
133 | | - |
134 | | -tape( 'the function supports providing a leftmost multi-parameter function', function test( t ) { |
135 | | - var f; |
136 | | - |
137 | | - function a( x, y, next ) { |
138 | | - setTimeout( onTimeout, 0 ); |
139 | | - function onTimeout() { |
140 | | - next( null, x*y ); |
141 | | - } |
142 | | - } |
143 | | - |
144 | | - function b( z, next ) { |
145 | | - setTimeout( onTimeout, 0 ); |
146 | | - function onTimeout() { |
147 | | - next( null, z+5 ); |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - function c( r, next ) { |
152 | | - setTimeout( onTimeout, 0 ); |
153 | | - function onTimeout() { |
154 | | - next( null, r/10 ); |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - f = funseqAsync( a, b, c ); |
159 | | - f( 5, 3, done ); |
160 | | - |
161 | | - function done( error, v ) { |
162 | | - if ( error ) { |
163 | | - t.fail( error.message ); |
164 | | - } else { |
165 | | - t.strictEqual( v, 2, 'returns pipeline result' ); |
166 | | - } |
167 | | - t.end(); |
168 | | - } |
169 | | -}); |
170 | | - |
171 | | -tape( 'if an error is encountered while invoking a provided function, the pipeline function suspends execution and immediately returns the error', function test( t ) { |
172 | | - var count; |
173 | | - var f; |
174 | | - |
175 | | - function a( x, next ) { |
176 | | - setTimeout( onTimeout, 0 ); |
177 | | - function onTimeout() { |
178 | | - count += 1; |
179 | | - next( new Error( 'beep' ) ); |
180 | | - } |
181 | | - } |
182 | | - |
183 | | - function b( z, next ) { |
184 | | - setTimeout( onTimeout, 0 ); |
185 | | - function onTimeout() { |
186 | | - count += 1; |
187 | | - next( null, z+5 ); |
188 | | - } |
189 | | - } |
190 | | - |
191 | | - function c( r, next ) { |
192 | | - setTimeout( onTimeout, 0 ); |
193 | | - function onTimeout() { |
194 | | - count += 1; |
195 | | - next( null, r/10 ); |
196 | | - } |
197 | | - } |
198 | | - |
199 | | - f = funseqAsync( a, b, c ); |
200 | | - count = 0; |
201 | | - f( 5, done ); |
202 | | - |
203 | | - function done( error ) { |
204 | | - if ( error ) { |
205 | | - t.pass( error.message ); |
206 | | - } else { |
207 | | - t.fail( 'should return an error' ); |
208 | | - } |
209 | | - t.strictEqual( count, 1, 'invokes expected number of functions' ); |
210 | | - t.end(); |
211 | | - } |
212 | | -}); |
213 | | - |
214 | | -tape( 'if an error is encountered while invoking a provided function, the pipeline function suspends execution and immediately returns the error', function test( t ) { |
215 | | - var count; |
216 | | - var f; |
217 | | - |
218 | | - function a( x, next ) { |
219 | | - setTimeout( onTimeout, 0 ); |
220 | | - function onTimeout() { |
221 | | - count += 1; |
222 | | - next( null, x*3 ); |
223 | | - } |
224 | | - } |
225 | | - |
226 | | - function b( z, next ) { |
227 | | - setTimeout( onTimeout, 0 ); |
228 | | - function onTimeout() { |
229 | | - count += 1; |
230 | | - next( new Error( 'beep' ) ); |
231 | | - } |
232 | | - } |
233 | | - |
234 | | - function c( r, next ) { |
235 | | - setTimeout( onTimeout, 0 ); |
236 | | - function onTimeout() { |
237 | | - count += 1; |
238 | | - next( null, r/10 ); |
239 | | - } |
240 | | - } |
241 | | - |
242 | | - f = funseqAsync( a, b, c ); |
243 | | - count = 0; |
244 | | - f( 5, done ); |
245 | | - |
246 | | - function done( error ) { |
247 | | - if ( error ) { |
248 | | - t.pass( error.message ); |
249 | | - } else { |
250 | | - t.fail( 'should return an error' ); |
251 | | - } |
252 | | - t.strictEqual( count, 2, 'invokes expected number of functions' ); |
253 | | - t.end(); |
254 | | - } |
255 | | -}); |
256 | | - |
257 | | -tape( 'if an error is encountered while invoking a provided function, the pipeline function suspends execution and immediately returns the error', function test( t ) { |
258 | | - var count; |
259 | | - var f; |
260 | | - |
261 | | - function a( x, next ) { |
262 | | - setTimeout( onTimeout, 0 ); |
263 | | - function onTimeout() { |
264 | | - count += 1; |
265 | | - next( null, x*3 ); |
266 | | - } |
267 | | - } |
268 | | - |
269 | | - function b( z, next ) { |
270 | | - setTimeout( onTimeout, 0 ); |
271 | | - function onTimeout() { |
272 | | - count += 1; |
273 | | - next( null, z+5 ); |
274 | | - } |
275 | | - } |
276 | | - |
277 | | - function c( r, next ) { |
278 | | - setTimeout( onTimeout, 0 ); |
279 | | - function onTimeout() { |
280 | | - count += 1; |
281 | | - next( new Error( 'beep' ) ); |
282 | | - } |
283 | | - } |
284 | | - |
285 | | - f = funseqAsync( a, b, c ); |
286 | | - count = 0; |
287 | | - f( 5, done ); |
288 | | - |
289 | | - function done( error ) { |
290 | | - if ( error ) { |
291 | | - t.pass( error.message ); |
292 | | - } else { |
293 | | - t.fail( 'should return an error' ); |
294 | | - } |
295 | | - t.strictEqual( count, 3, 'invokes expected number of functions' ); |
296 | | - t.end(); |
297 | | - } |
298 | | -}); |
0 commit comments