-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* @param index the position to assert on | ||
* @param value the value to expect | ||
* @return this | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
@SuppressWarnings("unchecked") | ||
public final U assertValueAt(int index, T value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
int s = values.size(); | ||
if (s == 0) { | ||
throw fail("No values"); | ||
} | ||
|
||
if (index >= values.size()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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 butObjectHelper
is internal, thus maybe let's say "with respect to null-safe Object.equals()".There was a problem hiding this comment.
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 :)