Skip to content

Add assertValueAt(int, value) to TestObserver #5529

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

Merged
merged 5 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions src/main/java/io/reactivex/observers/BaseTestConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,31 @@ 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 Objects.equals.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects is a Java 8 component but ObjectHelper is internal, thus maybe let's say "with respect to null-safe Object.equals()".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the other javadoc I copied that from too :)

* @param index the position to assert on
* @param value the value to expect
* @return this
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@since 2.1.3 - experimental

@SuppressWarnings("unchecked")
public final U assertValueAt(int index, T value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Experimental

int s = values.size();
if (s == 0) {
throw fail("No values");
}

if (index >= values.size()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use s instead of values.size() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

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, 1);
}

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

Observable.just(1, 2).subscribe(ts);

ts.assertValueAt(1, 2);
}

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

Observable.just(1, 2, 3).subscribe(ts);

thrown.expect(AssertionError.class);
thrown.expectMessage("Expected: 2 (class: Integer), Actual: 3 (class: Integer) (latch = 0, values = 3, errors = 0, completions = 1)");
ts.assertValueAt(2, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please compare something else than integers (String will be ok)?

Silly code change in assert method can break its logic but pass this test because both index and value are 2 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.

}

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

Observable.just(1, 2).subscribe(ts);

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

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