2016-05-20 19:42:26 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
var common = parent.__common;
|
2016-05-26 01:28:20 +00:00
|
|
|
var frameId = window.__frameId;
|
|
|
|
var frameReady = common[frameId + 'Ready$'] || { onNext() {} };
|
|
|
|
var Rx = document.Rx;
|
2016-09-12 20:21:41 +00:00
|
|
|
var helpers = Rx.helpers;
|
2016-05-26 01:28:20 +00:00
|
|
|
var chai = parent.chai;
|
|
|
|
var source = document.__source;
|
2016-05-20 19:42:26 +00:00
|
|
|
|
2016-05-26 01:28:20 +00:00
|
|
|
document.__getJsOutput = function getJsOutput() {
|
2016-05-20 19:42:26 +00:00
|
|
|
if (window.__err || !common.shouldRun()) {
|
|
|
|
return window.__err || 'source disabled';
|
|
|
|
}
|
|
|
|
let output;
|
|
|
|
try {
|
|
|
|
/* eslint-disable no-eval */
|
|
|
|
output = eval(source);
|
|
|
|
/* eslint-enable no-eval */
|
|
|
|
} catch (e) {
|
2016-05-28 00:11:25 +00:00
|
|
|
output = e.message + '\n' + e.stack;
|
2016-05-20 19:42:26 +00:00
|
|
|
window.__err = e;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
2016-05-26 01:28:20 +00:00
|
|
|
document.__runTests$ = function runTests$(tests = []) {
|
|
|
|
/* eslint-disable no-unused-vars */
|
2016-05-20 19:42:26 +00:00
|
|
|
const editor = { getValue() { return source; } };
|
2016-05-26 01:28:20 +00:00
|
|
|
const code = source;
|
|
|
|
/* eslint-enable no-unused-vars */
|
2016-05-20 19:42:26 +00:00
|
|
|
if (window.__err) {
|
|
|
|
return Rx.Observable.throw(window.__err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the test one at a time
|
|
|
|
// on new stacks
|
|
|
|
return Rx.Observable.from(tests, null, null, Rx.Scheduler.default)
|
|
|
|
// add delay here for firefox to catch up
|
2016-05-26 01:28:20 +00:00
|
|
|
.delay(200)
|
|
|
|
/* eslint-disable no-unused-vars */
|
2016-09-12 20:21:41 +00:00
|
|
|
.flatMap(({ text, testString }) => {
|
2016-05-26 01:28:20 +00:00
|
|
|
const assert = chai.assert;
|
|
|
|
/* eslint-enable no-unused-vars */
|
2016-05-20 19:42:26 +00:00
|
|
|
const newTest = { text, testString };
|
|
|
|
let test;
|
2016-09-12 20:21:41 +00:00
|
|
|
let __result;
|
2016-05-20 19:42:26 +00:00
|
|
|
try {
|
|
|
|
/* eslint-disable no-eval */
|
2016-09-12 20:21:41 +00:00
|
|
|
// eval test string to actual JavaScript
|
|
|
|
// This return can be a function
|
|
|
|
// i.e. function() { assert(true, 'happy coding'); }
|
2016-05-20 19:42:26 +00:00
|
|
|
test = eval(testString);
|
|
|
|
/* eslint-enable no-eval */
|
|
|
|
if (typeof test === 'function') {
|
2016-09-12 20:21:41 +00:00
|
|
|
|
|
|
|
// we know that the test eval'ed to a function
|
|
|
|
// the function could expect a callback
|
|
|
|
// or it could return a promise/observable
|
|
|
|
// or it could still be sync
|
2016-05-20 19:42:26 +00:00
|
|
|
if (test.length === 0) {
|
2016-09-12 20:21:41 +00:00
|
|
|
// a function with length 0 means it expects 0 args
|
|
|
|
// We call it and store the result
|
|
|
|
// This result may be a promise or an observable or undefined
|
|
|
|
__result = test();
|
|
|
|
} else {
|
|
|
|
// if function takes arguments
|
|
|
|
// we expect it to be of the form
|
|
|
|
// function(cb) { /* ... */ }
|
|
|
|
// and callback has the following signature
|
|
|
|
// function(err) { /* ... */ }
|
|
|
|
__result = Rx.Observable.fromNodeCallback(test)();
|
2016-05-20 19:42:26 +00:00
|
|
|
}
|
2016-09-12 20:21:41 +00:00
|
|
|
|
|
|
|
if (helpers.isPromise(__result)) {
|
|
|
|
// turn promise into an observable
|
|
|
|
__result = Rx.Observable.fromPromise(__result);
|
2016-05-20 19:42:26 +00:00
|
|
|
}
|
2016-09-12 20:21:41 +00:00
|
|
|
} else {
|
|
|
|
// test is not a function
|
|
|
|
// fill result with for compatibility
|
|
|
|
__result = Rx.Observable.of(null);
|
2016-05-20 19:42:26 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2016-09-12 20:21:41 +00:00
|
|
|
// something threw an uncaught error
|
|
|
|
// we catch here and wrap it in an observable
|
|
|
|
__result = Rx.Observable.throw(e);
|
2016-05-20 19:42:26 +00:00
|
|
|
}
|
2016-09-12 20:21:41 +00:00
|
|
|
return __result
|
|
|
|
.map(() => {
|
|
|
|
// we don't need the result of a promise/observable/cb here
|
|
|
|
// all data asserts should happen further up the chain
|
|
|
|
// mark test as passing
|
|
|
|
newTest.pass = true;
|
|
|
|
return newTest;
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
// we catch the error here to prevent the error from bubbling up
|
|
|
|
// and collapsing the pipe
|
|
|
|
newTest.err = err.message + '\n' + err.stack;
|
|
|
|
// RxJS catch expects an observable as a return
|
2016-10-04 05:33:54 +00:00
|
|
|
return Rx.Observable.of(newTest);
|
2016-09-12 20:21:41 +00:00
|
|
|
});
|
2016-05-20 19:42:26 +00:00
|
|
|
})
|
|
|
|
// gather tests back into an array
|
|
|
|
.toArray();
|
|
|
|
};
|
|
|
|
|
2016-05-28 00:11:25 +00:00
|
|
|
// notify that the window methods are ready to run
|
2016-05-26 01:28:20 +00:00
|
|
|
frameReady.onNext(null);
|
2016-05-20 19:42:26 +00:00
|
|
|
});
|