Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertValueAt(int, value) to TestObserver #5529

Merged
merged 5 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: java
jdk:
- oraclejdk7
- oraclejdk8

# force upgrade Java8 as per https://github.com/travis-ci/travis-ci/issues/4042 (fixes compilation issue)
#addons:
Expand Down
30 changes: 29 additions & 1 deletion src/main/java/io/reactivex/observers/BaseTestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.*;

import io.reactivex.Notification;
import io.reactivex.annotations.Experimental;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.CompositeException;
import io.reactivex.functions.Predicate;
Expand Down Expand Up @@ -335,7 +336,7 @@ public final U assertValue(T value) {

/**
* Assert that this TestObserver/TestSubscriber did not receive an onNext value which is equal to
* the given value with respect to Objects.equals.
* the given value with respect to null-safe Object.equals.
*
* <p>History: 2.0.5 - experimental
* @param value the value to expect not being received
Expand Down Expand Up @@ -401,6 +402,33 @@ public final U assertNever(Predicate<? super T> valuePredicate) {
return (U)this;
}

/**
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
* which is equal to the given value with respect to null-safe Object.equals.
* @param index the position to assert on
* @param value the value to expect
* @return this
* @since 2.1.3 - experimental
*/
@SuppressWarnings("unchecked")
@Experimental
public final U assertValueAt(int index, T value) {
int s = values.size();
if (s == 0) {
throw fail("No values");
}

if (index >= s) {
throw fail("Invalid index: " + index);
}

T v = values.get(index);
if (!ObjectHelper.equals(value, v)) {
throw fail("Expected: " + valueAndClass(value) + ", Actual: " + valueAndClass(v));
}
return (U)this;
}

/**
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
* for the provided predicate returns true.
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/io/reactivex/observers/TestObserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,48 @@ public void assertValueAtInvalidIndex() {
});
}

@Test
public void assertValueAtIndexEmpty() {
TestObserver<Object> ts = new TestObserver<Object>();

Observable.empty().subscribe(ts);

thrown.expect(AssertionError.class);
thrown.expectMessage("No values");
ts.assertValueAt(0, "a");
}

@Test
public void assertValueAtIndexMatch() {
TestObserver<String> ts = new TestObserver<String>();

Observable.just("a", "b").subscribe(ts);

ts.assertValueAt(1, "b");
}

@Test
public void assertValueAtIndexNoMatch() {
TestObserver<String> ts = new TestObserver<String>();

Observable.just("a", "b", "c").subscribe(ts);

thrown.expect(AssertionError.class);
thrown.expectMessage("Expected: b (class: String), Actual: c (class: String) (latch = 0, values = 3, errors = 0, completions = 1)");
ts.assertValueAt(2, "b");
}

@Test
public void assertValueAtIndexInvalidIndex() {
TestObserver<String> ts = new TestObserver<String>();

Observable.just("a", "b").subscribe(ts);

thrown.expect(AssertionError.class);
thrown.expectMessage("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)");
ts.assertValueAt(2, "c");
}

@Test
public void withTag() {
try {
Expand Down