source: trip-planner-front/node_modules/rxjs/_esm5/internal/testing/TestScheduler.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 15.3 KB
Line 
1/** PURE_IMPORTS_START tslib,_Observable,_Notification,_ColdObservable,_HotObservable,_SubscriptionLog,_scheduler_VirtualTimeScheduler,_scheduler_AsyncScheduler PURE_IMPORTS_END */
2import * as tslib_1 from "tslib";
3import { Observable } from '../Observable';
4import { Notification } from '../Notification';
5import { ColdObservable } from './ColdObservable';
6import { HotObservable } from './HotObservable';
7import { SubscriptionLog } from './SubscriptionLog';
8import { VirtualTimeScheduler, VirtualAction } from '../scheduler/VirtualTimeScheduler';
9import { AsyncScheduler } from '../scheduler/AsyncScheduler';
10var defaultMaxFrame = 750;
11var TestScheduler = /*@__PURE__*/ (function (_super) {
12 tslib_1.__extends(TestScheduler, _super);
13 function TestScheduler(assertDeepEqual) {
14 var _this = _super.call(this, VirtualAction, defaultMaxFrame) || this;
15 _this.assertDeepEqual = assertDeepEqual;
16 _this.hotObservables = [];
17 _this.coldObservables = [];
18 _this.flushTests = [];
19 _this.runMode = false;
20 return _this;
21 }
22 TestScheduler.prototype.createTime = function (marbles) {
23 var indexOf = marbles.indexOf('|');
24 if (indexOf === -1) {
25 throw new Error('marble diagram for time should have a completion marker "|"');
26 }
27 return indexOf * TestScheduler.frameTimeFactor;
28 };
29 TestScheduler.prototype.createColdObservable = function (marbles, values, error) {
30 if (marbles.indexOf('^') !== -1) {
31 throw new Error('cold observable cannot have subscription offset "^"');
32 }
33 if (marbles.indexOf('!') !== -1) {
34 throw new Error('cold observable cannot have unsubscription marker "!"');
35 }
36 var messages = TestScheduler.parseMarbles(marbles, values, error, undefined, this.runMode);
37 var cold = new ColdObservable(messages, this);
38 this.coldObservables.push(cold);
39 return cold;
40 };
41 TestScheduler.prototype.createHotObservable = function (marbles, values, error) {
42 if (marbles.indexOf('!') !== -1) {
43 throw new Error('hot observable cannot have unsubscription marker "!"');
44 }
45 var messages = TestScheduler.parseMarbles(marbles, values, error, undefined, this.runMode);
46 var subject = new HotObservable(messages, this);
47 this.hotObservables.push(subject);
48 return subject;
49 };
50 TestScheduler.prototype.materializeInnerObservable = function (observable, outerFrame) {
51 var _this = this;
52 var messages = [];
53 observable.subscribe(function (value) {
54 messages.push({ frame: _this.frame - outerFrame, notification: Notification.createNext(value) });
55 }, function (err) {
56 messages.push({ frame: _this.frame - outerFrame, notification: Notification.createError(err) });
57 }, function () {
58 messages.push({ frame: _this.frame - outerFrame, notification: Notification.createComplete() });
59 });
60 return messages;
61 };
62 TestScheduler.prototype.expectObservable = function (observable, subscriptionMarbles) {
63 var _this = this;
64 if (subscriptionMarbles === void 0) {
65 subscriptionMarbles = null;
66 }
67 var actual = [];
68 var flushTest = { actual: actual, ready: false };
69 var subscriptionParsed = TestScheduler.parseMarblesAsSubscriptions(subscriptionMarbles, this.runMode);
70 var subscriptionFrame = subscriptionParsed.subscribedFrame === Number.POSITIVE_INFINITY ?
71 0 : subscriptionParsed.subscribedFrame;
72 var unsubscriptionFrame = subscriptionParsed.unsubscribedFrame;
73 var subscription;
74 this.schedule(function () {
75 subscription = observable.subscribe(function (x) {
76 var value = x;
77 if (x instanceof Observable) {
78 value = _this.materializeInnerObservable(value, _this.frame);
79 }
80 actual.push({ frame: _this.frame, notification: Notification.createNext(value) });
81 }, function (err) {
82 actual.push({ frame: _this.frame, notification: Notification.createError(err) });
83 }, function () {
84 actual.push({ frame: _this.frame, notification: Notification.createComplete() });
85 });
86 }, subscriptionFrame);
87 if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
88 this.schedule(function () { return subscription.unsubscribe(); }, unsubscriptionFrame);
89 }
90 this.flushTests.push(flushTest);
91 var runMode = this.runMode;
92 return {
93 toBe: function (marbles, values, errorValue) {
94 flushTest.ready = true;
95 flushTest.expected = TestScheduler.parseMarbles(marbles, values, errorValue, true, runMode);
96 }
97 };
98 };
99 TestScheduler.prototype.expectSubscriptions = function (actualSubscriptionLogs) {
100 var flushTest = { actual: actualSubscriptionLogs, ready: false };
101 this.flushTests.push(flushTest);
102 var runMode = this.runMode;
103 return {
104 toBe: function (marbles) {
105 var marblesArray = (typeof marbles === 'string') ? [marbles] : marbles;
106 flushTest.ready = true;
107 flushTest.expected = marblesArray.map(function (marbles) {
108 return TestScheduler.parseMarblesAsSubscriptions(marbles, runMode);
109 });
110 }
111 };
112 };
113 TestScheduler.prototype.flush = function () {
114 var _this = this;
115 var hotObservables = this.hotObservables;
116 while (hotObservables.length > 0) {
117 hotObservables.shift().setup();
118 }
119 _super.prototype.flush.call(this);
120 this.flushTests = this.flushTests.filter(function (test) {
121 if (test.ready) {
122 _this.assertDeepEqual(test.actual, test.expected);
123 return false;
124 }
125 return true;
126 });
127 };
128 TestScheduler.parseMarblesAsSubscriptions = function (marbles, runMode) {
129 var _this = this;
130 if (runMode === void 0) {
131 runMode = false;
132 }
133 if (typeof marbles !== 'string') {
134 return new SubscriptionLog(Number.POSITIVE_INFINITY);
135 }
136 var len = marbles.length;
137 var groupStart = -1;
138 var subscriptionFrame = Number.POSITIVE_INFINITY;
139 var unsubscriptionFrame = Number.POSITIVE_INFINITY;
140 var frame = 0;
141 var _loop_1 = function (i) {
142 var nextFrame = frame;
143 var advanceFrameBy = function (count) {
144 nextFrame += count * _this.frameTimeFactor;
145 };
146 var c = marbles[i];
147 switch (c) {
148 case ' ':
149 if (!runMode) {
150 advanceFrameBy(1);
151 }
152 break;
153 case '-':
154 advanceFrameBy(1);
155 break;
156 case '(':
157 groupStart = frame;
158 advanceFrameBy(1);
159 break;
160 case ')':
161 groupStart = -1;
162 advanceFrameBy(1);
163 break;
164 case '^':
165 if (subscriptionFrame !== Number.POSITIVE_INFINITY) {
166 throw new Error('found a second subscription point \'^\' in a ' +
167 'subscription marble diagram. There can only be one.');
168 }
169 subscriptionFrame = groupStart > -1 ? groupStart : frame;
170 advanceFrameBy(1);
171 break;
172 case '!':
173 if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
174 throw new Error('found a second subscription point \'^\' in a ' +
175 'subscription marble diagram. There can only be one.');
176 }
177 unsubscriptionFrame = groupStart > -1 ? groupStart : frame;
178 break;
179 default:
180 if (runMode && c.match(/^[0-9]$/)) {
181 if (i === 0 || marbles[i - 1] === ' ') {
182 var buffer = marbles.slice(i);
183 var match = buffer.match(/^([0-9]+(?:\.[0-9]+)?)(ms|s|m) /);
184 if (match) {
185 i += match[0].length - 1;
186 var duration = parseFloat(match[1]);
187 var unit = match[2];
188 var durationInMs = void 0;
189 switch (unit) {
190 case 'ms':
191 durationInMs = duration;
192 break;
193 case 's':
194 durationInMs = duration * 1000;
195 break;
196 case 'm':
197 durationInMs = duration * 1000 * 60;
198 break;
199 default:
200 break;
201 }
202 advanceFrameBy(durationInMs / this_1.frameTimeFactor);
203 break;
204 }
205 }
206 }
207 throw new Error('there can only be \'^\' and \'!\' markers in a ' +
208 'subscription marble diagram. Found instead \'' + c + '\'.');
209 }
210 frame = nextFrame;
211 out_i_1 = i;
212 };
213 var this_1 = this, out_i_1;
214 for (var i = 0; i < len; i++) {
215 _loop_1(i);
216 i = out_i_1;
217 }
218 if (unsubscriptionFrame < 0) {
219 return new SubscriptionLog(subscriptionFrame);
220 }
221 else {
222 return new SubscriptionLog(subscriptionFrame, unsubscriptionFrame);
223 }
224 };
225 TestScheduler.parseMarbles = function (marbles, values, errorValue, materializeInnerObservables, runMode) {
226 var _this = this;
227 if (materializeInnerObservables === void 0) {
228 materializeInnerObservables = false;
229 }
230 if (runMode === void 0) {
231 runMode = false;
232 }
233 if (marbles.indexOf('!') !== -1) {
234 throw new Error('conventional marble diagrams cannot have the ' +
235 'unsubscription marker "!"');
236 }
237 var len = marbles.length;
238 var testMessages = [];
239 var subIndex = runMode ? marbles.replace(/^[ ]+/, '').indexOf('^') : marbles.indexOf('^');
240 var frame = subIndex === -1 ? 0 : (subIndex * -this.frameTimeFactor);
241 var getValue = typeof values !== 'object' ?
242 function (x) { return x; } :
243 function (x) {
244 if (materializeInnerObservables && values[x] instanceof ColdObservable) {
245 return values[x].messages;
246 }
247 return values[x];
248 };
249 var groupStart = -1;
250 var _loop_2 = function (i) {
251 var nextFrame = frame;
252 var advanceFrameBy = function (count) {
253 nextFrame += count * _this.frameTimeFactor;
254 };
255 var notification = void 0;
256 var c = marbles[i];
257 switch (c) {
258 case ' ':
259 if (!runMode) {
260 advanceFrameBy(1);
261 }
262 break;
263 case '-':
264 advanceFrameBy(1);
265 break;
266 case '(':
267 groupStart = frame;
268 advanceFrameBy(1);
269 break;
270 case ')':
271 groupStart = -1;
272 advanceFrameBy(1);
273 break;
274 case '|':
275 notification = Notification.createComplete();
276 advanceFrameBy(1);
277 break;
278 case '^':
279 advanceFrameBy(1);
280 break;
281 case '#':
282 notification = Notification.createError(errorValue || 'error');
283 advanceFrameBy(1);
284 break;
285 default:
286 if (runMode && c.match(/^[0-9]$/)) {
287 if (i === 0 || marbles[i - 1] === ' ') {
288 var buffer = marbles.slice(i);
289 var match = buffer.match(/^([0-9]+(?:\.[0-9]+)?)(ms|s|m) /);
290 if (match) {
291 i += match[0].length - 1;
292 var duration = parseFloat(match[1]);
293 var unit = match[2];
294 var durationInMs = void 0;
295 switch (unit) {
296 case 'ms':
297 durationInMs = duration;
298 break;
299 case 's':
300 durationInMs = duration * 1000;
301 break;
302 case 'm':
303 durationInMs = duration * 1000 * 60;
304 break;
305 default:
306 break;
307 }
308 advanceFrameBy(durationInMs / this_2.frameTimeFactor);
309 break;
310 }
311 }
312 }
313 notification = Notification.createNext(getValue(c));
314 advanceFrameBy(1);
315 break;
316 }
317 if (notification) {
318 testMessages.push({ frame: groupStart > -1 ? groupStart : frame, notification: notification });
319 }
320 frame = nextFrame;
321 out_i_2 = i;
322 };
323 var this_2 = this, out_i_2;
324 for (var i = 0; i < len; i++) {
325 _loop_2(i);
326 i = out_i_2;
327 }
328 return testMessages;
329 };
330 TestScheduler.prototype.run = function (callback) {
331 var prevFrameTimeFactor = TestScheduler.frameTimeFactor;
332 var prevMaxFrames = this.maxFrames;
333 TestScheduler.frameTimeFactor = 1;
334 this.maxFrames = Number.POSITIVE_INFINITY;
335 this.runMode = true;
336 AsyncScheduler.delegate = this;
337 var helpers = {
338 cold: this.createColdObservable.bind(this),
339 hot: this.createHotObservable.bind(this),
340 flush: this.flush.bind(this),
341 expectObservable: this.expectObservable.bind(this),
342 expectSubscriptions: this.expectSubscriptions.bind(this),
343 };
344 try {
345 var ret = callback(helpers);
346 this.flush();
347 return ret;
348 }
349 finally {
350 TestScheduler.frameTimeFactor = prevFrameTimeFactor;
351 this.maxFrames = prevMaxFrames;
352 this.runMode = false;
353 AsyncScheduler.delegate = undefined;
354 }
355 };
356 return TestScheduler;
357}(VirtualTimeScheduler));
358export { TestScheduler };
359//# sourceMappingURL=TestScheduler.js.map
Note: See TracBrowser for help on using the repository browser.