Skip to content

Commit 6ad74b3

Browse files
vanniktechakarnokd
authored andcommitted
2.x: BaseTestConsumer add assertValueAt(index, Predicate<T>) (#4690)
1 parent 7e89c1f commit 6ad74b3

File tree

3 files changed

+141
-6
lines changed

3 files changed

+141
-6
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,26 +312,45 @@ public final U assertValue(T value) {
312312
*/
313313
@SuppressWarnings("unchecked")
314314
public final U assertValue(Predicate<T> valuePredicate) {
315+
assertValueAt(0, valuePredicate);
316+
317+
if (values.size() > 1) {
318+
throw fail("Value present but other values as well");
319+
}
320+
321+
return (U)this;
322+
}
323+
324+
/**
325+
* Asserts that this TestObserver/TestSubscriber received an onNext value at the given index
326+
* for the provided predicate returns true.
327+
* @param valuePredicate
328+
* the predicate that receives the onNext value
329+
* and should return true for the expected value.
330+
* @return this
331+
*/
332+
@SuppressWarnings("unchecked")
333+
public final U assertValueAt(int index, Predicate<T> valuePredicate) {
315334
int s = values.size();
316335
if (s == 0) {
317336
throw fail("No values");
318337
}
319338

339+
if (index >= values.size()) {
340+
throw fail("Invalid index: " + index);
341+
}
342+
320343
boolean found = false;
321344

322345
try {
323-
if (valuePredicate.test(values.get(0))) {
346+
if (valuePredicate.test(values.get(index))) {
324347
found = true;
325348
}
326349
} catch (Exception ex) {
327350
throw ExceptionHelper.wrapOrThrow(ex);
328351
}
329352

330-
if (found) {
331-
if (s != 1) {
332-
throw fail("Value present but other values as well");
333-
}
334-
} else {
353+
if (!found) {
335354
throw fail("Value not present");
336355
}
337356
return (U)this;

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,4 +1263,62 @@ public void assertValuePredicateMatchButMore() {
12631263
}
12641264
});
12651265
}
1266+
1267+
@Test
1268+
public void assertValueAtPredicateEmpty() {
1269+
TestObserver<Object> ts = new TestObserver<Object>();
1270+
1271+
Observable.empty().subscribe(ts);
1272+
1273+
thrown.expect(AssertionError.class);
1274+
thrown.expectMessage("No values");
1275+
ts.assertValueAt(0, new Predicate<Object>() {
1276+
@Override public boolean test(final Object o) throws Exception {
1277+
return false;
1278+
}
1279+
});
1280+
}
1281+
1282+
@Test
1283+
public void assertValueAtPredicateMatch() {
1284+
TestObserver<Integer> ts = new TestObserver<Integer>();
1285+
1286+
Observable.just(1, 2).subscribe(ts);
1287+
1288+
ts.assertValueAt(1, new Predicate<Integer>() {
1289+
@Override public boolean test(final Integer o) throws Exception {
1290+
return o == 2;
1291+
}
1292+
});
1293+
}
1294+
1295+
@Test
1296+
public void assertValueAtPredicateNoMatch() {
1297+
TestObserver<Integer> ts = new TestObserver<Integer>();
1298+
1299+
Observable.just(1, 2, 3).subscribe(ts);
1300+
1301+
thrown.expect(AssertionError.class);
1302+
thrown.expectMessage("Value not present");
1303+
ts.assertValueAt(2, new Predicate<Integer>() {
1304+
@Override public boolean test(final Integer o) throws Exception {
1305+
return o != 3;
1306+
}
1307+
});
1308+
}
1309+
1310+
@Test
1311+
public void assertValueAtInvalidIndex() {
1312+
TestObserver<Integer> ts = new TestObserver<Integer>();
1313+
1314+
Observable.just(1, 2).subscribe(ts);
1315+
1316+
thrown.expect(AssertionError.class);
1317+
thrown.expectMessage("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)");
1318+
ts.assertValueAt(2, new Predicate<Integer>() {
1319+
@Override public boolean test(final Integer o) throws Exception {
1320+
return o == 1;
1321+
}
1322+
});
1323+
}
12661324
}

src/test/java/io/reactivex/subscribers/TestSubscriberTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,4 +1649,62 @@ public void assertValuePredicateMatchButMore() {
16491649
}
16501650
});
16511651
}
1652+
1653+
@Test
1654+
public void assertValueAtPredicateEmpty() {
1655+
TestSubscriber<Object> ts = new TestSubscriber<Object>();
1656+
1657+
Flowable.empty().subscribe(ts);
1658+
1659+
thrown.expect(AssertionError.class);
1660+
thrown.expectMessage("No values");
1661+
ts.assertValueAt(0, new Predicate<Object>() {
1662+
@Override public boolean test(final Object o) throws Exception {
1663+
return false;
1664+
}
1665+
});
1666+
}
1667+
1668+
@Test
1669+
public void assertValueAtPredicateMatch() {
1670+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
1671+
1672+
Flowable.just(1, 2).subscribe(ts);
1673+
1674+
ts.assertValueAt(1, new Predicate<Integer>() {
1675+
@Override public boolean test(final Integer o) throws Exception {
1676+
return o == 2;
1677+
}
1678+
});
1679+
}
1680+
1681+
@Test
1682+
public void assertValueAtPredicateNoMatch() {
1683+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
1684+
1685+
Flowable.just(1, 2, 3).subscribe(ts);
1686+
1687+
thrown.expect(AssertionError.class);
1688+
thrown.expectMessage("Value not present");
1689+
ts.assertValueAt(2, new Predicate<Integer>() {
1690+
@Override public boolean test(final Integer o) throws Exception {
1691+
return o != 3;
1692+
}
1693+
});
1694+
}
1695+
1696+
@Test
1697+
public void assertValueAtInvalidIndex() {
1698+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
1699+
1700+
Flowable.just(1, 2).subscribe(ts);
1701+
1702+
thrown.expect(AssertionError.class);
1703+
thrown.expectMessage("Invalid index: 2 (latch = 0, values = 2, errors = 0, completions = 1)");
1704+
ts.assertValueAt(2, new Predicate<Integer>() {
1705+
@Override public boolean test(final Integer o) throws Exception {
1706+
return o == 1;
1707+
}
1708+
});
1709+
}
16521710
}

0 commit comments

Comments
 (0)