rx.marble.junit.MarbleRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of marbletest4j Show documentation
Show all versions of marbletest4j Show documentation
Java port of RxJS marble tests
package rx.marble.junit;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import rx.Observable;
import rx.marble.*;
import java.util.List;
import java.util.Map;
public class MarbleRule implements TestRule {
private static ThreadLocal schedulerHolder = new ThreadLocal<>();
public final MarbleScheduler scheduler;
public MarbleRule() {
scheduler = new MarbleScheduler();
}
public MarbleRule(long frameTimeFactor) {
scheduler = new MarbleScheduler(frameTimeFactor);
}
public static HotObservable hot(String marbles, Map values) {
return schedulerHolder.get().createHotObservable(marbles, values);
}
public static HotObservable hot(String marbles) {
return schedulerHolder.get().createHotObservable(marbles);
}
public static ColdObservable cold(String marbles, Map values) {
return schedulerHolder.get().createColdObservable(marbles, values);
}
public static ColdObservable cold(String marbles) {
return schedulerHolder.get().createColdObservable(marbles);
}
public static ISetupTest expectObservable(Observable> actual) {
return schedulerHolder.get().expectObservable(actual);
}
public static ISetupTest expectObservable(Observable> actual, String unsubscriptionMarbles) {
return schedulerHolder.get().expectObservable(actual, unsubscriptionMarbles);
}
public static ISetupSubscriptionsTest expectSubscriptions(List subscriptions) {
return schedulerHolder.get().expectSubscriptions(subscriptions);
}
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
schedulerHolder.set(scheduler);
try {
base.evaluate();
scheduler.flush();
} finally {
schedulerHolder.remove();
}
}
};
}
}