Skip to content

Commit

Permalink
refactor(testscheduler): make TestScheduler take optional params
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Jan 27, 2017
1 parent a79863a commit 63039dd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
19 changes: 12 additions & 7 deletions src/testing/Marble.ts
@@ -1,18 +1,21 @@
/**
* Created by tushar.mathur on 23/10/16.
*/
import {START_SUBSCRIPTION_TIME} from './TestScheduler'
import {IEvent, EventType} from '../types/IEvent'
import {ReactiveEvents, EventNext} from './ReactiveEvents'
import {OptionType, resolveOptions} from './TestOptions'

// todo: migrate all tests to use marble
// todo: configurations should be arguments and not globally set once
export const MARBLE_SIZE = 10

export function marble (message: String, err: Error = new Error()): Array<IEvent> {
var events: Array<IEvent> = []
var time = START_SUBSCRIPTION_TIME
for (var i = 0; i < message.length; ++i) {
export function marble (message: String,
err: Error = new Error(),
o?: OptionType): Array<IEvent> {
const options = resolveOptions(o)
const events: Array<IEvent> = []
let time = options.start
for (let i = 0; i < message.length; ++i) {
switch (message[i]) {
case '-' :
break
Expand All @@ -31,8 +34,10 @@ export function marble (message: String, err: Error = new Error()): Array<IEvent
return events
}

export function toMarble<T> (events: Array<IEvent>) {
let time = START_SUBSCRIPTION_TIME - MARBLE_SIZE
export function toMarble<T> (events: Array<IEvent>,
o?: OptionType) {
const options = resolveOptions(o)
let time = options.start - options.size
let message = ''
events.forEach(ev => {
if (ev.time % MARBLE_SIZE !== 0)
Expand Down
18 changes: 18 additions & 0 deletions src/testing/TestOptions.ts
@@ -0,0 +1,18 @@
/**
* Created by tushar on 27/01/17.
*/

export type OptionType = {
start: number
stop: number
size: number
}
const DEFAULT_OPTIONS = {
start: 200,
stop: 2000,
size: 10
}

export const resolveOptions = (opt: {start?: number, stop?: number, size?: number} = {}): OptionType => {
return {...opt, ...DEFAULT_OPTIONS}
}
17 changes: 7 additions & 10 deletions src/testing/TestScheduler.ts
Expand Up @@ -12,10 +12,7 @@ import {HotTestObservable} from './HotTestObservable'
import {LinkedList, LinkedListNode} from '../lib/LinkedList'
import {ISchedulerOptions} from '../types/ISchedulerOptions'
import {TestObservable} from './TestObservable'

export const START_SUBSCRIPTION_TIME = 200
export const STOP_SUBSCRIPTION_TIME = 2000

import {resolveOptions, OptionType} from './TestOptions'

class TaskSchedule {
constructor (public task: ITask, public time: number) {
Expand Down Expand Up @@ -88,13 +85,13 @@ export class TestScheduler implements Scheduler {
}

start<T> (f: () => Observable<T>,
start = START_SUBSCRIPTION_TIME,
stop = STOP_SUBSCRIPTION_TIME) {
var subscription: Subscription
o?: OptionType) {
const options = resolveOptions(o)
let subscription: Subscription
const resultsObserver = new TestObserver(this)
this.setTimeout(() => subscription = f().subscribe(resultsObserver, this), start, 0)
this.setTimeout(() => !subscription.closed && subscription.unsubscribe(), stop, 0)
this.advanceBy(stop)
this.setTimeout(() => subscription = f().subscribe(resultsObserver, this), options.start, 0)
this.setTimeout(() => !subscription.closed && subscription.unsubscribe(), options.stop, 0)
this.advanceBy(options.stop)
return resultsObserver
}

Expand Down
3 changes: 1 addition & 2 deletions test/test.Combine.ts
@@ -1,12 +1,11 @@
/**
* Created by tushar on 08/12/16.
*/

import test from 'ava'
import {combine} from '../src/operators/Combine'
import {TestScheduler} from '../src/testing/TestScheduler'
import {marble} from '../src/testing/Marble'
import { ReactiveEvents } from '../src/testing/ReactiveEvents'
import {ReactiveEvents} from '../src/testing/ReactiveEvents'

test(t => {
const S = TestScheduler.of()
Expand Down

0 comments on commit 63039dd

Please sign in to comment.