Ohm-Management - Projektarbeit B-ME
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.

TestScheduler.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /** PURE_IMPORTS_START tslib,_Observable,_Notification,_ColdObservable,_HotObservable,_SubscriptionLog,_scheduler_VirtualTimeScheduler,_scheduler_AsyncScheduler PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Observable } from '../Observable';
  4. import { Notification } from '../Notification';
  5. import { ColdObservable } from './ColdObservable';
  6. import { HotObservable } from './HotObservable';
  7. import { SubscriptionLog } from './SubscriptionLog';
  8. import { VirtualTimeScheduler, VirtualAction } from '../scheduler/VirtualTimeScheduler';
  9. import { AsyncScheduler } from '../scheduler/AsyncScheduler';
  10. var defaultMaxFrame = 750;
  11. var 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));
  358. export { TestScheduler };
  359. //# sourceMappingURL=TestScheduler.js.map