Skip to content

Commit 4f0791f

Browse files
vanniktechakarnokd
authored andcommitted
2.x: TestSubscriber & TestObserver add assertValue(Predicate) (ReactiveX#4607)
* 2.x: TestSubscriber & TestObserver add assertValue(Predicate) * Adjust
1 parent c19e16e commit 4f0791f

File tree

4 files changed

+186
-3
lines changed

4 files changed

+186
-3
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,40 @@ public final TestObserver<T> assertValue(T value) {
533533
return this;
534534
}
535535

536+
/**
537+
* Asserts that this TestObserver received exactly one onNext value for which
538+
* the provided predicate returns true.
539+
* @param valuePredicate
540+
* the predicate that receives the onNext value
541+
* and should return true for the expected value.
542+
* @return this
543+
*/
544+
public final TestObserver<T> assertValue(Predicate<T> valuePredicate) {
545+
int s = values.size();
546+
if (s == 0) {
547+
throw fail("No values");
548+
}
549+
550+
boolean found = false;
551+
552+
try {
553+
if (valuePredicate.test(values.get(0))) {
554+
found = true;
555+
}
556+
} catch (Exception ex) {
557+
throw ExceptionHelper.wrapOrThrow(ex);
558+
}
559+
560+
if (found) {
561+
if (s != 1) {
562+
throw fail("Value present but other values as well");
563+
}
564+
} else {
565+
throw fail("Value not present");
566+
}
567+
return this;
568+
}
569+
536570
/** Appends the class name to a non-null value. */
537571
static String valueAndClass(Object o) {
538572
if (o != null) {

src/main/java/io/reactivex/subscribers/TestSubscriber.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,40 @@ public final TestSubscriber<T> assertValue(T value) {
577577
return this;
578578
}
579579

580+
/**
581+
* Asserts that this TestSubscriber received exactly one onNext value for which
582+
* the provided predicate returns true.
583+
* @param valuePredicate
584+
* the predicate that receives the onNext value
585+
* and should return true for the expected value.
586+
* @return this
587+
*/
588+
public final TestSubscriber<T> assertValue(Predicate<T> valuePredicate) {
589+
int s = values.size();
590+
if (s == 0) {
591+
throw fail("No values");
592+
}
593+
594+
boolean found = false;
595+
596+
try {
597+
if (valuePredicate.test(values.get(0))) {
598+
found = true;
599+
}
600+
} catch (Exception ex) {
601+
throw ExceptionHelper.wrapOrThrow(ex);
602+
}
603+
604+
if (found) {
605+
if (s != 1) {
606+
throw fail("Value present but other values as well");
607+
}
608+
} else {
609+
throw fail("Value not present");
610+
}
611+
return this;
612+
}
613+
580614
/** Appends the class name to a non-null value. */
581615
static String valueAndClass(Object o) {
582616
if (o != null) {

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void testNullExpected() {
178178
to.onNext(1);
179179

180180
try {
181-
to.assertValue(null);
181+
to.assertValue((Integer) null);
182182
} catch (AssertionError ex) {
183183
// this is expected
184184
return;
@@ -1205,4 +1205,62 @@ public void asyncFusion() {
12051205
.assertFusionMode(QueueDisposable.ASYNC)
12061206
.assertResult(1);
12071207
}
1208-
}
1208+
1209+
@Test
1210+
public void assertValuePredicateEmpty() {
1211+
TestObserver<Object> ts = new TestObserver<Object>();
1212+
1213+
Observable.empty().subscribe(ts);
1214+
1215+
thrown.expect(AssertionError.class);
1216+
thrown.expectMessage("No values");
1217+
ts.assertValue(new Predicate<Object>() {
1218+
@Override public boolean test(final Object o) throws Exception {
1219+
return false;
1220+
}
1221+
});
1222+
}
1223+
1224+
@Test
1225+
public void assertValuePredicateMatch() {
1226+
TestObserver<Integer> ts = new TestObserver<Integer>();
1227+
1228+
Observable.just(1).subscribe(ts);
1229+
1230+
ts.assertValue(new Predicate<Integer>() {
1231+
@Override public boolean test(final Integer o) throws Exception {
1232+
return o == 1;
1233+
}
1234+
});
1235+
}
1236+
1237+
@Test
1238+
public void assertValuePredicateNoMatch() {
1239+
TestObserver<Integer> ts = new TestObserver<Integer>();
1240+
1241+
Observable.just(1).subscribe(ts);
1242+
1243+
thrown.expect(AssertionError.class);
1244+
thrown.expectMessage("Value not present");
1245+
ts.assertValue(new Predicate<Integer>() {
1246+
@Override public boolean test(final Integer o) throws Exception {
1247+
return o != 1;
1248+
}
1249+
});
1250+
}
1251+
1252+
@Test
1253+
public void assertValuePredicateMatchButMore() {
1254+
TestObserver<Integer> ts = new TestObserver<Integer>();
1255+
1256+
Observable.just(1, 2).subscribe(ts);
1257+
1258+
thrown.expect(AssertionError.class);
1259+
thrown.expectMessage("Value present but other values as well");
1260+
ts.assertValue(new Predicate<Integer>() {
1261+
@Override public boolean test(final Integer o) throws Exception {
1262+
return o == 1;
1263+
}
1264+
});
1265+
}
1266+
}

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,4 +1592,61 @@ public void asyncQueueThrows() {
15921592
.assertFailure(TestException.class);
15931593
}
15941594

1595-
}
1595+
@Test
1596+
public void assertValuePredicateEmpty() {
1597+
TestSubscriber<Object> ts = new TestSubscriber<Object>();
1598+
1599+
Flowable.empty().subscribe(ts);
1600+
1601+
thrown.expect(AssertionError.class);
1602+
thrown.expectMessage("No values");
1603+
ts.assertValue(new Predicate<Object>() {
1604+
@Override public boolean test(final Object o) throws Exception {
1605+
return false;
1606+
}
1607+
});
1608+
}
1609+
1610+
@Test
1611+
public void assertValuePredicateMatch() {
1612+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
1613+
1614+
Flowable.just(1).subscribe(ts);
1615+
1616+
ts.assertValue(new Predicate<Integer>() {
1617+
@Override public boolean test(final Integer o) throws Exception {
1618+
return o == 1;
1619+
}
1620+
});
1621+
}
1622+
1623+
@Test
1624+
public void assertValuePredicateNoMatch() {
1625+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
1626+
1627+
Flowable.just(1).subscribe(ts);
1628+
1629+
thrown.expect(AssertionError.class);
1630+
thrown.expectMessage("Value not present");
1631+
ts.assertValue(new Predicate<Integer>() {
1632+
@Override public boolean test(final Integer o) throws Exception {
1633+
return o != 1;
1634+
}
1635+
});
1636+
}
1637+
1638+
@Test
1639+
public void assertValuePredicateMatchButMore() {
1640+
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
1641+
1642+
Flowable.just(1, 2).subscribe(ts);
1643+
1644+
thrown.expect(AssertionError.class);
1645+
thrown.expectMessage("Value present but other values as well");
1646+
ts.assertValue(new Predicate<Integer>() {
1647+
@Override public boolean test(final Integer o) throws Exception {
1648+
return o == 1;
1649+
}
1650+
});
1651+
}
1652+
}

0 commit comments

Comments
 (0)