Skip to content

Commit a3cd3a7

Browse files
authored
Merge pull request #1 from nobitlost/develop
CSE-344: Don't bundle the Promise library - Test Framework Improvements
2 parents ae0eb5b + 1407b3b commit a3cd3a7

9 files changed

+214
-359
lines changed

Diff for: dist/impUnit.nut

+32-176
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
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+
126
/**
227
* impUnit Test Framework
328
*
429
* @author Mikhail Yurasov <mikhail@electricimp.com>
5-
* @version 0.5.0
30+
* @version 1.0.0
631
* @package ImpUnit
732
*/
833

@@ -200,178 +225,8 @@ function __module_ImpUnit_JSONEncoder() {
200225
return exports;
201226
}
202227

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-
373228
// impUnit module
374-
function __module_impUnit(Promise, JSONEncoder) {
229+
function __module_impUnit(JSONEncoder) {
375230
/**
376231
* Message handling
377232
* @package ImpUnit
@@ -846,8 +701,9 @@ local ImpUnitRunner = class {
846701

847702
test.timedOut <- false;
848703

704+
local isPending = true;
849705
test.timerId <- imp.wakeup(this.timeout, function () {
850-
if (test.result.isPending()) {
706+
if (isPending) {
851707
test.timedOut = true;
852708

853709
// update assertions counter to ignore assertions afrer the timeout
@@ -863,11 +719,13 @@ local ImpUnitRunner = class {
863719

864720
// we're fine
865721
.then(function (message) {
722+
isPending = false;
866723
test.message <- message;
867724
})
868725

869726
// we're screwed
870727
.fail(function (error) {
728+
isPending = false;
871729
test.error <- error;
872730
})
873731

@@ -920,12 +778,10 @@ local ImpUnitRunner = class {
920778
}
921779
922780
// resolve modules
923-
__module_ImpUnit_Promise_exports <- __module_ImpUnit_Promise();
924781
__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);
926783
927784
// add symbols to root scope for old-scool usage
928-
Promise <- __module_ImpUnit_Promise_exports;
929785
ImpTestCase <- __module_impUnit_exports.ImpTestCase;
930786
ImpUnitRunner <- __module_impUnit_exports.ImpUnitRunner;
931787

Diff for: example.nut

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
// #include "dist/impUnit.nut"
1+
// MIT License
2+
//
3+
// Copyright 2016-2017 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
25+
@include "dist/impUnit.nut"
226

327
class SampleTestCase extends ImpTestCase {
428
function testSomethingSync() {

Diff for: index.nut

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
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+
126
/**
227
* impUnit Test Framework
328
*
429
* @author Mikhail Yurasov <mikhail@electricimp.com>
5-
* @version 0.5.0
30+
* @version 1.0.0
631
* @package ImpUnit
732
*/
833

934
// libs required by impUnit
1035

11-
// #include "lib/JSONEncoder.mod.nut"
12-
13-
// #include "lib/Promise.mod.nut"
36+
@include __PATH__+"/lib/JSONEncoder.mod.nut"
1437

1538
// impUnit module
16-
function __module_impUnit(Promise, JSONEncoder) {
17-
// #include "src/ImpUnit.nut"
39+
function __module_impUnit(JSONEncoder) {
40+
@include __PATH__+"/src/ImpUnit.nut"
1841
// export symbols
1942
return {
2043
"ImpTestCase" : ImpTestCase,
@@ -23,11 +46,9 @@ function __module_impUnit(Promise, JSONEncoder) {
2346
}
2447

2548
// resolve modules
26-
__module_ImpUnit_Promise_exports <- __module_ImpUnit_Promise();
2749
__module_ImpUnit_JSONEncoder_exports <- __module_ImpUnit_JSONEncoder();
28-
__module_impUnit_exports <- __module_impUnit(__module_ImpUnit_Promise_exports, __module_ImpUnit_JSONEncoder_exports);
50+
__module_impUnit_exports <- __module_impUnit(__module_ImpUnit_JSONEncoder_exports);
2951

3052
// add symbols to root scope for old-scool usage
31-
Promise <- __module_ImpUnit_Promise_exports;
3253
ImpTestCase <- __module_impUnit_exports.ImpTestCase;
3354
ImpUnitRunner <- __module_impUnit_exports.ImpUnitRunner;

Diff for: lib/JSONEncoder.mod.nut

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
// MIT License
2+
//
3+
// Copyright 2016 Electric Imp
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
20+
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21+
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
125
/**
226
* JSON encoder
327
*

0 commit comments

Comments
 (0)