Code coverage report for RxJS/dist/cjs/testing/TestScheduler.js

Statements: 93.71% (149 / 159)      Branches: 82.28% (65 / 79)      Functions: 100% (27 / 27)      Lines: 94.7% (143 / 151)      Ignored: none     

All files » RxJS/dist/cjs/testing/ » TestScheduler.js
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249    1   6   814   1   1   1   1   1   1   1   1   1   1   1   1   1   1 1   1 814   814 814 814 814     1 491     491     491 491     1 680     680 680 680 680     1 138   138 138 251   35   96   138     1 736   736   736 736 736 736 736 736 1400   1400 138   1400   226   360     736 40 40     736 736   734 734         1 122 122 122   120 120 120 137           1 818 818 680   818 814 858   814 854 854       1 896 696   200 200 200 200 200 5419 5419 5419     5054   15 15   15 15   149     149 149   186     186 186         200     200       1 1943   1943     1943 1943 1943 1943 1943 3395     1396 138   1258   1943 1943 31145 31145 31145 31145     23858   273 273   274 274   1203 1203   295   451 451   4791 4791   31145 31145 6445     1943     1     1
'use strict';
 
exports.__esModule = true;
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
var _Observable = require('../Observable');
 
var _Observable2 = _interopRequireDefault(_Observable);
 
var _schedulersVirtualTimeScheduler = require('../schedulers/VirtualTimeScheduler');
 
var _schedulersVirtualTimeScheduler2 = _interopRequireDefault(_schedulersVirtualTimeScheduler);
 
var _Notification = require('../Notification');
 
var _Notification2 = _interopRequireDefault(_Notification);
 
var _ColdObservable = require('./ColdObservable');
 
var _ColdObservable2 = _interopRequireDefault(_ColdObservable);
 
var _HotObservable = require('./HotObservable');
 
var _HotObservable2 = _interopRequireDefault(_HotObservable);
 
var _SubscriptionLog = require('./SubscriptionLog');
 
var _SubscriptionLog2 = _interopRequireDefault(_SubscriptionLog);
 
var TestScheduler = (function (_VirtualTimeScheduler) {
    _inherits(TestScheduler, _VirtualTimeScheduler);
 
    function TestScheduler(assertDeepEqual) {
        _classCallCheck(this, TestScheduler);
 
        _VirtualTimeScheduler.call(this);
        this.assertDeepEqual = assertDeepEqual;
        this.hotObservables = [];
        this.flushTests = [];
    }
 
    TestScheduler.prototype.createColdObservable = function createColdObservable(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);
        return new _ColdObservable2['default'](messages, this);
    };
 
    TestScheduler.prototype.createHotObservable = function createHotObservable(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 _HotObservable2['default'](messages, this);
        this.hotObservables.push(subject);
        return subject;
    };
 
    TestScheduler.prototype.materializeInnerObservable = function materializeInnerObservable(observable, outerFrame) {
        var _this = this;
 
        var messages = [];
        observable.subscribe(function (value) {
            messages.push({ frame: _this.frame - outerFrame, notification: _Notification2['default'].createNext(value) });
        }, function (err) {
            messages.push({ frame: _this.frame - outerFrame, notification: _Notification2['default'].createError(err) });
        }, function () {
            messages.push({ frame: _this.frame - outerFrame, notification: _Notification2['default'].createComplete() });
        });
        return messages;
    };
 
    TestScheduler.prototype.expectObservable = function expectObservable(observable) {
        var _this2 = this;
 
        var unsubscriptionMarbles = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
 
        var actual = [];
        var flushTest = { actual: actual, ready: false };
        var unsubscriptionFrame = TestScheduler.parseMarblesAsSubscriptions(unsubscriptionMarbles).unsubscribedFrame;
        var subscription = undefined;
        this.schedule(function () {
            subscription = observable.subscribe(function (x) {
                var value = x;
                // Support Observable-of-Observables
                if (x instanceof _Observable2['default']) {
                    value = _this2.materializeInnerObservable(value, _this2.frame);
                }
                actual.push({ frame: _this2.frame, notification: _Notification2['default'].createNext(value) });
            }, function (err) {
                actual.push({ frame: _this2.frame, notification: _Notification2['default'].createError(err) });
            }, function () {
                actual.push({ frame: _this2.frame, notification: _Notification2['default'].createComplete() });
            });
        }, 0);
        if (unsubscriptionFrame !== Number.POSITIVE_INFINITY) {
            this.schedule(function () {
                return subscription.unsubscribe();
            }, unsubscriptionFrame);
        }
        this.flushTests.push(flushTest);
        return {
            toBe: function toBe(marbles, values, errorValue) {
                flushTest.ready = true;
                flushTest.expected = TestScheduler.parseMarbles(marbles, values, errorValue, true);
            }
        };
    };
 
    TestScheduler.prototype.expectSubscriptions = function expectSubscriptions(actualSubscriptionLogs) {
        var flushTest = { actual: actualSubscriptionLogs, ready: false };
        this.flushTests.push(flushTest);
        return {
            toBe: function toBe(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 flush() {
        var hotObservables = this.hotObservables;
        while (hotObservables.length > 0) {
            hotObservables.shift().setup();
        }
        _VirtualTimeScheduler.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 parseMarblesAsSubscriptions(marbles) {
        if (typeof marbles !== 'string') {
            return new _SubscriptionLog2['default'](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 _SubscriptionLog2['default'](subscriptionFrame);
        } else {
            return new _SubscriptionLog2['default'](subscriptionFrame, unsubscriptionFrame);
        }
    };
 
    TestScheduler.parseMarbles = function parseMarbles(marbles, values, errorValue) {
        var materializeInnerObservables = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];
 
        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 _ColdObservable2['default']) {
                return values[x].messages;
            }
            return values[x];
        };
        var groupStart = -1;
        for (var i = 0; i < len; i++) {
            var frame = i * this.frameTimeFactor;
            var notification = undefined;
            var c = marbles[i];
            switch (c) {
                case '-':
                case ' ':
                    break;
                case '(':
                    groupStart = frame;
                    break;
                case ')':
                    groupStart = -1;
                    break;
                case '|':
                    notification = _Notification2['default'].createComplete();
                    break;
                case '^':
                    break;
                case '#':
                    notification = _Notification2['default'].createError(errorValue || 'error');
                    break;
                default:
                    notification = _Notification2['default'].createNext(getValue(c));
                    break;
            }
            frame += frameOffset;
            if (notification) {
                testMessages.push({ frame: groupStart > -1 ? groupStart : frame, notification: notification });
            }
        }
        return testMessages;
    };
 
    return TestScheduler;
})(_schedulersVirtualTimeScheduler2['default']);
 
exports.TestScheduler = TestScheduler;