1
+ # require "promise.class.nut:3.0.0"
2
+ // MIT License
3
+ //
4
+ // Copyright 2016-2017 Electric Imp
5
+ //
6
+ // SPDX-License-Identifier: MIT
7
+ //
8
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ // of this software and associated documentation files (the "Software"), to deal
10
+ // in the Software without restriction, including without limitation the rights
11
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ // copies of the Software, and to permit persons to whom the Software is
13
+ // furnished to do so, subject to the following conditions:
14
+ //
15
+ // The above copyright notice and this permission notice shall be
16
+ // included in all copies or substantial portions of the Software.
17
+ //
18
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
21
+ // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
22
+ // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23
+ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ // OTHER DEALINGS IN THE SOFTWARE.
25
+
1
26
/* *
2
27
* impUnit Test Framework
3
28
*
4
29
* @author Mikhail Yurasov <mikhail@electricimp.com>
5
- * @version 0.5 .0
30
+ * @version 1.0 .0
6
31
* @package ImpUnit
7
32
*/
8
33
@@ -200,178 +225,8 @@ function __module_ImpUnit_JSONEncoder() {
200
225
return exports;
201
226
}
202
227
203
- /* *
204
- * Promise class for Squirrel (Electric Imp)
205
- * This file is licensed under the MIT License
206
- *
207
- * Initial version: 08-12-2015
208
- *
209
- * @see https://www.promisejs.org/implementing/
210
- *
211
- * @copyright (c) 2015 SMS Diagnostics Pty Ltd
212
- * @author Aron Steg
213
- * @author Mikhail Yurasov <mikhail@electricimp.com>
214
- * @version 1.1.0-impUnit
215
- *
216
- * impUnit chages:
217
- * - packaging as module
218
- * - isPendinng()
219
- */
220
- function __module_ImpUnit_Promise () {
221
- local exports = class {
222
-
223
- static version = [1 , 1 , 0 , " impUnit" ];
224
-
225
- _state = null ;
226
- _value = null ;
227
- _handlers = null ;
228
-
229
- constructor (fn) {
230
-
231
- const PROMISE_STATE_PENDING = 0 ;
232
- const PROMISE_STATE_FULFILLED = 1 ;
233
- const PROMISE_STATE_REJECTED = 2 ;
234
-
235
- _state = PROMISE_STATE_PENDING;
236
- _handlers = [];
237
- _doResolve (fn, _resolve, _reject);
238
- }
239
-
240
- // **** Private functions ****
241
-
242
- function _fulfill (result) {
243
- _state = PROMISE_STATE_FULFILLED;
244
- _value = result;
245
- foreach (handler in _handlers) {
246
- _handle (handler);
247
- }
248
- _handlers = null ;
249
- }
250
-
251
- function _reject (error) {
252
- _state = PROMISE_STATE_REJECTED;
253
- _value = error;
254
- foreach (handler in _handlers) {
255
- _handle (handler);
256
- }
257
- _handlers = null ;
258
- }
259
-
260
- function _resolve (result) {
261
- try {
262
- local then = _getThen (result);
263
- if (then) {
264
- _doResolve (then. bindenv (result), _resolve, _reject);
265
- return ;
266
- }
267
- _fulfill (result);
268
- } catch (e) {
269
- _reject (e);
270
- }
271
- }
272
-
273
- /* *
274
- * Check if a value is a Promise and, if it is,
275
- * return the `then` method of that promise.
276
- *
277
- * @param {Promise|*} value
278
- * @return {function|null}
279
- */
280
- function _getThen (value) {
281
-
282
- if (
283
- // detect that the value is some form of Promise
284
- // by the fact it has .then() method
285
- (typeof value == " instance" )
286
- && (" then" in value)
287
- && (typeof value. then == " function" )
288
- ) {
289
- return value. then ;
290
- }
291
-
292
- return null ;
293
- }
294
-
295
- function _doResolve (fn, onFulfilled, onRejected) {
296
- local done = false ;
297
- try {
298
- fn (
299
- function (value = null /* allow resolving without argument */ ) {
300
- if (done) return ;
301
- done = true ;
302
- onFulfilled (value)
303
- }. bindenv (this ),
304
-
305
- function (reason = null /* allow rejection without argument */ ) {
306
- if (done) return ;
307
- done = true ;
308
- onRejected (reason)
309
- }. bindenv (this )
310
- )
311
- } catch (ex) {
312
- if (done) return ;
313
- done = true ;
314
- onRejected (ex);
315
- }
316
- }
317
-
318
- function _handle (handler) {
319
- if (_state == PROMISE_STATE_PENDING) {
320
- _handlers. push (handler);
321
- } else {
322
- if (_state == PROMISE_STATE_FULFILLED && typeof handler. onFulfilled == " function" ) {
323
- handler. onFulfilled (_value);
324
- }
325
- if (_state == PROMISE_STATE_REJECTED && typeof handler. onRejected == " function" ) {
326
- handler. onRejected (_value);
327
- }
328
- }
329
- }
330
-
331
- // **** Public functions ****
332
-
333
- /* *
334
- * Execute handler once the Promise is resolved/rejected
335
- * @param {function|null} onFulfilled
336
- * @param {function|null} onRejected
337
- */
338
- function then (onFulfilled = null , onRejected = null ) {
339
- // ensure we are always asynchronous
340
- imp. wakeup (0 , function () {
341
- _handle ({ onFulfilled= onFulfilled, onRejected= onRejected });
342
- }. bindenv (this ));
343
-
344
- return this ;
345
- }
346
-
347
- /* *
348
- * Execute handler on failure
349
- * @param {function|null} onRejected
350
- */
351
- function fail (onRejected = null ) {
352
- return then (null , onRejected);
353
- }
354
-
355
- /* *
356
- * Execute handler both on success and failure
357
- * @param {function|null} always
358
- */
359
- function finally (always = null ) {
360
- return then (always, always);
361
- }
362
-
363
- // impUnit additions
364
-
365
- function isPending () {
366
- return this . _state == PROMISE_STATE_PENDING;
367
- }
368
- }
369
-
370
- return exports;
371
- }
372
-
373
228
// impUnit module
374
- function __module_impUnit (Promise, JSONEncoder) {
229
+ function __module_impUnit (JSONEncoder) {
375
230
/* *
376
231
* Message handling
377
232
* @package ImpUnit
@@ -846,8 +701,9 @@ local ImpUnitRunner = class {
846
701
847
702
test. timedOut <- false;
848
703
704
+ local isPending = true ;
849
705
test. timerId <- imp.wakeup(this . timeout , function () {
850
- if (test . result . isPending () ) {
706
+ if (isPending) {
851
707
test. timedOut = true ;
852
708
853
709
// update assertions counter to ignore assertions afrer the timeout
@@ -863,11 +719,13 @@ local ImpUnitRunner = class {
863
719
864
720
// we're fine
865
721
. then (function (message) {
722
+ isPending = false ;
866
723
test. message <- message;
867
724
})
868
725
869
726
// we're screwed
870
727
. fail (function (error) {
728
+ isPending = false ;
871
729
test. error <- error;
872
730
})
873
731
@@ -920,12 +778,10 @@ local ImpUnitRunner = class {
920
778
}
921
779
922
780
// resolve modules
923
- __module_ImpUnit_Promise_exports <- __module_ImpUnit_Promise();
924
781
__module_ImpUnit_JSONEncoder_exports <- __module_ImpUnit_JSONEncoder();
925
- __module_impUnit_exports <- __module_impUnit(__module_ImpUnit_Promise_exports, __module_ImpUnit_JSONEncoder_exports);
782
+ __module_impUnit_exports <- __module_impUnit(__module_ImpUnit_JSONEncoder_exports);
926
783
927
784
// add symbols to root scope for old-scool usage
928
- Promise <- __module_ImpUnit_Promise_exports;
929
785
ImpTestCase <- __module_impUnit_exports.ImpTestCase;
930
786
ImpUnitRunner <- __module_impUnit_exports.ImpUnitRunner;
931
787
0 commit comments