Skip to content

Commit 5f1ce20

Browse files
vanniktechakarnokd
authored andcommitted
2.x: Add assertValueSetOnly and assertValueSequenceOnly to TestObserver + TestSubscriber (#6010)
* 2.x: Add assertValueSetOnly and assertValueSequenceOnly to TestObserver + TestSubscriber. * Experimental * Lowercase
1 parent f87879d commit 5f1ce20

File tree

3 files changed

+300
-22
lines changed

3 files changed

+300
-22
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,21 @@ public final U assertValueSet(Collection<? extends T> expected) {
587587
return (U)this;
588588
}
589589

590+
/**
591+
* Assert that the TestObserver/TestSubscriber received only the specified values in any order without terminating.
592+
* @param expected the collection of values expected in any order
593+
* @return this;
594+
* @since 2.1.14 - experimental
595+
*/
596+
@SuppressWarnings("unchecked")
597+
@Experimental
598+
public final U assertValueSetOnly(Collection<? extends T> expected) {
599+
return assertSubscribed()
600+
.assertValueSet(expected)
601+
.assertNoErrors()
602+
.assertNotComplete();
603+
}
604+
590605
/**
591606
* Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
592607
* @param sequence the sequence of expected values in order
@@ -625,6 +640,21 @@ public final U assertValueSequence(Iterable<? extends T> sequence) {
625640
return (U)this;
626641
}
627642

643+
/**
644+
* Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.
645+
* @param sequence the sequence of expected values in order
646+
* @return this;
647+
* @since 2.1.14 - experimental
648+
*/
649+
@SuppressWarnings("unchecked")
650+
@Experimental
651+
public final U assertValueSequenceOnly(Iterable<? extends T> sequence) {
652+
return assertSubscribed()
653+
.assertValueSequence(sequence)
654+
.assertNoErrors()
655+
.assertNotComplete();
656+
}
657+
628658
/**
629659
* Assert that the TestObserver/TestSubscriber terminated (i.e., the terminal latch reached zero).
630660
* @return this;

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

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ public void withTag() {
14341434
.assertResult(1)
14351435
;
14361436
}
1437-
fail("Should have thrown!");
1437+
throw new RuntimeException("Should have thrown!");
14381438
} catch (AssertionError ex) {
14391439
assertTrue(ex.toString(), ex.toString().contains("testing with item=2"));
14401440
}
@@ -1466,7 +1466,7 @@ public void assertValuesOnlyThrowsOnUnexpectedValue() {
14661466

14671467
try {
14681468
to.assertValuesOnly(5);
1469-
fail();
1469+
throw new RuntimeException();
14701470
} catch (AssertionError ex) {
14711471
// expected
14721472
}
@@ -1481,7 +1481,7 @@ public void assertValuesOnlyThrowsWhenCompleted() {
14811481

14821482
try {
14831483
to.assertValuesOnly();
1484-
fail();
1484+
throw new RuntimeException();
14851485
} catch (AssertionError ex) {
14861486
// expected
14871487
}
@@ -1496,7 +1496,131 @@ public void assertValuesOnlyThrowsWhenErrored() {
14961496

14971497
try {
14981498
to.assertValuesOnly();
1499-
fail();
1499+
throw new RuntimeException();
1500+
} catch (AssertionError ex) {
1501+
// expected
1502+
}
1503+
}
1504+
1505+
@Test
1506+
public void assertValueSetOnly() {
1507+
TestObserver<Integer> to = TestObserver.create();
1508+
to.onSubscribe(Disposables.empty());
1509+
to.assertValueSetOnly(Collections.<Integer>emptySet());
1510+
1511+
to.onNext(5);
1512+
to.assertValueSetOnly(Collections.singleton(5));
1513+
1514+
to.onNext(-1);
1515+
to.assertValueSetOnly(new HashSet<Integer>(Arrays.asList(5, -1)));
1516+
}
1517+
1518+
@Test
1519+
public void assertValueSetOnlyThrowsOnUnexpectedValue() {
1520+
TestObserver<Integer> to = TestObserver.create();
1521+
to.onSubscribe(Disposables.empty());
1522+
to.assertValueSetOnly(Collections.<Integer>emptySet());
1523+
1524+
to.onNext(5);
1525+
to.assertValueSetOnly(Collections.singleton(5));
1526+
1527+
to.onNext(-1);
1528+
1529+
try {
1530+
to.assertValueSetOnly(Collections.singleton(5));
1531+
throw new RuntimeException();
1532+
} catch (AssertionError ex) {
1533+
// expected
1534+
}
1535+
}
1536+
1537+
@Test
1538+
public void assertValueSetOnlyThrowsWhenCompleted() {
1539+
TestObserver<Integer> to = TestObserver.create();
1540+
to.onSubscribe(Disposables.empty());
1541+
1542+
to.onComplete();
1543+
1544+
try {
1545+
to.assertValueSetOnly(Collections.<Integer>emptySet());
1546+
throw new RuntimeException();
1547+
} catch (AssertionError ex) {
1548+
// expected
1549+
}
1550+
}
1551+
1552+
@Test
1553+
public void assertValueSetOnlyThrowsWhenErrored() {
1554+
TestObserver<Integer> to = TestObserver.create();
1555+
to.onSubscribe(Disposables.empty());
1556+
1557+
to.onError(new TestException());
1558+
1559+
try {
1560+
to.assertValueSetOnly(Collections.<Integer>emptySet());
1561+
throw new RuntimeException();
1562+
} catch (AssertionError ex) {
1563+
// expected
1564+
}
1565+
}
1566+
1567+
@Test
1568+
public void assertValueSequenceOnly() {
1569+
TestObserver<Integer> to = TestObserver.create();
1570+
to.onSubscribe(Disposables.empty());
1571+
to.assertValueSequenceOnly(Collections.<Integer>emptyList());
1572+
1573+
to.onNext(5);
1574+
to.assertValueSequenceOnly(Collections.singletonList(5));
1575+
1576+
to.onNext(-1);
1577+
to.assertValueSequenceOnly(Arrays.asList(5, -1));
1578+
}
1579+
1580+
@Test
1581+
public void assertValueSequenceOnlyThrowsOnUnexpectedValue() {
1582+
TestObserver<Integer> to = TestObserver.create();
1583+
to.onSubscribe(Disposables.empty());
1584+
to.assertValueSequenceOnly(Collections.<Integer>emptyList());
1585+
1586+
to.onNext(5);
1587+
to.assertValueSequenceOnly(Collections.singletonList(5));
1588+
1589+
to.onNext(-1);
1590+
1591+
try {
1592+
to.assertValueSequenceOnly(Collections.singletonList(5));
1593+
throw new RuntimeException();
1594+
} catch (AssertionError ex) {
1595+
// expected
1596+
}
1597+
}
1598+
1599+
@Test
1600+
public void assertValueSequenceOnlyThrowsWhenCompleted() {
1601+
TestObserver<Integer> to = TestObserver.create();
1602+
to.onSubscribe(Disposables.empty());
1603+
1604+
to.onComplete();
1605+
1606+
try {
1607+
to.assertValueSequenceOnly(Collections.<Integer>emptyList());
1608+
throw new RuntimeException();
1609+
} catch (AssertionError ex) {
1610+
// expected
1611+
}
1612+
}
1613+
1614+
@Test
1615+
public void assertValueSequenceOnlyThrowsWhenErrored() {
1616+
TestObserver<Integer> to = TestObserver.create();
1617+
to.onSubscribe(Disposables.empty());
1618+
1619+
to.onError(new TestException());
1620+
1621+
try {
1622+
to.assertValueSequenceOnly(Collections.<Integer>emptyList());
1623+
throw new RuntimeException();
15001624
} catch (AssertionError ex) {
15011625
// expected
15021626
}

0 commit comments

Comments
 (0)