Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
Add in Observable.empty method that emits nothing, and test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonycr committed Nov 5, 2016
1 parent fd73bea commit 68eb7eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
17 changes: 17 additions & 0 deletions library/src/main/java/com/anthonycr/bonsai/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public static <T> Observable<T> create(@NonNull Action<T> action) {
return new Observable<>(action);
}

/**
* Static creator that creates an Observable that is empty
* and emits no items, but completes immediately.
*
* @param <T> the type that will be emitted to the onSubscribe
* @return a valid non-null empty Observable.
*/
@NonNull
public static <T> Observable<T> empty() {
return new Observable<>(new Action<T>() {
@Override
public void onSubscribe(@NonNull Subscriber<T> subscriber) {
subscriber.onComplete();
}
});
}

/**
* Tells the Observable what Scheduler that the onSubscribe
* work should run on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ObservableUnitTest extends BaseUnitTest{
public class ObservableUnitTest extends BaseUnitTest {

@Test
public void testMainLooperWorking() throws Exception {
Expand Down Expand Up @@ -402,4 +402,26 @@ public void onNext(@Nullable String item) {
Assert.assertTrue("isUnsubscribed() was not correct", unsubscribed.get());
}

@Test
public void testObservableEmpty_emitsNothingImmediately() throws Exception {
final Assertion<Boolean> onNextAssertion = new Assertion<>(false);
final Assertion<Boolean> onCompleteAssertion = new Assertion<>(false);
Observable.empty().subscribe(new OnSubscribe<Object>() {

@Override
public void onNext(@Nullable Object item) {
onNextAssertion.set(true);
}

@Override
public void onComplete() {
onCompleteAssertion.set(true);
}

});

Assert.assertFalse(onNextAssertion.get());
Assert.assertTrue(onCompleteAssertion.get());
}

}

0 comments on commit 68eb7eb

Please sign in to comment.