229 lines
9.8 KiB
JavaScript
Executable File
229 lines
9.8 KiB
JavaScript
Executable File
/** PURE_IMPORTS_START .._Observable,.._Notification,._ColdObservable,._HotObservable,._SubscriptionLog,.._scheduler_VirtualTimeScheduler PURE_IMPORTS_END */
|
|
var __extends = (this && this.__extends) || function (d, b) {
|
|
for (var p in b)
|
|
if (b.hasOwnProperty(p))
|
|
d[p] = b[p];
|
|
function __() { this.constructor = d; }
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
};
|
|
import { Observable } from '../Observable';
|
|
import { Notification } from '../Notification';
|
|
import { ColdObservable } from './ColdObservable';
|
|
import { HotObservable } from './HotObservable';
|
|
import { SubscriptionLog } from './SubscriptionLog';
|
|
import { VirtualTimeScheduler, VirtualAction } from '../scheduler/VirtualTimeScheduler';
|
|
var defaultMaxFrame = 750;
|
|
export var TestScheduler = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
|
|
__extends(TestScheduler, _super);
|
|
function TestScheduler(assertDeepEqual) {
|
|
_super.call(this, VirtualAction, defaultMaxFrame);
|
|
this.assertDeepEqual = assertDeepEqual;
|
|
this.hotObservables = [];
|
|
this.coldObservables = [];
|
|
this.flushTests = [];
|
|
}
|
|
TestScheduler.prototype.createTime = function (marbles) {
|
|
var indexOf = marbles.indexOf('|');
|
|
if (indexOf === -1) {
|
|
throw new Error('marble diagram for time should have a completion marker "|"');
|
|
}
|
|
return indexOf * TestScheduler.frameTimeFactor;
|
|
};
|
|
TestScheduler.prototype.createColdObservable = function (marbles, values, error) {
|
|
if (marbles.indexOf('^') !== -1) {
|
|
throw new Error('cold observable cannot have subscription offset "^"');
|
|
}
|
|
if (marbles.indexOf('!') !== -1) {
|
|
throw new Error('cold observable cannot have unsubscription marker "!"');
|
|
}
|
|
var messages = TestScheduler.parseMarbles(marbles, values, error);
|
|
var cold = new ColdObservable(messages, this);
|
|
this.coldObservables.push(cold);
|
|
return cold;
|
|
};
|
|
TestScheduler.prototype.createHotObservable = function (marbles, values, error) {
|
|
if (marbles.indexOf('!') !== -1) {
|
|
throw new Error('hot observable cannot have unsubscription marker "!"');
|
|
}
|
|
var messages = TestScheduler.parseMarbles(marbles, values, error);
|
|
var subject = new HotObservable(messages, this);
|
|
this.hotObservables.push(subject);
|
|
return subject;
|
|
};
|
|
TestScheduler.prototype.materializeInnerObservable = function (observable, outerFrame) {
|
|
var _this = this;
|
|
var messages = [];
|
|
observable.subscribe(function (value) {
|
|
messages.push({ frame: _this.frame - outerFrame, notification: Notification.createNext(value) });
|
|
}, function (err) {
|
|
messages.push({ frame: _this.frame - outerFrame, notification: Notification.createError(err) });
|
|
}, function () {
|
|
messages.push({ frame: _this.frame - outerFrame, notification: Notification.createComplete() });
|
|
});
|
|
return messages;
|
|
};
|
|
TestScheduler.prototype.expectObservable = function (observable, unsubscriptionMarbles) {
|
|
var _this = this;
|
|
if (unsubscriptionMarbles === void 0) {
|
|
unsubscriptionMarbles = null;
|
|
}
|
|
var actual = [];
|
|
var flushTest = { actual: actual, ready: false };
|
|
var unsubscriptionFrame = TestScheduler
|
|
.parseMarblesAsSubscriptions(unsubscriptionMarbles).unsubscribedFrame;
|
|
var subscription;
|
|
this.schedule(function () {
|
|
subscription = observable.subscribe(function (x) {
|
|
var value = x;
|
|
// Support Observable-of-Observables
|
|
if (x instanceof Observable) {
|
|
value = _this.materializeInnerObservable(value, _this.frame);
|
|
}
|
|
actual.push({ frame: _this.frame, notification: Notification.createNext(value) });
|
|
}, function (err) {
|
|
actual.push({ frame: _this.frame, notification: Notification.createError(err) });
|
|
}, function () {
|
|
actual.push({ frame: _this.frame, notification: Notification.createComplete() });
|
|
});
|
|
}, 0);
|
|
if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
|
|
this.schedule(function () { return subscription.unsubscribe(); }, unsubscriptionFrame);
|
|
}
|
|
this.flushTests.push(flushTest);
|
|
return {
|
|
toBe: function (marbles, values, errorValue) {
|
|
flushTest.ready = true;
|
|
flushTest.expected = TestScheduler.parseMarbles(marbles, values, errorValue, true);
|
|
}
|
|
};
|
|
};
|
|
TestScheduler.prototype.expectSubscriptions = function (actualSubscriptionLogs) {
|
|
var flushTest = { actual: actualSubscriptionLogs, ready: false };
|
|
this.flushTests.push(flushTest);
|
|
return {
|
|
toBe: function (marbles) {
|
|
var marblesArray = (typeof marbles === 'string') ? [marbles] : marbles;
|
|
flushTest.ready = true;
|
|
flushTest.expected = marblesArray.map(function (marbles) {
|
|
return TestScheduler.parseMarblesAsSubscriptions(marbles);
|
|
});
|
|
}
|
|
};
|
|
};
|
|
TestScheduler.prototype.flush = function () {
|
|
var hotObservables = this.hotObservables;
|
|
while (hotObservables.length > 0) {
|
|
hotObservables.shift().setup();
|
|
}
|
|
_super.prototype.flush.call(this);
|
|
var readyFlushTests = this.flushTests.filter(function (test) { return test.ready; });
|
|
while (readyFlushTests.length > 0) {
|
|
var test = readyFlushTests.shift();
|
|
this.assertDeepEqual(test.actual, test.expected);
|
|
}
|
|
};
|
|
TestScheduler.parseMarblesAsSubscriptions = function (marbles) {
|
|
if (typeof marbles !== 'string') {
|
|
return new SubscriptionLog(Number.POSITIVE_INFINITY);
|
|
}
|
|
var len = marbles.length;
|
|
var groupStart = -1;
|
|
var subscriptionFrame = Number.POSITIVE_INFINITY;
|
|
var unsubscriptionFrame = Number.POSITIVE_INFINITY;
|
|
for (var i = 0; i < len; i++) {
|
|
var frame = i * this.frameTimeFactor;
|
|
var c = marbles[i];
|
|
switch (c) {
|
|
case '-':
|
|
case ' ':
|
|
break;
|
|
case '(':
|
|
groupStart = frame;
|
|
break;
|
|
case ')':
|
|
groupStart = -1;
|
|
break;
|
|
case '^':
|
|
if (subscriptionFrame !== Number.POSITIVE_INFINITY) {
|
|
throw new Error('found a second subscription point \'^\' in a ' +
|
|
'subscription marble diagram. There can only be one.');
|
|
}
|
|
subscriptionFrame = groupStart > -1 ? groupStart : frame;
|
|
break;
|
|
case '!':
|
|
if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
|
|
throw new Error('found a second subscription point \'^\' in a ' +
|
|
'subscription marble diagram. There can only be one.');
|
|
}
|
|
unsubscriptionFrame = groupStart > -1 ? groupStart : frame;
|
|
break;
|
|
default:
|
|
throw new Error('there can only be \'^\' and \'!\' markers in a ' +
|
|
'subscription marble diagram. Found instead \'' + c + '\'.');
|
|
}
|
|
}
|
|
if (unsubscriptionFrame < 0) {
|
|
return new SubscriptionLog(subscriptionFrame);
|
|
}
|
|
else {
|
|
return new SubscriptionLog(subscriptionFrame, unsubscriptionFrame);
|
|
}
|
|
};
|
|
TestScheduler.parseMarbles = function (marbles, values, errorValue, materializeInnerObservables) {
|
|
if (materializeInnerObservables === void 0) {
|
|
materializeInnerObservables = false;
|
|
}
|
|
if (marbles.indexOf('!') !== -1) {
|
|
throw new Error('conventional marble diagrams cannot have the ' +
|
|
'unsubscription marker "!"');
|
|
}
|
|
var len = marbles.length;
|
|
var testMessages = [];
|
|
var subIndex = marbles.indexOf('^');
|
|
var frameOffset = subIndex === -1 ? 0 : (subIndex * -this.frameTimeFactor);
|
|
var getValue = typeof values !== 'object' ?
|
|
function (x) { return x; } :
|
|
function (x) {
|
|
// Support Observable-of-Observables
|
|
if (materializeInnerObservables && values[x] instanceof ColdObservable) {
|
|
return values[x].messages;
|
|
}
|
|
return values[x];
|
|
};
|
|
var groupStart = -1;
|
|
for (var i = 0; i < len; i++) {
|
|
var frame = i * this.frameTimeFactor + frameOffset;
|
|
var notification = void 0;
|
|
var c = marbles[i];
|
|
switch (c) {
|
|
case '-':
|
|
case ' ':
|
|
break;
|
|
case '(':
|
|
groupStart = frame;
|
|
break;
|
|
case ')':
|
|
groupStart = -1;
|
|
break;
|
|
case '|':
|
|
notification = Notification.createComplete();
|
|
break;
|
|
case '^':
|
|
break;
|
|
case '#':
|
|
notification = Notification.createError(errorValue || 'error');
|
|
break;
|
|
default:
|
|
notification = Notification.createNext(getValue(c));
|
|
break;
|
|
}
|
|
if (notification) {
|
|
testMessages.push({ frame: groupStart > -1 ? groupStart : frame, notification: notification });
|
|
}
|
|
}
|
|
return testMessages;
|
|
};
|
|
return TestScheduler;
|
|
}(VirtualTimeScheduler));
|
|
//# sourceMappingURL=TestScheduler.js.map
|