You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
9.9 KiB
223 lines
9.9 KiB
2 years ago
|
"use strict";
|
||
|
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 __());
|
||
|
};
|
||
|
var Observable_1 = require('../Observable');
|
||
|
var Notification_1 = require('../Notification');
|
||
|
var ColdObservable_1 = require('./ColdObservable');
|
||
|
var HotObservable_1 = require('./HotObservable');
|
||
|
var SubscriptionLog_1 = require('./SubscriptionLog');
|
||
|
var VirtualTimeScheduler_1 = require('../scheduler/VirtualTimeScheduler');
|
||
|
var defaultMaxFrame = 750;
|
||
|
var TestScheduler = (function (_super) {
|
||
|
__extends(TestScheduler, _super);
|
||
|
function TestScheduler(assertDeepEqual) {
|
||
|
_super.call(this, VirtualTimeScheduler_1.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_1.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_1.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_1.Notification.createNext(value) });
|
||
|
}, function (err) {
|
||
|
messages.push({ frame: _this.frame - outerFrame, notification: Notification_1.Notification.createError(err) });
|
||
|
}, function () {
|
||
|
messages.push({ frame: _this.frame - outerFrame, notification: Notification_1.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_1.Observable) {
|
||
|
value = _this.materializeInnerObservable(value, _this.frame);
|
||
|
}
|
||
|
actual.push({ frame: _this.frame, notification: Notification_1.Notification.createNext(value) });
|
||
|
}, function (err) {
|
||
|
actual.push({ frame: _this.frame, notification: Notification_1.Notification.createError(err) });
|
||
|
}, function () {
|
||
|
actual.push({ frame: _this.frame, notification: Notification_1.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_1.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_1.SubscriptionLog(subscriptionFrame);
|
||
|
}
|
||
|
else {
|
||
|
return new SubscriptionLog_1.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_1.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_1.Notification.createComplete();
|
||
|
break;
|
||
|
case '^':
|
||
|
break;
|
||
|
case '#':
|
||
|
notification = Notification_1.Notification.createError(errorValue || 'error');
|
||
|
break;
|
||
|
default:
|
||
|
notification = Notification_1.Notification.createNext(getValue(c));
|
||
|
break;
|
||
|
}
|
||
|
if (notification) {
|
||
|
testMessages.push({ frame: groupStart > -1 ? groupStart : frame, notification: notification });
|
||
|
}
|
||
|
}
|
||
|
return testMessages;
|
||
|
};
|
||
|
return TestScheduler;
|
||
|
}(VirtualTimeScheduler_1.VirtualTimeScheduler));
|
||
|
exports.TestScheduler = TestScheduler;
|
||
|
//# sourceMappingURL=TestScheduler.js.map
|