TestScheduler.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /** PURE_IMPORTS_START .._Observable,.._Notification,._ColdObservable,._HotObservable,._SubscriptionLog,.._scheduler_VirtualTimeScheduler PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Observable } from '../Observable';
  10. import { Notification } from '../Notification';
  11. import { ColdObservable } from './ColdObservable';
  12. import { HotObservable } from './HotObservable';
  13. import { SubscriptionLog } from './SubscriptionLog';
  14. import { VirtualTimeScheduler, VirtualAction } from '../scheduler/VirtualTimeScheduler';
  15. var defaultMaxFrame = 750;
  16. export var TestScheduler = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  17. __extends(TestScheduler, _super);
  18. function TestScheduler(assertDeepEqual) {
  19. _super.call(this, VirtualAction, defaultMaxFrame);
  20. this.assertDeepEqual = assertDeepEqual;
  21. this.hotObservables = [];
  22. this.coldObservables = [];
  23. this.flushTests = [];
  24. }
  25. TestScheduler.prototype.createTime = function (marbles) {
  26. var indexOf = marbles.indexOf('|');
  27. if (indexOf === -1) {
  28. throw new Error('marble diagram for time should have a completion marker "|"');
  29. }
  30. return indexOf * TestScheduler.frameTimeFactor;
  31. };
  32. TestScheduler.prototype.createColdObservable = function (marbles, values, error) {
  33. if (marbles.indexOf('^') !== -1) {
  34. throw new Error('cold observable cannot have subscription offset "^"');
  35. }
  36. if (marbles.indexOf('!') !== -1) {
  37. throw new Error('cold observable cannot have unsubscription marker "!"');
  38. }
  39. var messages = TestScheduler.parseMarbles(marbles, values, error);
  40. var cold = new ColdObservable(messages, this);
  41. this.coldObservables.push(cold);
  42. return cold;
  43. };
  44. TestScheduler.prototype.createHotObservable = function (marbles, values, error) {
  45. if (marbles.indexOf('!') !== -1) {
  46. throw new Error('hot observable cannot have unsubscription marker "!"');
  47. }
  48. var messages = TestScheduler.parseMarbles(marbles, values, error);
  49. var subject = new HotObservable(messages, this);
  50. this.hotObservables.push(subject);
  51. return subject;
  52. };
  53. TestScheduler.prototype.materializeInnerObservable = function (observable, outerFrame) {
  54. var _this = this;
  55. var messages = [];
  56. observable.subscribe(function (value) {
  57. messages.push({ frame: _this.frame - outerFrame, notification: Notification.createNext(value) });
  58. }, function (err) {
  59. messages.push({ frame: _this.frame - outerFrame, notification: Notification.createError(err) });
  60. }, function () {
  61. messages.push({ frame: _this.frame - outerFrame, notification: Notification.createComplete() });
  62. });
  63. return messages;
  64. };
  65. TestScheduler.prototype.expectObservable = function (observable, unsubscriptionMarbles) {
  66. var _this = this;
  67. if (unsubscriptionMarbles === void 0) {
  68. unsubscriptionMarbles = null;
  69. }
  70. var actual = [];
  71. var flushTest = { actual: actual, ready: false };
  72. var unsubscriptionFrame = TestScheduler
  73. .parseMarblesAsSubscriptions(unsubscriptionMarbles).unsubscribedFrame;
  74. var subscription;
  75. this.schedule(function () {
  76. subscription = observable.subscribe(function (x) {
  77. var value = x;
  78. // Support Observable-of-Observables
  79. if (x instanceof Observable) {
  80. value = _this.materializeInnerObservable(value, _this.frame);
  81. }
  82. actual.push({ frame: _this.frame, notification: Notification.createNext(value) });
  83. }, function (err) {
  84. actual.push({ frame: _this.frame, notification: Notification.createError(err) });
  85. }, function () {
  86. actual.push({ frame: _this.frame, notification: Notification.createComplete() });
  87. });
  88. }, 0);
  89. if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
  90. this.schedule(function () { return subscription.unsubscribe(); }, unsubscriptionFrame);
  91. }
  92. this.flushTests.push(flushTest);
  93. return {
  94. toBe: function (marbles, values, errorValue) {
  95. flushTest.ready = true;
  96. flushTest.expected = TestScheduler.parseMarbles(marbles, values, errorValue, true);
  97. }
  98. };
  99. };
  100. TestScheduler.prototype.expectSubscriptions = function (actualSubscriptionLogs) {
  101. var flushTest = { actual: actualSubscriptionLogs, ready: false };
  102. this.flushTests.push(flushTest);
  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);
  109. });
  110. }
  111. };
  112. };
  113. TestScheduler.prototype.flush = function () {
  114. var hotObservables = this.hotObservables;
  115. while (hotObservables.length > 0) {
  116. hotObservables.shift().setup();
  117. }
  118. _super.prototype.flush.call(this);
  119. var readyFlushTests = this.flushTests.filter(function (test) { return test.ready; });
  120. while (readyFlushTests.length > 0) {
  121. var test = readyFlushTests.shift();
  122. this.assertDeepEqual(test.actual, test.expected);
  123. }
  124. };
  125. TestScheduler.parseMarblesAsSubscriptions = function (marbles) {
  126. if (typeof marbles !== 'string') {
  127. return new SubscriptionLog(Number.POSITIVE_INFINITY);
  128. }
  129. var len = marbles.length;
  130. var groupStart = -1;
  131. var subscriptionFrame = Number.POSITIVE_INFINITY;
  132. var unsubscriptionFrame = Number.POSITIVE_INFINITY;
  133. for (var i = 0; i < len; i++) {
  134. var frame = i * this.frameTimeFactor;
  135. var c = marbles[i];
  136. switch (c) {
  137. case '-':
  138. case ' ':
  139. break;
  140. case '(':
  141. groupStart = frame;
  142. break;
  143. case ')':
  144. groupStart = -1;
  145. break;
  146. case '^':
  147. if (subscriptionFrame !== Number.POSITIVE_INFINITY) {
  148. throw new Error('found a second subscription point \'^\' in a ' +
  149. 'subscription marble diagram. There can only be one.');
  150. }
  151. subscriptionFrame = groupStart > -1 ? groupStart : frame;
  152. break;
  153. case '!':
  154. if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
  155. throw new Error('found a second subscription point \'^\' in a ' +
  156. 'subscription marble diagram. There can only be one.');
  157. }
  158. unsubscriptionFrame = groupStart > -1 ? groupStart : frame;
  159. break;
  160. default:
  161. throw new Error('there can only be \'^\' and \'!\' markers in a ' +
  162. 'subscription marble diagram. Found instead \'' + c + '\'.');
  163. }
  164. }
  165. if (unsubscriptionFrame < 0) {
  166. return new SubscriptionLog(subscriptionFrame);
  167. }
  168. else {
  169. return new SubscriptionLog(subscriptionFrame, unsubscriptionFrame);
  170. }
  171. };
  172. TestScheduler.parseMarbles = function (marbles, values, errorValue, materializeInnerObservables) {
  173. if (materializeInnerObservables === void 0) {
  174. materializeInnerObservables = false;
  175. }
  176. if (marbles.indexOf('!') !== -1) {
  177. throw new Error('conventional marble diagrams cannot have the ' +
  178. 'unsubscription marker "!"');
  179. }
  180. var len = marbles.length;
  181. var testMessages = [];
  182. var subIndex = marbles.indexOf('^');
  183. var frameOffset = subIndex === -1 ? 0 : (subIndex * -this.frameTimeFactor);
  184. var getValue = typeof values !== 'object' ?
  185. function (x) { return x; } :
  186. function (x) {
  187. // Support Observable-of-Observables
  188. if (materializeInnerObservables && values[x] instanceof ColdObservable) {
  189. return values[x].messages;
  190. }
  191. return values[x];
  192. };
  193. var groupStart = -1;
  194. for (var i = 0; i < len; i++) {
  195. var frame = i * this.frameTimeFactor + frameOffset;
  196. var notification = void 0;
  197. var c = marbles[i];
  198. switch (c) {
  199. case '-':
  200. case ' ':
  201. break;
  202. case '(':
  203. groupStart = frame;
  204. break;
  205. case ')':
  206. groupStart = -1;
  207. break;
  208. case '|':
  209. notification = Notification.createComplete();
  210. break;
  211. case '^':
  212. break;
  213. case '#':
  214. notification = Notification.createError(errorValue || 'error');
  215. break;
  216. default:
  217. notification = Notification.createNext(getValue(c));
  218. break;
  219. }
  220. if (notification) {
  221. testMessages.push({ frame: groupStart > -1 ? groupStart : frame, notification: notification });
  222. }
  223. }
  224. return testMessages;
  225. };
  226. return TestScheduler;
  227. }(VirtualTimeScheduler));
  228. //# sourceMappingURL=TestScheduler.js.map