Skip to content

Commit 5242cf3

Browse files
NickFirmaniakarnokd
authored andcommitted
Add assertValueAt(int, value) to TestObserver (#5529)
* Add assertValueAt(int, value) to TestObserver * Fix test to call correct method * David's comments * Artem's comments * 2.x: Try fixing Travis CI lack of java (#5531) * 2.x: Try fixing Travis CI lack of java * Force dist: precise * Oracle JDK 8 is then
1 parent e9a9af3 commit 5242cf3

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/main/java/io/reactivex/observers/BaseTestConsumer.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.concurrent.*;
1818

1919
import io.reactivex.Notification;
20+
import io.reactivex.annotations.Experimental;
2021
import io.reactivex.disposables.Disposable;
2122
import io.reactivex.exceptions.CompositeException;
2223
import io.reactivex.functions.Predicate;
@@ -335,7 +336,7 @@ public final U assertValue(T value) {
335336

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

405+
/**
406+
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
407+
* which is equal to the given value with respect to null-safe Object.equals.
408+
* @param index the position to assert on
409+
* @param value the value to expect
410+
* @return this
411+
* @since 2.1.3 - experimental
412+
*/
413+
@SuppressWarnings("unchecked")
414+
@Experimental
415+
public final U assertValueAt(int index, T value) {
416+
int s = values.size();
417+
if (s == 0) {
418+
throw fail("No values");
419+
}
420+
421+
if (index >= s) {
422+
throw fail("Invalid index: " + index);
423+
}
424+
425+
T v = values.get(index);
426+
if (!ObjectHelper.equals(value, v)) {
427+
throw fail("Expected: " + valueAndClass(value) + ", Actual: " + valueAndClass(v));
428+
}
429+
return (U)this;
430+
}
431+
404432
/**
405433
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
406434
* for the provided predicate returns true.

src/test/java/io/reactivex/observers/TestObserverTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,48 @@ public void assertValueAtInvalidIndex() {
13821382
});
13831383
}
13841384

1385+
@Test
1386+
public void assertValueAtIndexEmpty() {
1387+
TestObserver<Object> ts = new TestObserver<Object>();
1388+
1389+
Observable.empty().subscribe(ts);
1390+
1391+
thrown.expect(AssertionError.class);
1392+
thrown.expectMessage("No values");
1393+
ts.assertValueAt(0, "a");
1394+
}
1395+
1396+
@Test
1397+
public void assertValueAtIndexMatch() {
1398+
TestObserver<String> ts = new TestObserver<String>();
1399+
1400+
Observable.just("a", "b").subscribe(ts);
1401+
1402+
ts.assertValueAt(1, "b");
1403+
}
1404+
1405+
@Test
1406+
public void assertValueAtIndexNoMatch() {
1407+
TestObserver<String> ts = new TestObserver<String>();
1408+
1409+
Observable.just("a", "b", "c").subscribe(ts);
1410+
1411+
thrown.expect(AssertionError.class);
1412+
thrown.expectMessage("Expected: b (class: String), Actual: c (class: String) (latch = 0, values = 3, errors = 0, completions = 1)");
1413+
ts.assertValueAt(2, "b");
1414+
}
1415+
1416+
@Test
1417+
public void assertValueAtIndexInvalidIndex() {
1418+
TestObserver<String> ts = new TestObserver<String>();
1419+
1420+
Observable.just("a", "b").subscribe(ts);
1421+
1422+
thrown.expect(AssertionError.class);
1423+
thrown.expectMessage("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)");
1424+
ts.assertValueAt(2, "c");
1425+
}
1426+
13851427
@Test
13861428
public void withTag() {
13871429
try {

0 commit comments

Comments
 (0)