1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 | 1
1
1
1
1
1
1
1
1
1
1
1
1
1681
1681
1681
1681
1681
1
1287
1287
1287
1287
1287
1287
1
1418
1418
1418
1418
1418
1
175
175
175
326
44
122
175
1
1558
1558
1558
1558
1558
1558
1558
1558
3551
3551
175
3551
459
729
1558
137
1558
1558
1556
1556
1
1978
1978
1978
1976
1976
1976
2054
1
1694
1694
1418
1694
3536
1690
3532
3532
1
3635
1421
2214
2214
2214
2214
2214
40275
40275
40275
35645
285
285
285
285
2068
2068
2068
1992
1992
1992
2214
2214
1
4296
4296
4296
4296
4296
4296
4296
7846
2357
175
2182
4296
4296
68212
68212
68212
68212
52673
603
603
603
603
2653
2653
539
938
938
10203
10203
68212
13794
4296
1
1
| var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) Eif (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 VirtualTimeScheduler_1 = require('../scheduler/VirtualTimeScheduler');
var Notification_1 = require('../Notification');
var ColdObservable_1 = require('./ColdObservable');
var HotObservable_1 = require('./HotObservable');
var SubscriptionLog_1 = require('./SubscriptionLog');
var TestScheduler = (function (_super) {
__extends(TestScheduler, _super);
function TestScheduler(assertDeepEqual) {
_super.call(this);
this.assertDeepEqual = assertDeepEqual;
this.hotObservables = [];
this.coldObservables = [];
this.flushTests = [];
}
TestScheduler.prototype.createColdObservable = function (marbles, values, error) {
Iif (marbles.indexOf('^') !== -1) {
throw new Error('Cold observable cannot have subscription offset "^"');
}
Iif (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) {
Iif (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 '^':
Iif (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 '!':
Iif (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 + '\'.');
}
}
Iif (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; }
Iif (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 |